我的问题有点棘手,如果我的问题根本不清楚,请道歉。
我使用 googleMap v3 API。用户可以在 map 上创建一个标记,如果他点击该标记,我将纬度和经度信息输入到一个信息窗口中(这就完成了,没关系)
尝试做的事情:用户可以给出尺寸(公里/英里),我必须绘制一个给定尺寸的矩形(创建标记应该在区域的中间)。问题来自于只接受纬度和经度的 latlng 或 bounds。所以我尝试转换它。
这是代码的主要部分。
//latitude
// 1°lat = 111,11 km
//for me: 1km = 0.00900 .
//longitude
//1°lat = 111,11 km * cos latitude
// for me : 1 km = ((1/Math.cos(latitude)) /111.11)
var myBounds = new Array();
var longkm = 5;
var largkm = 2;
myBounds[0] = new google.maps.LatLngBounds(
new google.maps.LatLng(positionmarqueur.lat()-(0.009*longkm), positionmarqueur.lng()-(Math.abs(((1/Math.cos(positionmarqueur.lat()-(0.009*longkm))) /111.11))*largkm)),
new google.maps.LatLng(positionmarqueur.lat()+(0.009*longkm), positionmarqueur.lng()+(Math.abs(((1/Math.cos(positionmarqueur.lat()+(0.009*longkm))) /111.11))*largkm))
);
var overlay1 = new google.maps.Rectangle({
map: map,
bounds: myBounds[0],
strokeColor: "green",
strokeWeight: 1
});
var longkm = 10;
var largkm = 10;
myBounds[1] = new google.maps.LatLngBounds(
new google.maps.LatLng(positionmarqueur.lat()-(0.009*longkm), positionmarqueur.lng()-(Math.abs(((1/Math.cos(positionmarqueur.lat()-(0.009*longkm))) /111.11))*largkm)),
new google.maps.LatLng(positionmarqueur.lat()+(0.009*longkm), positionmarqueur.lng()+(Math.abs(((1/Math.cos(positionmarqueur.lat()+(0.009*longkm))) /111.11))*largkm))
);
var overlay2 = new google.maps.Rectangle({
map: map,
bounds: myBounds[1],
strokeColor: "blue",
strokeWeight: 1
});
这应该画一个 5,2 的矩形和一个 10,10 的矩形(好吧,一个 10 平方公里的正方形) (有关信息:var positionmarqueur = latLng; 上面的代码)
如果我看一下结果:第一个矩形看起来超过 50/20 公里。 (我除法时是不是做错了?用 10 个因数?我的数学不是很好,但它看起来是正确的)。
第二个....看起来就像一个200*40公里的长方形....
所以。问题在哪里?是不是公式错了?或者我只是很笨,我错过了什么?
思考。
最佳答案
使用的算法是
var largkm = 5,
longkm = 10;
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(positionmarqueur.lat() - ((largkm/2)/110.54), positionmarqueur.lng() - ((longkm/2)/(Math.cos(positionmarqueur.lat())*111.32))),
new google.maps.LatLng(positionmarqueur.lat() + ((largkm/2)/110.54), positionmarqueur.lng() + ((longkm/2)/(Math.cos(positionmarqueur.lat())*111.32)))
);
(注意:我将矩形大小除以 2,因为我们是从中心定义边界)
演示在 http://jsfiddle.net/gaby/a5QVM/
更新
您应该使用 google 几何库,它使用 google map 使用的任何投影,因此转换将是准确的。 (您需要将 google API 的 URL 更改为 http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false
)
更具体地说,您应该使用 google.maps.geometry.spherical.computeOffset
方法
var rectbounds = new google.maps.LatLngBounds(
spherical.computeOffset(spherical.computeOffset(positionmarqueur, longkm*1000/2, -90),largkm*1000/2,0),
spherical.computeOffset(spherical.computeOffset(positionmarqueur, longkm*1000/2, 90), largkm*1000/2,180)
);
更新演示 http://jsfiddle.net/a5QVM/10/
(*1000
是将距离转换为米,因为这是 computeOffset
方法的预期值)
关于javascript - 将纬度/经度转换为距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23266230/