Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Cordinates on mouse click on google map api v3 and angularJS

$scope.map = {
    center: {
        latitude: 36,
        longitude: -80
    },
    zoom: 3,
    showOverlay: true,
    events: {
        "click": function ()
        { console.log(" latitude: " + $scope.map.center.latitude + "longitude: " + $scope.map.center.longitude); }
    },
    options: {
        streetViewControl: false,
        panControl: false,
        maxZoom: 20,
        minZoom: 1,

    }
}

So I have a problem to get the longitude and latitude of my mouse click event using angularJS and the google map api v3. Here is an example of my controller. I've added an event to the map so to get the cordinates, but now what i'm getting is just the cordinates of the center. I don't have any idea how to get the exact cordinates of my mouse click event. Thanks for your help.

like image 529
Ayyoub Avatar asked Mar 31 '15 07:03

Ayyoub


People also ask

How do I pull coordinates from Google Maps?

Touch and hold an area of the map that isn't labeled to drop a red pin after open the Google Maps app on your Android phone or iPhone devices. If you're Android users, you can find the coordinates on the search box. For iPhone, tap Dropped pin to find the coordinates at the bottom.


2 Answers

You must implement a listener that catches the click and gets location:

google.maps.event.addListener(map, 'click', function(event) {
    alert(event.latLng);  // in event.latLng  you have the coordinates of click
});

Check this DEMO FIDDLE

like image 191
Jordi Castilla Avatar answered Oct 11 '22 15:10

Jordi Castilla


The click event in Angular and google maps v3 returns three objects: instance, eventName, handler.

The third one has latLong with lat(), lng() functions.

So you can get latitude by handler[0].latLng.lat().

There is also a useful pixel object with position attributes x,y of the clicked point.

like image 40
AK-101111 Avatar answered Oct 11 '22 13:10

AK-101111