【问题标题】:Google maps API - LatLng returning NaNGoogle 地图 API - LatLng 返回 NaN
【发布时间】:2014-03-06 22:56:00
【问题描述】:

由于某些未知原因,我的 latlng 对象返回 NaN。

HTML:

<td class="addresslatlng" style="display:none">(60.1932146, 24.9068178)</td>
<td class="addresslatlng" style="display:none">(60.1883387, 24.95304060000001)</td>
<td class="addresslatlng" style="display:none">(60.22956294570635, 24.877114473016377)</td>
and so on..

JS:

var geolocation = $(".addresslatlng").text();

var marker1 = $(".addresslatlng").each(function(){
    var markers = []; 
    var value = $(this).text();
    var strippedtemp = value.replace('(', '').replace(')','').replace(' ', '').replace(/\"/g, "");      
    var splittedtemp = strippedtemp.split(",");
    var marker = splittedtemp;
    markers.push(marker);           

    function setMarkers(map, locations) {
        var myLatLng1 = new google.maps.LatLng(parseFloat(markers[0]), parseFloat(markers[1]));
        var marker1 = new google.maps.Marker({
        position: myLatLng1,
        map: new google.maps.Map(document.getElementById('map-canvas2'))
    });


};
setMarkers();   
});

parsefloat(markers[0]) 工作正常,但由于某种原因 parsefloat(markers[1]) 将始终返回 NaN。即使我将固定数字作为值,myLatLng1 也将始终将第二个值返回为 NaN,例如 new google.maps.LatLng(60.1932146, 60.1932146);

我一辈子都想不通为什么。有什么建议吗?

【问题讨论】:

    标签: javascript jquery google-maps


    【解决方案1】:

    你必须使用

    new google.maps.LatLng(parseFloat(markers[0][0]), parseFloat(markers[0][1]));
    

    因为markers 在一个数组中,其中一个元素是两个元素的数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多