【问题标题】:Marker Info-window not working using ngMap标记信息窗口无法使用 ngMap
【发布时间】:2018-04-30 15:06:40
【问题描述】:

我正在尝试使用 在谷歌地图上显示信息窗口。这是我的代码:

<div id="map">
  <ng-map zoom="13" center="[{{latitude}}, {{longitude}}]" style="display: block; height: 100%;">
    <marker ng-repeat="pothole in potholeData" position="{{pothole.lat}},{{pothole.lng}}" on-click="showDetails(e, pothole)"></marker>
  </ng-map>
</div>

我在每个标记上给出了一个 on-click 函数,并带有一个 lat 和 long 值。但在我的控制器中,我得到了undefined 值。

这是我的控制器:

$scope.showDetails = function(e, pothole) {
  var infowindow = new google.maps.InfoWindow();
  var center = new google.maps.LatLng(pothole.lat, pothole.lng);

  infowindow.setContent(
    '<h3>' + pothole + '</h3>');

  infowindow.setPosition(center);
  infowindow.open($scope.map);
}

showDetails 函数中,我得到pothole undefined。 有人可以帮帮我吗?

【问题讨论】:

    标签: ng-map javascript angularjs angularjs-ng-repeat google-maps-markers ng-map


    【解决方案1】:

    问题出在on-click="showDetails(e, pothole),改成on-click="showDetails(event, pothole)。 我已经制作了 sn-p 工作。

    angular.module('mapApp', ['ngMap'])
        .controller('mapController', function($scope, NgMap) {
    
            NgMap.getMap().then(function(map) {
                $scope.map = map;
            });
            $scope.potholeData = [
                { lat: 59.923043, lng: 10.752839 },
                { lat: 59.339025, lng: 18.065818 },
                { lat: 55.675507, lng: 12.574227 },
                { lat: 52.521248, lng: 13.399038 },
                { lat: 48.856127, lng: 2.346525 }
            ];
            $scope.showDetails = function(e, pothole) {
              console.log(pothole);
              var infowindow = new google.maps.InfoWindow();
              var center = new google.maps.LatLng(pothole.lat,pothole.lng);
    
              infowindow.setContent(
                '<h3>' + pothole.lat + " " + pothole.lng + '</h3>');
    
              infowindow.setPosition(center);
              infowindow.open($scope.map);
            };
    
        });
    <script src="https://maps.google.com/maps/api/js"></script>
    <script src="https://code.angularjs.org/1.3.15/angular.js"></script>
    <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
    <div ng-app="mapApp" ng-controller="mapController">
        <ng-map default-style="true" zoom="5" center="59.339025, 18.065818">
            <marker ng-repeat="pothole in potholeData"
                    position="{{pothole.lat}},{{pothole.lng}}" on-click="showDetails(event, pothole)">
            </marker>
        </ng-map>
    </div>

    【讨论】:

    • 嘿,我有完全相同的代码。我已经包含了所有脚本。我不是我出错的地方
    • @chan 您需要将on-click="showDetails(e, pothole) 更改为on-click="showDetails(event, pothole)。请参阅event 参数。这才是真正的问题。
    • @artgb 感谢您的回答。我只有一个问题。如何向该信息窗口添加样式?
    【解决方案2】:

    根据@artgb 的回答,您需要将on-click="showDetails(e, pothole) 更改为on-click="showDetails(event, pothole)。请参阅event 参数。这才是真正的问题。

    在这个完整的version 中工作方式相同。

    根据https://ngmap.github.io/.

    1. 您需要这些脚本:

      1. &lt;script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCGsmJhDsK9HS2_kYtQXbiJqaAE2AT_Pw0"&gt;&lt;/script&gt;AIzaSyCGsmJhDsK9HS2_kYtQXbiJqaAE2AT_Pw0 是我用于此演示的 API KEY。您可以通过以下方式获取自己的信息:https://developers.google.com/maps/documentation/javascript/
      2. &lt;script src="https://code.angularjs.org/1.3.15/angular.js"&gt;&lt;/script&gt;
      3. &lt;script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"&gt;&lt;/script&gt;
    2. 您需要使用 id 来识别您的标记。当您使用ngRepeat时,我正在使用这些数据:


    [{
      "id": "Location1",
      "text": "Some content 1...",
      "lat": -12.339223,
      "lng": -76.808624
    }, {
      "id": "Location2",
      "text": "Some content 2...",
      "lat": -12.349423,
      "lng": -76.828824
    }]
    

    1. 指令包含向用户提供的原始 Google Maps V3 api,因此您无需使用 new google.maps 对象。

    类似这样的:

    (function() {
    
      angular.module("app", ["ngMap"]).controller("Controller", ["$scope", "NgMap", function($scope, NgMap) {
        NgMap.getMap().then(function(map) {
          $scope.map = map;
        });
    
        $scope.latitude = -12.339223;
        $scope.longitude = -76.808624;
        $scope.potholeData = [{
          "id": "Location1",
          "text": "Some content 1...",
          "lat": -12.339223,
          "lng": -76.808624
        }, {
          "id": "Location2",
          "text": "Some content 2...",
          "lat": -12.349423,
          "lng": -76.828824
        }];
        $scope.pothole = {};
    
        $scope.showDetails = function(e, pothole) {
          $scope.pothole = pothole;
          $scope.map.showInfoWindow('foo-iw', pothole.id);
        };
    
        $scope.hideDetail = function() {
          $scope.map.hideInfoWindow('foo-iw');
        };
      }]);
    })();
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCGsmJhDsK9HS2_kYtQXbiJqaAE2AT_Pw0"></script>
    <script src="https://code.angularjs.org/1.3.15/angular.js"></script>
    <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
    
    <div data-ng-app="app">
      <div data-ng-controller="Controller">
        <ng-map default-style="true" center="{{latitude}}, {{longitude}}" zoom="13">
          <marker id="{{pothole.id}}" ng-repeat="pothole in potholeData" position="{{pothole.lat}},{{pothole.lng}}" on-click="showDetails(event, pothole)"></marker>
    
          <info-window id="foo-iw">
            <div ng-non-bindable="">
              id: {{pothole.id}}<br/> Text: {{pothole.text}}<br/> Position: {{anchor.getPosition()}}
            </div>
          </info-window>
        </ng-map>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2016-07-27
      • 2013-09-02
      • 2013-06-26
      相关资源
      最近更新 更多