简体   繁体   中英

Google maps API - LatLng returning NaN

For some unknown reason my latlng object returns 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]) works fine but for some reason the parsefloat(markers[1]) will always return NaN. Also myLatLng1 will always return the second value as NaN even if I put fixed numbers as values eg new google.maps.LatLng(60.1932146, 60.1932146);

Can't for the life of me figure out why. Any tips?

You have to use

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

because markers in an array with one element which is an array of two elements.

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