Gmap:如何在地球上绘制正方形(lat,lon)

时间:2012-07-29 17:47:36

标签: javascript google-maps geolocation

我试图画一个正方形

rectangle = new google.maps.Rectangle();
var rectOptions = {
    strokeColor : "#FF0000",
    strokeOpacity : 0.8,
    strokeWeight : 2,
    fillOpacity : 0,
    map : map
};
rectangle.setOptions(rectOptions);

长度为0.005'

google.maps.event.addListener(map, 'dragend', function() {

    var center = map.getCenter();
    rectangle.setBounds(new google.maps.LatLngBounds(
            new google.maps.LatLng(center.lat()-0.005, center.lng()-0.005),
            new google.maps.LatLng(center.lat()+0.005, center.lng()+0.005)));
});

但我意识到这个正方形是赤道附近的正方形,但在其他地方变形很大, 是否有一个随纬度减小的函数来获得经度的等效长度? thx的帮助

编辑:square的高度= cos(纬度)宽度

1 个答案:

答案 0 :(得分:0)

来自Andrew Leach的评论:

function drawSquare(position=new google.maps.LatLng(41, 36.12), radius=0.001){

    var rectangle = new google.maps.Rectangle();
    var rectOptions = {
        strokeColor : "#FF0000",
        strokeOpacity : 0.8,
        strokeWeight : 2,
        fillOpacity : 0
    };
    rectangle.setOptions(rectOptions);

    var c = Math.cos(position.lat()* Math.PI / 180);
    rectangle.setBounds(new google.maps.LatLngBounds(
            new google.maps.LatLng(position.lat()-c*radius/2, position.lng()-radius/2),
            new google.maps.LatLng(position.lat()+c*radius/2, position.lng()+radius/2)));
    return rectangle;
}