dylanfprice / angular-gm

AngularJS Google Maps Directives
MIT License
198 stars 47 forks source link

Opening/closing infoWindow on marker-click #63

Closed igler closed 10 years ago

igler commented 10 years ago

I have created a Plunker at http://plnkr.co/edit/eGa4A7TcV1QAq0ZzO3Xs?p=preview First click on marker should open the infoWindow, a second click on the marker should close it again. Does anyone knows how to achieve this?

igler commented 10 years ago

Have found a solution:

$scope.triggerInfoWindow = function (entity, infoWindow, marker, contentFunction) {
if (infoWindow.getMap() != null) {
infoWindow.close();
if (infoWindow.id != entity.id) {
infoWindow.content = contentFunction(entity).toString();
infoWindow.id = entity.id;
infoWindow.open(marker.getMap(), marker);
}
} else {
infoWindow.content = contentFunction(entity).toString();
infoWindow.id = entity.id;
infoWindow.open(marker.getMap(), marker);
}
};

The HTML code is:

...
<div gm-info-window="infoWindowLocation"></div>
...
<gm-markers gm-objects="filteredLocations"
            gm-id="object.id"
            gm-position="{lat: object.latitude, lng: object.longitude}"
            gm-marker-options="getLocationMarkerOptions(object)"
            gm-events="markerEvents"
            gm-on-click="triggerInfoWindow(object, infoWindowLocation, marker, getLocationMarkerText)">
</gm-markers>
...