简体   繁体   中英

Object has no method “charAt”

I'm using the following function to get the elevation on Google Maps:

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]) {
                $('#elevation').html(parseInt(s).charAt(0) + ' meter');
            } else {
                alert('Inget resultat hittades');
            }
        } else {
            alert('Det gick inte att hitta höjdskillnaden på grund av följande: ' + status);
        }
    });
}

I'm getting Uncaught TypeError: Object 65 has no method 'charAt' when I click somewhere on the map and I don't know how I shall fix this problem. parseInt(s) prints for example 44 depending on where you click on the map. If I click on the ocean it shows for example -4837 and it's just that minus character I want to identify if it's exists in this string.

Any ideas of how I can fix this problem?

Thanks in advance.

Numbers don't have a "charAt()" method on their prototype, but strings do.

$('#elevation').html(('' + parseInt(s)).charAt(0) + ' meter');

The parseInt() function returns a number. You have to explicitly convert it to a string first.

Why it is that you're trying to make it a number in the first place is a little mysterious.

parseInt(s).charAt(0)

You are parsing a string and returning an integer, and then trying to call charAt on an integer, which won't work too well. You should call charAt before you parse it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM