谷歌地图圆半径适合地图

时间:2014-07-04 13:55:24

标签: javascript google-maps geometry

我面临一个问题,要在地图中拟合圆的大小(半径),考虑到地图缩放可以从1到〜19,你也可以在下面的图片中看到,在我的情况下我有按钮在右上角Create Zone,它显示实际地图中心的圆圈和子菜单中的表格,但不确定的大小应该是动态的,基于地图的缩放。

enter image description here

欢迎任何帮助。

1 个答案:

答案 0 :(得分:2)

我找到了解决方案,它计算了地图视口的宽度/高度,只是疯狂的高度/ 2,这将是圆的理想半径

$("#create_zone_trap").click(function(){
    //Set the circle in the middle of the screen
    circle.setCenter(new google.maps.LatLng(map.getCenter());

    var spherical = google.maps.geometry.spherical, 
    bounds = map.getBounds(), 
    cor1 = bounds.getNorthEast(), 
    cor2 = bounds.getSouthWest(), 
    cor3 = new google.maps.LatLng(cor2.lat(), cor1.lng()), 
    cor4 = new google.maps.LatLng(cor1.lat(), cor2.lng()), 
    height = spherical.computeDistanceBetween(cor1,cor3), 
    width = spherical.computeDistanceBetween( cor1, cor4);

    //Fit the circle radius to the map 
    // 0.9 means 90% of the height
    circle.setRadius((height/2) * 0.9);

});
相关问题