gpt4 book ai didi

javascript - Gmap : how to draw squares on Earth (lat, lon)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:46:27 36 4
gpt4 key购买 nike

我想画一个正方形

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)));
});

但我发现这个正方形是一个靠近赤道的正方形,但在其他地方却很变形,是否有随纬度减小的函数以获得经度的等效长度?感谢您的帮助

编辑:正方形的高度 = cos(lat)width

最佳答案

来自 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;
}

关于javascript - Gmap : how to draw squares on Earth (lat, lon),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11711524/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com