GoogleMaps - 显示infoWindows(angularjs)

时间:2018-03-19 18:30:42

标签: javascript angularjs google-maps infowindow

我想知道你是否可以帮助我。通过angular js v1,我的网页中嵌入了一个地图,其中包含:

  • 允许用户输入出发地和目的地。
  • 绘制标记并显示从起点和终点的路线。
  • 显示原点,目的地和中间航点的航点的餐馆(作为标记)。 (所以3点,我不要过度使用谷歌api请求/限制。

问题: 当我点击我的标记时,我的InfoWindow不会出现。 默认情况下,它似乎自然会出现在原点和目的地,但我无法弄清楚如何让它们出现在所有显示为标记的餐厅中。 (使用PlaceSearch为此)。

我已经google了很多,但由于我是JS / Angular的新手,我无法找到最好的方法。

我的指令代码如下,带有一些infoWindow代码,但正如你所看到的那样,我很难过。不确定我是否需要点击处理程序?

    googleMap.$inject = [];
     function googleMap() {

    return {
     restrict: 'E',
     template: '<div class="google-map"></div>',
     replace: true,
     scope: {
       center: '=',
       zoom: '=',
       origin: '=',
       destination: '=',
       travelMode: '='
    },

    link($scope, $element) {
      const map = new google.maps.Map($element[0], {
        zoom: $scope.zoom,
        center: $scope.center
      });

      const directionsService = new google.maps.DirectionsService();
      const directionsDisplay = new google.maps.DirectionsRenderer();
      const placesService = new google.maps.places.PlacesService(map);
      // const infoWindows = [];
      // const infowindow = new google.maps.InfoWindow();
      // let marker = new google.maps.Marker;

      directionsDisplay.setMap(map);

      $scope.$watch('center', () => map.setCenter($scope.center), true);

      $scope.$watchGroup(['origin', 'destination', 'travelMode'], 
    displayRoute);

      // DISPLAY ROUTE
      function displayRoute() {
        if(!$scope.origin || !$scope.destination || !$scope.travelMode) 
    return false;

        directionsService.route({
          origin: $scope.origin,
          destination: $scope.destination,
          travelMode: $scope.travelMode
        }, (response) => {

          directionsDisplay.setDirections(response);

          // beginning of this form
          // response.routes[0].legs[0].steps.map(step => {
          const steps = response.routes[0].legs[0].steps
          const lookup = [steps[0], steps[Math.round(steps.length / 2)], 
          steps[steps.length - 1]]
          lookup.map(step => {


            placesService.nearbySearch({
              location: step.start_point,
              radius: 50,
              type: ['restaurant'],
              openNow: true
            }, (results) => {
              results.map(place => {
                console.log(place.name);
                return new google.maps.Marker({
                  map: map,
                  position: place.geometry.location,
                  // label: '⭐️',
                  title: place.name
                });  //google maps marker
              });

              results.map(place => {
                console.log(place.vicinity);
                const contentString = place.name;
                return new google.maps.InfoWindow({
                  title: place.name,
                  content: contentString
                });  //google maps marker
                // infoWindows.push(infowindow);
              });


            });
          }); //end of this function

        });  //end return directionsdisplay
      }  //display route ends


    } //link scope ends
  };
}

export default googleMap;

谢谢!

1 个答案:

答案 0 :(得分:1)

一种方法是创建infowindow和marker - 然后绑定click事件以打开/关闭infowindow -

    var marker = new google.maps.Marker({
        position: latlng,
        map: mapObject,
        title: "MARKER"
    });

    var infoWindow = new google.maps.InfoWindow({
        content: "<h1>Hello World</h1>"
    });

    google.maps.event.addListener(marker, "click", function () {

         infoWindow.open(mapObject, marker);

    });

编辑 - 在您的情况下,您正在绘制一个数组/标记列表,每个标记都有自己的信息窗口 - 因此您可以将标记的绘图(结果)代码更改为以下内容:

注意:编译器未使用,可能包含语法错误

// keep reference of plotted markers, so we can clear them if required
var markers = [];

for (var i = 0; i < results.length; i++) {

    var place = results[i];

    // create marker    
    var marker = new google.maps.Marker({
      map: mapObject,
      position: place.geometry.location,
      title: place.name
    });

    // create info window
    var infoWindow = new google.maps.InfoWindow({
      content: ''
    });

    //adding an extra property to marker, (infoWindow - so we can get it inside click event of marker
    marker.infoWindow = infoWindow;

    // click event handler
    google.maps.event.addListener(marker, "click", function() {

        // this == marker 
        var map = this.infoWindow.getMap();

        if (map !== null && typeof map !== "undefined")
            this.infoWindow.close();
        else {
            // open info window at marker position
            this.infoWindow.open(mapObject, this);
        }
    });
    markers.push(marker);
}