简体   繁体   English

在Google Maps中单击时,AngularJS打开窗口

[英]AngularJS open window when click in Google Maps

I am building a website with different markers shown in a Google Maps map and a search engine. 我正在建立一个网站,并在Google Maps地图和搜索引擎中显示不同的标记。 I can navigate through the map with the markers, I can click on a marker to show the content. 我可以使用标记浏览地图,也可以单击标记以显示内容。 When I click on a search results, the map pan to the result and I would like at the same time that the windows with markers content to be opened. 当我单击搜索结果时,地图会平移到结果,同时我希望打开带有标记的窗口。 This is my code 这是我的代码

mymarkers.js mymarkers.js

var app = angular.module('myApp');
app.constant("myMarkers", [{
                lat: .., 
                lng: .., 
                title:'..'
},{
                lat: .., 
                lng: .., 
                title:'..'
}..]

app.js app.js

var app = angular.module('myApp', []);
angular.module('myApp').controller("myController", ["$scope", "myMarkers", function($scope, myMarkers){
    $scope.markers= myMarkers; 
    $scope.$on("gmaps.marker.click", function(event, map, marker) {

    });
    //call the directive with the Angular event system
    //1st parameter of the listener = event
    $scope.centerMapOnMarker = function (marker){
        $scope.$broadcast('center-on-marker', marker);
    }
}]);

app.directive('myMap', ["myMarkers", function(myMarkers) {
    // directive link function
    var link = function(scope, element, attrs) {
        var map, infoWindow;
        var markers = [];
        var mapOptions = {..
        };

        // init the map
        function initMap() {..
        }    

        // place a marker
        function setMarker(map, position, title, content,link) {..
           ..
        }
        //center to the marker   
        //1st parameter of the listener = event
        scope.$on('center-on-marker', function (event, args) {
            var center = new google.maps.LatLng(args.lat, args.lng);
            map.panTo(center);
            map.setZoom(15);
            var infowindow = new google.maps.InfoWindow(); <-- not working
            infowindow.setContent(args.content);
            infowindow.open(map, args);

        });
        function returnMarker(){
           window.alert(markers[0].title);
        }
        // show the map and place some markers
        initMap();

        for (var i = 0; i < 100; i++) {
            setMarker(map, new google.maps.LatLng(myMarkers[i].lat, myMarkers[i].lng), myMarkers[i].title, myMarkers[i].content, myMarkers[i].icon);
        }

    };

    return {
        restrict: 'A',
        template: '<div id="gmaps"></div>',
        replace: true,
        link: link
    };
}]);

index.html index.html

        <div my-map=""></div>

        <div class="myresearch">
            <input placeholder="Search a restaurant" ng-model="search">
                <div ng-show="search.length">
                <ul class="list" ng-repeat="marker in markers | filter: search as filtered"> 
                    <li>
                    <span ng-click="centerMapOnMarker(marker)">
                    <label class="textr"> {{marker.title}}  </label> 
</li>
</ul>

You can see with the arrow which function I would like to add in order to open the window when I click on the search results at the same time that the map move. 当我在地图移动的同时单击搜索结果时,可以用箭头看到要添加的功能以便打开窗口。 Thank you for any help 感谢您的任何帮助

It occurs since InfoWindow.open function expects Marker object as the second argument, but in your example args object is not a Marker class. 由于InfoWindow.open函数期望Marker object为第二个参数,所以会发生这种情况,但是在您的示例中args对象不是 Marker类。 You could pass instead the index of selected object : 您可以改为传递所选对象索引

<span ng-click="centerMapOnMarker($index)" />

$scope.centerMapOnMarker = function(index) {
    $scope.$broadcast('center-on-marker', index);
}

and then get the corresponding marker object: 然后获取相应的标记对象:

scope.$on('center-on-marker', function(event, index) {
   var selectedMarker = markers[index];
   map.panTo(selectedMarker.getPosition());
   map.setZoom(15);
   var infowindow = new google.maps.InfoWindow(); 
   infowindow.setContent(selectedMarker.content);
   infowindow.open(map, selectedMarker);
});

Demo (plunker) 演示(punker)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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