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

16,492
var NewMapCenter = map.getCenter();

will give you LatLng object, so you can call

var latitude = NewMapCenter.lat();
var longitude = NewMapCenter.lng();
Share:
16,492

Related videos on Youtube

Sebastian
Author by

Sebastian

Updated on June 04, 2022

Comments

  • Sebastian
    Sebastian almost 2 years

    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

  • nrabinowitz
    nrabinowitz over 12 years
    @Sebastian - if this is the right answer, you can mark it as correct by clicking the checkbox to the left of it. That'll close the question and give some extra rep to the person answering.
  • Sebastian
    Sebastian over 12 years
    Thanks, I didn't know, I just did it ;o) @nrabinowitz