是否可以计算角度 - 谷图中两个标记之间的距离?

Dis*_*Hay 0 javascript angularjs angular-google-maps

我想说明的中心之间的距离marker,以每另一个markers在它自己的window.有没有办法计算距离angular-google-maps

以下是此处的代码.

map.html

<div id="map_canvas" ng-controller="mainCtrl">
    <ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="options" bounds="map.bounds">
        <ui-gmap-markers models="randomMarkers" coords="'self'" icon="'icon'">
        </ui-gmap-markers>
    </ui-gmap-google-map>
</div>
Run Code Online (Sandbox Code Playgroud)

controller.js

angular.module('appMaps', ['uiGmapgoogle-maps'])
  .controller('mainCtrl', function($scope) {
    $scope.map = {
      center: {
        latitude: 40.1451,
        longitude: -99.6680
      },
      zoom: 4,
      bounds: {}
    };
    $scope.options = {
      scrollwheel: false
    };
    var createRandomMarker = function(i, bounds, idKey) {
      var lat_min = bounds.southwest.latitude,
        lat_range = bounds.northeast.latitude - lat_min,
        lng_min = bounds.southwest.longitude,
        lng_range = bounds.northeast.longitude - lng_min;

      if (idKey == null) {
        idKey = "id";
      }

      var latitude = lat_min + (Math.random() * lat_range);
      var longitude = lng_min + (Math.random() * lng_range);
      var ret = {
        latitude: latitude,
        longitude: longitude,
        title: 'm' + i
      };
      ret[idKey] = i;
      return ret;
    };
    $scope.randomMarkers = [];
    // Get the bounds from the map once it's loaded
    $scope.$watch(function() {
      return $scope.map.bounds;
    }, function(nv, ov) {
      // Only need to regenerate once
      if (!ov.southwest && nv.southwest) {
        var markers = [];
        for (var i = 0; i < 2; i++) {
          markers.push(createRandomMarker(i, $scope.map.bounds))
        }
        $scope.randomMarkers = markers;
      }
    }, true);
  });
Run Code Online (Sandbox Code Playgroud)

小智 5

为什么在功能已经在谷歌地图中时添加额外的代码?只需包含几何库和:

google.maps.geometry.spherical.computeDistanceBetween(latLng, latLng)
Run Code Online (Sandbox Code Playgroud)

文档:https://developers.google.com/maps/documentation/javascript/geometry?hl = zh- CN