Angularjs和Google地图标记群集

时间:2017-07-10 07:59:53

标签: javascript angularjs google-maps ng-map

我是谷歌地图的新用户,我添加了ng-map,但我遇到了与官方编码plunker相同的问题,标记没有出现。 这是我的Angularjs代码:

//Google maps
var app = angular.module('myApp',['ngMap', 'ngAnimate', 'ui.bootstrap', 'ui.select']);
app.controller('mapController', ['$scope','$http', 'NgMap', function($scope, $http, NgMap)  {
    NgMap.getMap().then(function(map) {
        //Get all buildings for maps
        $http({
            method: 'GET',
            url: '/booking/building/1',
        }).then(function successCallback(response) {
            if (typeof response.data.success == 'undefined'){
                window.location.href = "/500";
            }else if (response.data.success==true){
                for (var i=0; i < response.data.result.length;i++){
                    var latLng = new google.maps.LatLng(response.data.result[i].latitude, response.data.result[i].longitude);
                    vm.dynMarkers.push(new google.maps.Marker({position:latLng, title:response.data.result[i].name}));
                }
            }else if (response.data.success==false)
                notifyMessage(response.data.result, 'error');
        }, function errorCallback(response) {
            window.location.href = "/500";
        });
        vm.markerClusterer = new MarkerClusterer(map, vm.dynMarkers, {});
    });
});

在这种情况下,我只有一个城市,但将来我可能会有很多坐标。 在Html中,我有关于ng-map的脚本:

<!-- Maps -->
<script th:src="@{/static/assets/plugins/angular-maps/ng-map.min.js}"
    type="text/javascript"></script>
<script
    src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/testapp/scripts/markerclusterer.js"></script>

<script>
    MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ 
      = 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m1.png';  //changed image path 
  </script>
<script async defer
    src="https://maps.googleapis.com/maps/api/js?key=key">
    </script>

我试图删除

<script>MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH = 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m1.png';  //changed image path 
  </script>

但结果仍然相同。我想只是映射CDN链接。

1 个答案:

答案 0 :(得分:1)

我发现在ajax中的vm.markerClusterer = new MarkerClusterer(map, vm.dynMarkers, {});被终止之前调用了for所以vatiable是空的所以我在for结束后移动它并且它可以工作

for (var i=0; i < response.data.result.length;i++){
    var latLng = new google.maps.LatLng(response.data.result[i].latitude, response.data.result[i].longitude);
    vm.dynMarkers.push(new google.maps.Marker({position:latLng, title:response.data.result[i].name}));
}
vm.markerClusterer = new MarkerClusterer(map, vm.dynMarkers, {});