The object does not have an 'indexOf' method

I have the following function (taken from the Elevation Service @API of Google Maps ) that displays, for example 63.00425720214844, when I click somewhere on a map that I created using the Google Maps JavaScript API v3 :

function getElevation(event) {
    var locations = [];
    var clickedLocation = event.latLng;
    locations.push(clickedLocation);

    var positionalRequest = {
        'locations': locations
    }

    elevator.getElevationForLocations(positionalRequest, function(results, status) {
        if(status == google.maps.ElevationStatus.OK) {
            var s = results[0].elevation
            if(results[0]) {
                alert(s.substring(0, s.indexOf('.') - 1));
            } else {
                alert('Inget resultat hittades');
            }
        } else {
            alert('Det gick inte att hitta höjdskillnaden på grund av följande: ' + status);
        }
    });
}

I want to delete everything after the decimal point, including the point, for example, delete .00425720214844from 63.00425720214844, but when I click somewhere on the map, I get this error message in the console: Uncaught TypeError: Object 63.00425720214844 has no method 'indexOf'.

What did I do wrong?

Thanks in advance.

+3
source share
2 answers

Just do javascript parseInt(63.00425720214844)to get 63.

+5
source

The variable sdoes not contain a string.

, :

s = s.toString();

, :

alert(Math.floor(s));
+7

All Articles