Ayyoub
Ayyoub

Reputation: 1375

Undefined LatLng google map in AngularJs Controller

I want to retrieve the longitude and latitude position of my click mouse event, but i can't, it gives me Undefined. Any one can help me please ?
I'm using AngularJS and I found that we can do

google.maps.event.addListener(map, 'click', function(event) {
    console.log(event.latLng);
});

But I can't do so in my controller it gives me an error, or maybe i don't know how to use that!!

here is my code :

$scope.map = {
        center: {
            latitude: 36,
            longitude: -80 
        },
        events: {
            "click": function (event) {
                console.log(event.latLng);
              }
         }
     }

and I tried that too but it gives me (Nan,Nan)

  $scope.map = {
            center: {
                latitude: 36,
                longitude: -80 
            },
            events: {
                "click": function (event) {
                    var pos = new google.maps.LatLng(event.latLng, event.latLng);
                    console.log("position: " + pos);
                  }
             }
         }

Upvotes: 1

Views: 511

Answers (1)

Ayyoub
Ayyoub

Reputation: 1375

I could solve it like that

  events: {
        tilesloaded: function (map, eventName, originalEventArgs) {
            //map is trueley ready then this callback is hit
        },
        click: function (mapModel, eventName, originalEventArgs) {
            var e = originalEventArgs[0];
            var lat = e.latLng.lat(),lon = e.latLng.lng();
            console.log("lat long: "lat, lon);
            $scope.$apply();
        }
    }

Upvotes: 1

Related Questions