如何计算纬度和经度指定的两点之间的距离?
为了澄清,我想要以公里为单位的距离; 这些要点使用WGS84系统,我想了解可用方法的相对准确性.
嗨,我需要计算具有lat和long的两个点之间的距离.
我想避免任何外部API调用.
我尝试在PHP中实现Haversine公式:
这是代码:
class CoordDistance
{
public $lat_a = 0;
public $lon_a = 0;
public $lat_b = 0;
public $lon_b = 0;
public $measure_unit = 'kilometers';
public $measure_state = false;
public $measure = 0;
public $error = '';
public function DistAB()
{
$delta_lat = $this->lat_b - $this->lat_a ;
$delta_lon = $this->lon_b - $this->lon_a ;
$earth_radius = 6372.795477598;
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = …
Run Code Online (Sandbox Code Playgroud) 我想知道如何获得2个GPS点之间的距离和方位.我研究了半胱氨酸配方.有人告诉我,我也可以使用相同的数据找到轴承.
一切都运转良好,但轴承还没有正常工作.轴承输出负值但应在0 - 360度之间.设定数据应该是水平方位,96.02166666666666
并且是:
Start point: 53.32055555555556 , -1.7297222222222221
Bearing: 96.02166666666666
Distance: 2 km
Destination point: 53.31861111111111, -1.6997222222222223
Final bearing: 96.04555555555555
Run Code Online (Sandbox Code Playgroud)
这是我的新代码:
from math import *
Aaltitude = 2000
Oppsite = 20000
lat1 = 53.32055555555556
lat2 = 53.31861111111111
lon1 = -1.7297222222222221
lon2 = -1.6997222222222223
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * …
Run Code Online (Sandbox Code Playgroud) Pandas数据帧中的每一行包含2个点的lat/lng坐标.使用下面的Python代码,计算许多(数百万)行的这两个点之间的距离需要很长时间!
考虑到2点相距不到50英里并且准确性不是很重要,是否可以更快地进行计算?
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Haversine距离公式(如此处所示:http://www.movable-type.co.uk/scripts/latlong.html)但我无法使其工作,请参阅以下代码
function test() {
var lat2 = 42.741;
var lon2 = -71.3161;
var lat1 = 42.806911;
var lon1 = -71.290611;
var R = 6371; // km
//has a problem with the .toRad() method below.
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alert(d);
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Uncaught TypeError: Object -0.06591099999999983 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为CLLocation编写一个类别,以将轴承返回到另一个CLLocation.
我相信我对公式做错了(计算不是我的强项).返回的轴承始终关闭.
我一直在看这个问题并尝试应用被接受为正确答案的更改及其引用的网页:
计算两个CLLocationCoordinate2D之间的方位
http://www.movable-type.co.uk/scripts/latlong.html
谢谢你的任何指示.我已经尝试将其他问题的反馈结合起来,而我仍然没有得到任何东西.
谢谢
这是我的类别 -
----- CLLocation + Bearing.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface CLLocation (Bearing)
-(double) bearingToLocation:(CLLocation *) destinationLocation;
-(NSString *) compassOrdinalToLocation:(CLLocation *) nwEndPoint;
@end
Run Code Online (Sandbox Code Playgroud)
--------- CLLocation + Bearing.m
#import "CLLocation+Bearing.h"
double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};
@implementation CLLocation (Bearing)
-(double) bearingToLocation:(CLLocation *) destinationLocation {
double lat1 = DegreesToRadians(self.coordinate.latitude);
double lon1 = DegreesToRadians(self.coordinate.longitude);
double lat2 = DegreesToRadians(destinationLocation.coordinate.latitude);
double lon2 = DegreesToRadians(destinationLocation.coordinate.longitude);
double dLon = lon2 …
Run Code Online (Sandbox Code Playgroud) 我想用php使用这个公式.我有一个数据库,其中保存了一些latitute和经度值.
我想在输入中找到一定的纬度和经度值,从这一点到数据库中的每个点的所有距离(以km为单位).为此,我在googlemaps api上使用了公式:
( 6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )
Run Code Online (Sandbox Code Playgroud)
当然在php中使用它我用deg2rad
.替换弧度.值37,-122是我输入和lat的值,lng是我在数据库中的值.
下面是我的代码.问题是有什么不对,但我不明白.距离的价值当然是错误的.
//values of latitude and longitute in input (Rome - eur, IT)
$center_lat = "41.8350";
$center_lng = "12.470";
//connection to database. it works
(..)
//to take each value in the database:
$query = "SELECT * FROM Dati";
$result = mysql_query($query);
while …
Run Code Online (Sandbox Code Playgroud) 我正在使用OpenLayers和一个普通的墨卡托地图,我试图通过在latlong中找到一个点网格来对边界框进行采样.bbox以latlon表示,例如
48.1388,-15.3616,55.2057,-3.9359
Run Code Online (Sandbox Code Playgroud)
我可以用度数定义距离(例如x:2.5,y:2.4)并从那里计算出点数.但我想用米(例如50000)来表达这个距离,以便将它与用户心态联系起来(人们理解米,而不是度数).我怎样才能转换这个距离?我知道如何重新投射一个点,但不是距离.
谢谢你的任何提示!Mulone
我正在尝试使用Haversine公式来查找具有Pageable的Spring Data JPA Query中某个位置附近的实体,但我没有完成它.
我的第一种方法看起来像这样
@Query("SELECT m, (6371 * acos(cos(radians(:latitude)) * cos(radians(m.latitude)) * cos(radians(m.longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(m.latitude)))) as dist FROM Entity m WHERE dist < :distance ORDER BY dist DESC")
public List<Entity> findEntitiesByLocation(@Param("latitude") final double latitude, @Param("longitude") final double longitude, @Param("distance") final double distance, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
但它失败了,因为Spring/JPA似乎无法在where子句中使用别名.stacktrace中的SQL看起来像这样
select (...),(haversine) as col_1_0_ where dist<? order by col_1_0_ DESC
Run Code Online (Sandbox Code Playgroud)
所以where子句中的别名不会被替换.使用"col_1_0_"(不使用")代替dist也不起作用.
根据这个SO答案,至少MySQL是由内部解释的,并且在Where子句中使用别名是不可能的.建议的解决方案是使用HAVING而不是WHERE,但在HAVING子句中,别名不会被解析.
我知道我可以将Haversine公式移动到where子句中,但我仍然需要在Order By子句中使用它,我认为它可能会在Order By子句中使用相同的长Haversine公式来降低性能,因为我选择了几百个数千个实体.
然后我尝试手动创建查询,但我不知道如何将Pageable应用于此:
@Override
public List<Entity> findEntitiesByLocation(final double latitude, …
Run Code Online (Sandbox Code Playgroud) 我目前有一个MySQL表,结构如下:
id name lon lat
----- ----- ----------- -----------
1 Mark -76.316528 40.036027
2 John -95.995102 41.25716
3 Paul -82.337036 29.645095
4 Dave -82.337036 29.645095
5 Chris -76.316528 40.036027
Run Code Online (Sandbox Code Playgroud)
我当前查询数据库以查看用户的位置是否在给定位置的某个英里半径范围内的方式是这样做.
function Haversine($lat_from, $lon_from, $lat_to, $lon_to) {
$radius = 6371000;
$delta_lat = deg2rad($lat_to-$lat_from);
$delta_lon = deg2rad($lon_to-$lon_from);
$a = sin($delta_lat/2) * sin($delta_lat/2) +
cos(deg2rad($lat_from)) * cos(deg2rad($lat_to)) *
sin($delta_lon/2) * sin($delta_lon/2);
$c = 2*atan2(sqrt($a), sqrt(1-$a));
// Convert the distance from meters to miles
return ceil(($radius*$c)*0.000621371);
}
// Set the given location to NYC …
Run Code Online (Sandbox Code Playgroud) haversine ×10
php ×2
python ×2
algorithm ×1
bearing ×1
cllocation ×1
coordinates ×1
distance ×1
geocoding ×1
geolocation ×1
gis ×1
google-maps ×1
gps ×1
iphone ×1
javascript ×1
jpa ×1
mapkit ×1
maps ×1
math ×1
mysql ×1
numpy ×1
openlayers ×1
pandas ×1
proj4js ×1
spring-data ×1
sql ×1
transform ×1