最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google maps LatLng not a number - Stack Overflow

programmeradmin1浏览0评论

I'm trying to create polygon markers on a Google map by converting a string containing lat/lng positions into an array. I'm running into the following error:

mc {message: "at index 0: not a LatLng or LatLngLiteral: in property lat: not a number", name: "InvalidValueError"

When i inspect the array in the browser console it looks fine (index[0]):

lat:"12.3394541791301"
lng:"55.59020913559151"

The string has the following structure:

'12.3394541791301,55.59020913559151, 12.3444451791347,55.5930451355941, 12.3808361791686,55.6099911356099,

So first i replace every second ma with ;

When i use split(";") to create my array, which i when loop and push into a new array with the right keys:

s = s.replace(/([^,]+,[^,]+),/g,'$1;');
s = s.split(";");

for (var i = 0; i < s.length; i++) {
        var lat = s[i].split(',')[0];
        var lng = s[i].split(',')[1];
        // Push to array which is used by Google Maps
        denmarkLatLong.push({lat: lat, lng: lng})
}

Any suggestions to what could cause the problem?

I'm trying to create polygon markers on a Google map by converting a string containing lat/lng positions into an array. I'm running into the following error:

mc {message: "at index 0: not a LatLng or LatLngLiteral: in property lat: not a number", name: "InvalidValueError"

When i inspect the array in the browser console it looks fine (index[0]):

lat:"12.3394541791301"
lng:"55.59020913559151"

The string has the following structure:

'12.3394541791301,55.59020913559151, 12.3444451791347,55.5930451355941, 12.3808361791686,55.6099911356099,

So first i replace every second ma with ;

When i use split(";") to create my array, which i when loop and push into a new array with the right keys:

s = s.replace(/([^,]+,[^,]+),/g,'$1;');
s = s.split(";");

for (var i = 0; i < s.length; i++) {
        var lat = s[i].split(',')[0];
        var lng = s[i].split(',')[1];
        // Push to array which is used by Google Maps
        denmarkLatLong.push({lat: lat, lng: lng})
}

Any suggestions to what could cause the problem?

Share Improve this question asked Aug 8, 2017 at 6:35 TietjeDKTietjeDK 1,2072 gold badges18 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Try to do this way:

These pluses will convert your string values of lat, lng to numeric. Or try parseFloat()

 denmarkLatLong.push({lat: +lat, lng: +lng})

OR

denmarkLatLong.push({lat: parseFloat(lat), lng: parseFloat(lng)})    

Problem that you have strings instead of numbers. Just convert, say if it helped.

lat:"12.3394541791301" // strings, convert to numbers
lng:"55.59020913559151"

I'm not sure what a polygon marker is but I suspect you meant marker.

If you look at the latlng documentation the value for key lat and lng are supposed to be Number not String. So to fix your problem you can use the api Number(...) to cast the string coordinates into numbers.

Example:

s = s.replace(/([^,]+,[^,]+),/g,'$1;');
s = s.split(";");

for (var i = 0; i < s.length; i++) {
        var lat = s[i].split(',')[0];
        var lng = s[i].split(',')[1];
        // Push to array which is used by Google Maps
        denmarkLatLong.push({lat: Number(lat), lng: Number(lng) })
}

References:

LatLng: https://developers.google./maps/documentation/javascript/examples/map-latlng-literal

发布评论

评论列表(0)

  1. 暂无评论