角js谷歌地图无法使用谷歌事件监听器的标记

时间:2015-02-06 16:18:24

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

我正在测试角度js谷歌地图http://angular-ui.github.io/angular-google-maps/#!/api

我可以在地图上添加多个标记,但是我无法为每个标记设置事件监听器。我只是希望它在用户点击任何标记时写入控制台。

如何让我的代码有效?

以下是我的代码:

 <!DOCTYPE html>
    <html xmlns:ng="http://angularjs.org/" ng-app="appMaps">

    <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <link rel="stylesheet" href="style.css" />
    <script src="//maps.googleapis.com/maps/api/js?libraries=weather,geometry,visualization,places&sensor=false&language=en&v=3.17"></script>
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.26/angular.js" data-semver="1.2.26"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
    <script src="http://rawgit.com/angular-ui/angular-google-maps/master/dist/angular-google-maps.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!--css-->
    <style type="text/css">
        html, body, #map_canvas {
            height: 100%;
            width: 100%;
            margin: 0px;
        }

        #map_canvas {
            position: relative;
        }

        .angular-google-map-container {
            position: absolute;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
        }
    </style>
    <script>angular.module('appMaps', ['uiGmapgoogle-maps'])
      .controller('mainCtrl', function($scope) {
    $scope.map = {
      center: {
        latitude: 34.963916,
        longitude: 104.311893
      },
      zoom: 4,
      bounds: {},

    };

    $scope.options = {
      scrollwheel: false
    };
    var createRandomMarker = function(i, bounds, idKey) {
      var lat_min = bounds.southwest.latitude,
        lat_range = bounds.northeast.latitude - lat_min,
        lng_min = bounds.southwest.longitude,
        lng_range = bounds.northeast.longitude - lng_min;

      if (idKey == null) {
        idKey = "id";
      }

      var latitude = lat_min + (Math.random() * lat_range);
      var longitude = lng_min + (Math.random() * lng_range);
      var ret = {
        latitude: latitude,
        longitude: longitude,
        title: 'm' + i
      };
      ret[idKey] = i;
      return ret;
    };
    $scope.randomMarkers = [];
    // Get the bounds from the map once it's loaded
    $scope.$watch(function() {
      return $scope.map.bounds;
    }, function(nv, ov) {
      // Only need to regenerate once
      if (!ov.southwest && nv.southwest) {
        var markers = [];
        for (var i = 0; i < 2; i++) {   
             var ret = {
            latitude:  34.963916,
            longitude: 104.311893,
            title: 'm3',
            id: 1
            };

            var ret2 = {
            latitude:  37.096002,
            longitude: 126.987675,
            title: 'm2',
            id:2
            };
           markers.push(ret);
           markers.push(ret2);

        }
        $scope.randomMarkers = markers;
      }
    }, true);

    $scope.marker = {
        events:{click: console.log('click')},
    }

     });


  </script>
</head>

<body>
    <div id="map_canvas" ng-controller="mainCtrl">
    <ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="options" bounds="map.bounds" events = "'map.events'">
        <ui-gmap-markers models="randomMarkers" coords="'self'" icon="'icon'" click="'test'" events = "'events'"></ui-gmap-markers>
    </ui-gmap-google-map>

</div>
<!--example-->
</body>

</html>

2 个答案:

答案 0 :(得分:0)

我今天晚些时候会进一步研究这个问题。我现在想到的一种方法是将click中的<ui-gmap-markers>参数设置为调用console.log()的有效JS函数。

就我而言,我做了click="onClick"然后定义了以下函数:

$scope.onClick = function onClick() {
console.log("click");}

答案 1 :(得分:0)

点击谷歌地图上的活动

我不知道有角度的地图api但我有一些建议你可以使用地图api与帆布你可以在画布元素上给ng-click事件
代码在这里
网站:
jsfiddle.net/xSPAA/536/