3

I'm attempting to use this Angular Google map directive http://ngmap.github.io/. I would like to add a click event to each marker, which calls a function in the controller, passing through the marker's id. When I print out the id in the console, I get "hq {latLng: wf, ub: undefined, pixel: undefined, xa: undefined}", which isn't the id. My HTML is

<map center="{{map.center.latitude}},{{map.center.longitude}}" zoom="{{map.zoom}}">

                    <marker ng-repeat="marker in markers" position="{{marker.latitude}}, {{marker.longitude}}" icon="{{marker.icon}}" id="{{marker.id}}" on-click="goAnchor(marker.id)"></marker>
                </map>

My controller code:

$scope.goAnchor = function (id) {
            console.log(id);
            gotoAnchor(id);
        };

3 Answers 3

2

"this" returns the marker clicked by you

$scope.goAnchor = function (event) {
 console.log(this.id);
 gotoAnchor(this.id);
};

HTML

<marker ng-repeat="marker in markers" position="{{marker.latitude}},{{marker.longitude}}" icon="{{marker.icon}}" id="{{marker.id}}" on-click="goAnchor($event)"></marker>
Sign up to request clarification or add additional context in comments.

1 Comment

how do you access scope variable inside such callback functions? I get it as undefined.
0

You should use in function

$scope.goAnchor = function (event) {
   console.log(event.target.attributes.id.value);
   gotoAnchor(event.target.attributes.id.value);
};

Mark Up

<marker ng-repeat="marker in markers" position="{{marker.latitude}}, {{marker.longitude}}" icon="{{marker.icon}}" id="{{marker.id}}" on-click="goAnchor($event)"></marker>

5 Comments

That's what I thought, but the documentation shows on-click. ngmap.github.io/events.html# I tried ng-click and that doesn't even call the controller, on-click does.
I never see on-click. But I guess to pass the function's value in controller's $scope we need to use it's predefined directives.
Still no luck. Uncaught TypeError: Cannot read property 'attributes' of undefined
@user1024941 My Bad. Please check my edited answer. I forgot to pass $event in on-click
0

I used below code to get Latitude and Longitude of clicked marker.

 <ng-map zoom="12" center="{{vm.geoCenterLoc}}" style="height:500px">
            <marker ng-repeat="p in vm.positions"
                    position="{{p.pos}}"
                    data="{{data[$index]}}"
                    on-click="myFunc($event)";
                    title="pos: {{p.pos}}"></marker>
   </ng-map>

Javascript(on my controller):

$scope.myFunc = function (evt) {

            markerPosObj = {
                pos: [this.getPosition().lat(), this.getPosition().lng()]
            };
            markerPos = [];
            markerPos.push(markerPosObj);
            console.log('this marker', markerPos);
        }

Comments

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.