简体   繁体   English

用2个地理点计算距离

[英]calculate distance with 2 geo points

I want to calculate the distance between 2 geo points, for now between my school and my house. 我想计算两个地理点之间的距离,现在在我的学校和我的房子之间。 The distance should be close 1200 meter. 距离应该接近1200米。

I get these values back which don't make sense. 我得到了这些没有意义的价值观。

104.247784180256
35.017200205306295 (if I reverse lat and lon)

Google maps says: 谷歌地图说:

51.987957 is N
5.911305 is O

is 51.987957 lat or lon? 是51.987957 lat或lon? According to my documentation of where I get the GPS signal from it should be latitude but I have my doubt about that. 根据我的文件,我从哪里得到GPS信号应该是纬度,但我对此有所怀疑。

float R = 6371; // km

// 104.247784180256

float lat1 = 5.894213; // school 
float lon1 = 51.98381; // school

float lat2 = 5.909912; // keuken
float lon2 = 51.988781; // keuken

// switched > distance = 35.017200205306295
/*
float lon1 = 5.894213; // school 
float lat1 = 51.98381; // school

float lon2 = 5.909912; // keuken
float lat2 = 51.988781; // keuken
*/

void setup() {
  double d = calculateDistance( lon1, lat1, lon2, lat2);
  println(d);
}

double calculateDistance(float lon1, float lat1, float lon2, float lat2) {
  double d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
    Math.cos(lat1)*Math.cos(lat2) *
    Math.cos(lon2-lon1)) * R;
  return d;
}

You are using the latitude and longitude the wrong way around. 你正在以错误的方式使用纬度和经度。 You can simply check on google maps if you search for "@51.98381,5.894213", this points to your school. 如果您搜索“ @ 51.98381,5.894213”,则可以简单地在Google地图上查看,这指向您的学校。 And keep in mind it is latitude,longitude. 请记住,它是纬度,经度。

I use the following code to calculate (note i do it in sql, but its about the idea): 我使用以下代码进行计算(请注意,我在sql中执行此操作,但有关想法):

((ACOS(SIN(lat1 * PI() / 180) * SIN(lat2 * PI() / 180) + COS(lat1 * PI() / 180) * COS(lat2 * PI() / 180) * COS((long1 - long2) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344)

This gives back the distance in kilometers. 这以公里为单位返回距离。 Also note that the functions use radians and not degrees. 另请注意,函数使用弧度而不是度。

The result is 1.20877685491371 km, which is the 1200 meters you expect. 其结果是1.20877685491371 km,这是您期望的1200米。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM