javascriptangularjsgoogle-mapsng-map

Angularjs and Google maps marker cluster


I'm new with Google Maps and I add ng-map but I have the same problem of official plunker code plunker, markers don't appear. This is my Angularjs code:

//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, {});
    });
});

In this case I have only 1 city, but in the future I may have a loto of coordinates. In Html I have this script about 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>

I tryed to remove

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

but the result still the same. I would like to uso only maps CDN link.


Solution

  • I found out that vm.markerClusterer = new MarkerClusterer(map, vm.dynMarkers, {}); is called before for inside ajax is terminated so the vatiable is empty so I moved it after the end of for and it works

    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, {});