用2个地理点计算距离 [英] calculate distance with 2 geo points

查看:155
本文介绍了用2个地理点计算距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算两个地理位置之间的距离,现在在我的学校和我家之间。距离应该接近1200米。

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)

谷歌地图说:

51.987957 is N
5.911305 is O

是51.987957 lat还是lon?根据我的文件,我从哪里获得GPS信号应该是纬度但我对此有疑问。

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


推荐答案

您正在使用纬度和经度错误的方式。
如果您搜索@ 51.98381,5.894213,您可以直接查看谷歌地图,这指向您的学校。请记住它是纬度,经度。

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.

我使用以下代码进行计算(请注意我在sql中执行此操作,但是关于这个想法):

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.

结果是1.20877685491371 km,这是您期望的1200米。

The result is 1.20877685491371 km, which is the 1200 meters you expect.

这篇关于用2个地理点计算距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆