标记信息窗口无法使用ngMap

时间:2017-11-16 22:51:14

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

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

<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>

我在每个标记上使用lat和long值给出on-click函数。但在我的控制器中,我得到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。 有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

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

在这个完整的版本中,工作方式相同。

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

  1. 您需要以下脚本:

    1. <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCGsmJhDsK9HS2_kYtQXbiJqaAE2AT_Pw0"></script>AIzaSyCGsmJhDsK9HS2_kYtQXbiJqaAE2AT_Pw0是此演示的API密钥。你可以自己进入:https://developers.google.com/maps/documentation/javascript/
    2. <script src="https://code.angularjs.org/1.3.15/angular.js"></script>
    3. <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
  2. 您需要使用ID识别您的标记。当您使用ngRepeat我正在使用此数据时:

  3. [{
      "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对象。
    2. 这样的事情:

      &#13;
      &#13;
      (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');
          };
        }]);
      })();
      &#13;
      <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>
      &#13;
      &#13;
      &#13;

答案 1 :(得分:0)

问题出在on-click="showDetails(e, pothole),更改为on-click="showDetails(event, pothole)。 我已经制作了片段。

&#13;
&#13;
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);
        };

    });
&#13;
<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>
&#13;
&#13;
&#13;