Convert long long string to google maps API LatLng Object

I am trying to convert a long lat line (53.3603, -6.315050000000042) to a google map object in this format: B: -6.26747649999993 k: 53.339251

I used this function, which works for a long time, but returns Nan for 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 save lat long in the html tag since it caused the list view associated with the map. It is saved here in the correct format, however, when I extract it using jQuery.attr, it converts it to a string.

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

Any help would be greatly appreciated.

-one
javascript jquery latitude-longitude google-maps-api-3
source share
1 answer

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

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

Must work

+9
source share

All Articles