leaflet.js计算从中心到边界的半径

时间:2012-12-13 08:15:03

标签: geocoding leaflet

我正在使用谷歌地图切换到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";

1 个答案:

答案 0 :(得分:9)

供将来参考:

getMapRadiusKM: function() {
    var mapBoundNorthEast = map.getBounds().getNorthEast();
    var mapDistance = mapBoundNorthEast.distanceTo(map.getCenter());
    return mapDistance/1000;
},