将lat长字符串转换为Google Maps API LatLng对象 [英] Convert lat long string into google maps API LatLng Object

查看:127
本文介绍了将lat长字符串转换为Google Maps API LatLng对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将lat长字符串(53.3603,-6.315050000000042)转换为以下格式的Google地图对象:B:-6.26747649999993 k:53.339251

I

 函数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;
}
;

我将lat long存储在一个html标记中,因为它调用一个连接到地图的列表视图。它在这里以正确的格式存储,但是当我使用jQuery.attr检索它时,它将它转换为一个字符串。

  var location = $(this).closest('。allList')。attr(position); 

任何帮助都将不胜感激。

解决方案

假设lat / lng字符串看起来像10.5,-0.51

 函数getLatLngFromString(ll){
var latlng = ll.split(/,?/)
return new google.maps.LatLng(parseFloat(latlng [0]),parseFloat(latlng [1]));
}

应该可以工作


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

I have used this function which works for the long but returns Nan for the lat.

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.

var location = $(this).closest('.allList').attr("position");

Any help would be greatly appreciated.

解决方案

Assuming the lat/lng string looks like "10.5, -0.51"

function getLatLngFromString(ll) {
    var latlng = ll.split(/, ?/)
    return new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1])); 
}

Should work

这篇关于将lat长字符串转换为Google Maps API LatLng对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆