Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the lat & lng as separate variables from mapCenter

I have the map center, and comes as a Floating Point number.

How can I get it as 2 separated variables like this:

var lat = ...

var lng = ...

I have the float:

var NewMapCenter = map.getCenter();

I tried to pass the float toWords(x) but didn't work

I tried to subtract the (,) and split it, but didn't work

var latlngStr = (StrNewMapCenter.substr(1, (StrNewMapCenter.length-1))).split(",",2);
var NewMapCenter = parseFloat(latlngStr[0]);

I tried, this but comes as an event, but I need it without needing to click.

google.maps.event.addListener(map, 'click', function(event) {
    var myLatLng = event.latlng;
    var Newlat = position.coords.latitude;
    var Newlng = position.coords.longitude;
}

Thanks Sebastian

like image 254
Sebastian Avatar asked Jan 20 '12 14:01

Sebastian


1 Answers

var NewMapCenter = map.getCenter();

will give you LatLng object, so you can call

var latitude = NewMapCenter.lat();
var longitude = NewMapCenter.lng();
like image 139
Ramesh Kotha Avatar answered Nov 15 '22 04:11

Ramesh Kotha