5
$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.

3 Answers 3

13

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

Sign up to request clarification or add additional context in comments.

3 Comments

That's working using Standard JS, but i'm using Controllers with AngularJS and it's not working like that, events are on my map but latLng is not defined and I think i can't get position like that.
If you cant use javascript delete the tag from your question... if not, define the map with angularjs and use javascript to implement listeners...
Thanks a lot man, I will try that and combine it with the github solution given by kiro112.
1

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.

2 Comments

any reason for the down vote? I've solved the exact problem asked with this solution.
This should be the real answer,
0

If you are using angular why wont you use its directive for maps. ngMap

3 Comments

There are many directives for google maps. He is using a different directive. ngMap is not the only one.
He is actually not using any directives.. He is just putting Map info on the controller $scope.
I am using Angular Google Maps and I have the exact same setup in my controller. A directive is in the HTML the logic is in the controller. NgMap uses names functions connected to their directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.