【发布时间】:2013-05-29 13:25:43
【问题描述】:
我已经用谷歌搜索试图找到答案,但什么也没有。
我正在创建一个网站,用户可以在其中搜索并使用谷歌地理编码 api 选择一个地方,然后我得到那个地方的界限,但是用户应该能够增加这些界限。
例如,用户可以在 5 公里内增加与该地点的距离,所以我可以在 5 公里内增加边界。
这是用户搜索某个地方时的结果:
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.15420480,
"lng" : -6.19020910
},
"southwest" : {
"lat" : 32.403740,
"lng" : -31.2751580
}
},
现在我需要增加 5 公里的界限。 考虑到我必须将 km 转换为度数,我该如何在 javascript 中做到这一点?
非常感谢。
编辑
感谢@geocodezip,我尝试了这个,但有些东西不是qorking。我按照here 的说法在“目标点给定距离和距起点的方位”进行尝试,但有些东西无法正常工作。这是代码:
//convert decimal to degree
function getDD2DMS(dms, type){
var sign = 1, Abs=0;
var days, minutes, secounds, direction;
if(dms < 0) { sign = -1; }
Abs = Math.abs( Math.round(dms * 1000000.));
//Math.round is used to eliminate the small error caused by rounding in the computer:
//e.g. 0.2 is not the same as 0.20000000000284
//Error checks
if(type == 'lat' && Abs > (90 * 1000000)){
//alert(' Degrees Latitude must be in the range of -90. to 90. ');
return false;
} else if(type == 'lng' && Abs > (180 * 1000000)){
//alert(' Degrees Longitude must be in the range of -180 to 180. ');
return false;
}
days = Math.floor(Abs / 1000000);
minutes = Math.floor(((Abs/1000000) - days) * 60);
secounds = ( Math.floor((( ((Abs/1000000) - days) * 60) - minutes) * 100000) *60/100000 ).toFixed();
days = days * sign;
if(type == 'lat') direction = days<0 ? 'S' : 'N';
if(type == 'lng') direction = days<0 ? 'W' : 'E';
//else return value
return (days * sign) + 'º ' + minutes + "' " + secounds + "'' " + direction;
}
//calculate
var R = 6371; // km
var d = 10; //km
var brng = 0; //0 vertical, 90 horizontal
var lat1 = (42.15420480).toRad();
var lon1 = (-6.19020910).toRad();
var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
console.log('lat2 = ' + getDD2DMS(lat2, 'lat'));
console.log('lon2 = ' + getDD2DMS(lon2, 'lng'));
结果与链接不一样。 帮我。非常感谢。
【问题讨论】:
-
告诉我这个问题出了什么问题,而不是消极地评估这个问题不是更好吗?谢谢。
-
@duncan 谢谢,但这个想法不是将度数转换为米,而是相反,将公里或米转换为度,或者至少知道多少度等于 1 公里(或米) .
-
@geocodezip 谢谢。我看到了你发布的链接,但对我来说太多了!!大声笑有没有一个例子,一个在javascript中使用的公式?谢谢。
标签: javascript google-maps google-maps-api-3