简体   繁体   English

对象没有方法“charAt”

[英]Object has no method “charAt”

I'm using the following function to get the elevation on Google Maps: 我正在使用以下函数来获取Google地图上的高程:

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. 我得到了Uncaught TypeError: Object 65 has no method 'charAt'当我点击地图上的某个地方时, Uncaught TypeError: Object 65 has no method 'charAt' ,我不知道如何解决这个问题。 parseInt(s) prints for example 44 depending on where you click on the map. parseInt(s)打印例如44,具体取决于您在地图上单击的位置。 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. 如果我点击它显示的海洋例如-4837并且它只是那个减去字符我想要识别它是否存在于该字符串中。

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. 数字在其原型上没有“charAt()”方法,但是字符串可以。

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

The parseInt() function returns a number. parseInt()函数返回一个数字。 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. 您正在解析一个字符串并返回一个整数,然后尝试在整数上调用charAt,这将无法正常工作。 You should call charAt before you parse it. 你应该在解析它之前调用charAt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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