如何在GoularJS的Google地图中点击标记时关闭其他infoWindows

时间:2014-04-04 13:32:15

标签: javascript angularjs google-maps google-maps-api-3

我使用Google Maps for AngularJS 用infoWindow放一些标记。 单击标记时,它可以打开infoWindow。 但是当单击另一个标记时,它无法关闭打开的infoWindow。

<div id="map" ng-controller="MapCtrl">
    <google-map 
        center="map.center" 
        zoom="map.zoom" 
        draggable="true" 
        dragging="map.dragging" 
        events="map.events" 
        control="map.control" 
        refresh="true">
        <markers models="map.markers" coords="'self'" icon="'icon'" click="'onClicked'">
      <windows show="'showWindow'" closeClick="'closeClick'" ng-cloak>
        <img ng-non-bindable src="{{url}}" width="200px;" class="img-responsive" />
      </windows>
        </markers>
    </google-map>
</div>

这是controller.js

var mapControllers = angular.module('mapControllers', ['google-maps']);

mapControllers.controller('MapCtrl', function($scope, $http) {
var onMarkerClicked = function (marker) {
    _.each($scope.map.markers, function (mker) {
    mker.showWindow = false;
});
$scope.$apply();
  marker.showWindow = true;
};

$scope.map = {
    current_marker: {},
    control:{},
    markers: [],
center: {
    latitude: 35.4138,
    longitude: 139.4505
},
zoom: 6,
dragging: true
};

$http.get('/v1/tweets').success(function(data) {
    for (var i = 0; i < data.length; i++) {
        var marker = { 
        latitude: data[i].lat, 
        longitude: data[i].lon, 
        text: data[i].text, 
        url: data[i].url, 
        icon: '/assets/flower.png',
        showWindow: false
    };
    marker.closeClick = function () {
      marker.showWindow = false;
      $scope.$apply();
      };
      marker.onClicked = function () {
        onMarkerClicked(marker);
      };
    $scope.map.markers.push(marker);
  }
});
});

如何更改此代码?

1 个答案:

答案 0 :(得分:-1)

使用jQuery进行肮脏的破解:

$("img[src='https://maps.gstatic.com/mapfiles/api-3/images/mapcnt3.png']").click();

在显示新的Infowindow之前关闭/单击所有打开的窗口。