4

我正在从使用谷歌地图切换到leaflet.js。我在谷歌地图中做的一件事似乎在leaflet.js 中找不到,那就是计算从地图中心(即搜索位置)到地图两侧的半径。由于您可以放大和缩小人们正在查看的区域,因此可能会发生显着变化。下面的代码显示了我为了使用谷歌地图而拥有的几行代码。有人可以指出关于leaflet.js 的正确方向吗?

  // viewport stores the recommended viewport for the returned result.
  // (LatLngBounds)
  viewportLatLngBounds = zip.get("location").geometry.viewport;

  this.map.fitBounds(viewportLatLngBounds);

  this.collection.latitude = viewportLatLngBounds.getCenter().lat();
  this.collection.longitude = viewportLatLngBounds.getCenter().lng();

  // calculate radius
  // get distance..
  // from (lat of NE corner), (lng of center)
  // to   (lat of center), (lng of center)
  topCenterLatLng = new google.maps.LatLng(viewportLatLngBounds.getNorthEast().lat(), viewportLatLngBounds.getCenter().lng());

  metersRadius = google.maps.geometry.spherical.computeDistanceBetween(viewportLatLngBounds.getCenter(), topCenterLatLng);

  this.collection.radius = metersRadius / 1000;
  this.collection.radiusUnits = "km";
4

1 回答 1

16

备查:

getMapRadiusKM: function() {
    var mapBoundNorthEast = map.getBounds().getNorthEast();
    var mapDistance = mapBoundNorthEast.distanceTo(map.getCenter());
    return mapDistance/1000;
},
于 2013-01-03T19:25:35.893 回答