NgMap模块和MarkerClusterer函数

时间:2016-08-19 09:58:29

标签: javascript angularjs api google-maps

我有来自外部API的数据,我想在我的GoogleMap中对标记进行分组。 我使用的是NgMap指令:

ng-map center="[21.18, 27.71]" style='margin: 10px 0; height: 500px;' zoom='6'
        marker position="{{item.latitude}}, {{item.longitude}}" ng-repeat="item in items"

那么如何在AngularJS(1.5x)控制器中设置MarkerClusterer。 https://jsfiddle.net/7nqg7f7f/6/ - >我是这样做的。

1 个答案:

答案 0 :(得分:0)

ng-map库涵盖Google Maps API,相反,MarkerClusterer是Google Maps JavaScript API v3的单独库,ng-map不支持(至少在目前)

以下示例演示了如何将MarkerClustererng-map一起使用:

angular.module('mapApp', ['ngMap'])
    .controller('mapController', function ($scope, NgMap) {

        NgMap.getMap().then(function (map) {
            $scope.map = map;
            $scope.initMarkerClusterer();
        });

        $scope.cities = [
            { id: 1, name: 'Oslo', pos: [59.923043, 10.752839] },
            { id: 2, name: 'Stockholm', pos: [59.339025, 18.065818] },
            { id: 3, name: 'Copenhagen', pos: [55.675507, 12.574227] },
            { id: 4, name: 'Berlin', pos: [52.521248, 13.399038] },
            { id: 5, name: 'Paris', pos: [48.856127, 2.346525] }
        ];
        


        $scope.initMarkerClusterer = function () {
            var markers = $scope.cities.map(function (city) {
                return $scope.createMarkerForCity(city);
            });
            var mcOptions = { imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m' };
            return new MarkerClusterer($scope.map, markers, mcOptions);
        };


        $scope.createMarkerForCity = function (city) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(city.pos[0], city.pos[1]),
                title: city.name
            });
            google.maps.event.addListener(marker, 'click', function () {
                $scope.selectedCity = city;
                $scope.map.showInfoWindow('myInfoWindow', this);
            });
            return marker;
        }

    });
  <script src="https://code.angularjs.org/1.4.8/angular.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
    <script src="https://googlemaps.github.io/js-marker-clusterer/src/markerclusterer.js"></script>
<div ng-app="mapApp" ng-controller="mapController">
        <ng-map default-style="true" zoom="3" center="59.339025, 18.065818">
            <info-window id="myInfoWindow">
                <div ng-non-bindable>
                    <h4>{{selectedCity.name}}</h4>
                </div>
            </info-window>
            <!--marker ng-repeat="c in cities" position="{{c.pos}}" title="{{c.name}}" id="{{c.id}}" on-click="showCity(event, c)">
            </marker-->
        </ng-map>
    </div>

Modified fiddle