打开一个信息窗口,点击带有角度的谷歌地图

时间:2015-12-09 21:40:18

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

我有一个应用程序,我需要在谷歌地图上打开一个信息窗口,当我点击侧栏中的面板时,只显示它1秒钟。在此之后,它不再需要工作了。

我在侧边栏中有一个标记和信息列表,当用户点击其中一些标记时,我需要打开信息窗口。

(function(){
    angular.module('mapCtrl', ['presentacionService'])
        .controller('MapController', function($scope, Presentacion) {
            var self = this;
            function initialize() {
                var options = {
                    googleApiKey: '',
                    locationColumn: 'geometry',
                    map_center: [-16.494898, -68.133553],
                    locationScope: 'La Paz'
                };
                options = options || {};
                self.googleApiKey = options.googleApiKey || "";
                self.locationColumn = options.locationColumn || "geometry";
                self.locationScope = options.locationScope || "";
                self.defaultZoom = options.defaultZoom || 10;
                self.map_centroid = new google.maps.LatLng(options.map_center[0], options.map_center[1]);

                self.myOptions = {
                    zoom: self.defaultZoom,
                    center: self.map_centroid,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                self.map = new google.maps.Map($("#map")[0], self.myOptions);

                var infoWindow = new google.maps.InfoWindow();

                google.maps.event.addListener(map, 'click', function() {
                    infowindow.close();
                });

                $scope.markers = [];
                var createMarker = function (info){
                    var marker = new google.maps.Marker({
                        map: self.map,
                        position: new google.maps.LatLng(info.latitud, info.long),
                        title: info.nombre,
                        date: info.date,
                        imagen: info.imagen,
                        nombre_categoria: info.nombre_categoria
                    });
                    marker.content = '<div class="infoWindowContent">' + info.descripcion + '</div>';
                    google.maps.event.addListener(marker, 'click', function(){
                        infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content);
                        infoWindow.open(self.map, marker);
                    });
                    $scope.markers.push(marker);
                };
                Presentacion.getAll().success(function (datos) {
                    for (var i = 0; i < datos.length; i++){
                        createMarker(datos[i]);
                    }
                });
                $scope.openInfoWindow = function(e, selectedMarker){
                    console.log('show something');
                    e.preventDefault();
                    new google.maps.event.trigger(selectedMarker, 'click' );
                };
            }
            google.maps.event.addDomListener(window, 'load', initialize);

        });
})();

在视图中:

 <div class="row list_special" ng-repeat="marker in markers | orderBy : 'date'">
                        <div class="panel-google-plus" ng-click="openInfoWindow($event, marker)">
                   </div>
</div>

1 个答案:

答案 0 :(得分:1)

我准备了一个Plunker来模拟项目所需的功能。 以下是网址:http://plnkr.co/edit/gIhNLQgfiiD4QfuQQOi4?p=preview

我已使用在MainCtrl中初始化的简单数组places替换了您的服务Presentacion(同时markers是MainCtrl范围的属性,但由MapController继承):

$scope.markers = [];
$scope.places = [];
$scope.places.push({lat: 44.99758207, lng: 7.140598296999997, ...);

您的代码中没有其他编辑功能,它可以按要求运行:您可以点击某个项目,它会随时打开信息窗口。