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

javascript - Convert lat long string into google maps API LatLng Object - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Nov 12, 2014 at 15:26 Gemma GallagherGemma Gallagher 471 gold badge1 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

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

发布评论

评论列表(0)

  1. 暂无评论