【问题标题】:angularjs: ngmap and cluster markerangularjs:ngmap 和集群标记
【发布时间】:2015-05-14 08:21:53
【问题描述】:

在我的一个客户项目中,我正在使用 ngMap (http://ngmap.github.io/),但我对这个“指令”有疑问:如何使用标记集群和这样的地图:

<div ng-controller="MyCtrl">
      <map center="41,-87" zoom="3">

        <info-window id="foo2">
          <div ng-non-bindable="">
            Lat/Lng: {{this.getPosition()}}<br/>
            <ul>
              <li ng-repeat='item in store.items'>{{item}}</li>
            </ul>
          </div>
        </info-window>

        <marker ng-repeat="(id, store) in stores" id="{{id}}"
          position="{{store.position}}"
          on-click="showStore(event, id)"
        ></marker>
      </map>
    </div>

我在示例页面和代码中进行了搜索,但没有关于如何在我的情况下使用标记集群的文档。

有人使用这个 ngmap 吗?还是我需要更改 google map angularjs 指令?

谢谢。

【问题讨论】:

  • 可以查看this out,或者带有标记簇example的角度图库
  • 嗨,我知道示例页面,但在该页面中没有信息窗口,并且信息窗口(在 ngMap 指令中)是在控制器外部的 内创建的。在该示例中,所有内容都在控制器内部进行。不过谢谢
  • @ZioBudda 有你解决你的问题。我和你有一个非常相似的问题

标签: angularjs google-maps ng-map


【解决方案1】:

MarkerClusterer 是 Google Maps JavaScript API v3 的一个独立 库,下面是一个工作示例,演示如何使用 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>
        </ng-map>
    </div>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
相关资源
最近更新 更多