简体   繁体   中英

marker click event not firing second time in google maps

I am using ui-gmap-google-map AngularJs directive and ui-gmap-marker for marker with a ng-repeat and a click event calling a function in the controller. Everything seems to work. The Map loads up with marker and marker click firing for all markers for the first time when I click on each of the markers. But from the second attempt on any of the marker to click does not trigger the click event.

Here are code snippets

<ui-gmap-google-map center="map.center" zoom="map.zoom">
    <!--<ui-gmap-markers models="map.markers" fit="true" coords="'self'" icon="'icon'" idkey="map.marker.id" click="selectMarker()">-->
    <ui-gmap-markers models="map.markers" fit="true" coords="'self'" icon="'icon'" idkey='m.id'>
        <ui-gmap-marker ng-repeat="m in map.markers" coords="m" icon="m.icon" ng-click='onMarkerClicked(m.sequenceId)' idkey='m.id'>

        </ui-gmap-marker>
    </ui-gmap-markers>
</ui-gmap-google-map>

..... same result for 'click' and 'ng-click' events ..

$scope.generateMarkers = function (referencePoints) {
            $scope.map.markers.length = 0;
            //var bounds = new google.maps.LatLngBounds();
            for (i = 0; i < referencePoints.length; i++) {
                var record = referencePoints[i];
                var marker = {
                    latitude: record.Lat,
                    longitude: record.Long,
                    title: record.RoadName,
                    id: record.ID,
                    icon: { url: '/content/images/roundcyan.png' },//pin_url(record.ID)//
                    sequenceId:i
                };
                //var position = new google.maps.LatLng(record.Lat,record.Long);
                //bounds.extend(position);
                $scope.map.markers.push(marker);
            }
            //$scope.map.fitBounds(bounds);

        };

.... ..

$scope.onMarkerClicked = function (sequenceId) {
    if ($scope.selectedSequenceId >=0) {
        $scope.map.markers[$scope.selectedSequenceId].icon = { url: 'http://localhost:xxxxx/content/images/roundcyan.png' };
    }
    $scope.selectedSequenceId = sequenceId;
    $scope.map.markers[sequenceId].icon = { url: 'http://localhost:xxxxx/content/images/roundcyanplus.png' };
    $scope.$apply();
};

您可能必须取消事件传播

ng-click="onMarkerClicked(); $event.stopPropagation()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM