javascript 将经纬度字符串转换为谷歌地图 API LatLng 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26890514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert lat long string into google maps API LatLng Object
提问by Gemma Gallagher
I am trying to convert a lat long string (53.3603, -6.315050000000042) into a google maps object in this format: B: -6.26747649999993 k: 53.339251
我正在尝试将 lat 长字符串 (53.3603, -6.315050000000042) 转换为以下格式的谷歌地图对象:B: -6.26747649999993 k: 53.339251
I have used this function which works for the long but returns Nan for the lat.
我已经使用了这个函数,它可以长时间工作,但在纬度上返回 Nan。
function getLatLngFromString(ll) {
var lat = ll.replace(/\s*\,.*/, ''); // first 123
var lng = ll.replace(/.*,\s*/, ''); // second ,456
var locate = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
return locate;
}
;
I am storing the lat long in a html tag as it called to a list view connected to a map. It is stored here in the correct format however when I retrieve it using the jQuery .attr it converts it to a string.
我将 lat long 存储在 html 标签中,因为它调用了连接到地图的列表视图。它以正确的格式存储在这里,但是当我使用 jQuery .attr 检索它时,它会将其转换为字符串。
var location = $(this).closest('.allList').attr("position");
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by Jordan Burnett
Assuming the lat/lng string looks like "10.5, -0.51"
假设 lat/lng 字符串看起来像 "10.5, -0.51"
function getLatLngFromString(ll) {
var latlng = ll.split(/, ?/)
return new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1]));
}
Should work
应该管用