如何在Google地图上显示圈子?

时间:2018-01-16 08:53:08

标签: javascript angularjs google-maps google-maps-markers

我知道经常会问这个问题,但我在Google地图上显示圈子时遇到问题。

实际上,我在地图上有标记。



import htmlTemplate from './activityDetails.html';

export default {

  template: htmlTemplate,

  require: {
    parent: '^main'
  },

  bindings: {
    activity: '<'
  },

  controller: function controller(MapsService, GeolocationService, NgMap, $log) {
    'ngInject';

    this.$onInit = () => {
      // Load Google Maps API script
      MapsService.loadGoogleApi().then(() => {
        this.loaded = true;
        NgMap.getMap().then((map) => {
          this.map = map;
          $log.info('activityDetails component init');
          this.activity.lat = this.activity.location.coordinates[0];
          this.activity.lng = this.activity.location.coordinates[1];
          // Save each marker in its user object to facilitate hover
          this.createMarkers();
          // Set geolocation notification hook
          this.setGeolocationHook();
        });
      });
    };
  }
};
&#13;
<div class="block-map col m6">
    <ng-map class="map" center="{{$ctrl.activity.lat}}, {{$ctrl.activity.lng}}" zoom="16">
      <marker position="{{$ctrl.activity.lat}}, {{$ctrl.activity.lng}}"></marker>
    </ng-map>
  </div>
&#13;
&#13;
&#13;

我的问题是: 如何用圆圈替换我的标记?

非常感谢!

1 个答案:

答案 0 :(得分:0)

你可以添加&#34; Circles&#34;按照官方API文档中的说明进入您的地图。

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

重要的是这一部分:

var cityCircle = new google.maps.Circle({
  strokeColor: '#FF0000',
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: '#FF0000',
  fillOpacity: 0.35,
  map: map,
  center: citymap[city].center,
  radius: Math.sqrt(citymap[city].population) * 100
});

这很直接。

您需要传递lat / lang对象,而不是citymap[city].center,例如{lat: 41.878, lng: -87.629}

对于半径,您可以传递任何数字值。在地图演示中,他们使用了人口。

对于NG-Map:

<shape name="circle" ng-repeat="circle in vm.circles" no-watcher="true"
  stroke-color="#FF0000"
  stroke-opacity="0.8"
  stroke-weight="2"
  fill-color="#FF0000"
  fill-opacity="0.35"
  center="{{circle.position}}"
  radius="{{circle.radius}}">
</shape>