admin管理员组文章数量:1315090
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 badges2 Answers
Reset to default 6Try 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
本文标签: javascriptGoogle maps LatLng not a numberStack Overflow
版权声明:本文标题:javascript - Google maps LatLng not a number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741972749a2407938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论