calculate distance with 2 geo points

微笑、不失礼 提交于 2019-12-10 11:47:31

问题


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.

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? According to my documentation of where I get the GPS signal from it should be latitude but I have my doubt about that.

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

回答1:


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. 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):

((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.



来源:https://stackoverflow.com/questions/8588095/calculate-distance-with-2-geo-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!