确定是否经度&纬度坐标以英里和公里为半径 [英] Determine if a Longitude & Latitude Co-ordinate is Inside a Radius in Miles and Kilometers

查看:130
本文介绍了确定是否经度&纬度坐标以英里和公里为半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用伪代码或JavaScript,任何人都可以描述确定组成对象数组中的哪些项目的最佳方法:

  {
lat:float,
lng:float
}

在英里或公里范围内的给定半径范围内吗?



我将基于地理位置的查询添加到ForerunnerDB( https://github.com/irrelon/ForerunnerDB ),并希望能够通过搜索产生快速结果。



如果您可以描述可加速对数组的查询的索引策略,则可获得奖励积分。我从头开始编写了ForerunnerDB数据库,因此可以灵活地将答案集成到代码中,但主要关注的是查询性能。



虽然问题与这是ForerunnerDB的一个新功能,它不需要你去阅读那个项目的源代码或者熟悉这个系统,并且一个伪代码或者独立的JS例子将会非常受欢迎!

解决方案

这是一个简单的直接方法,使用 Haversine公式

  //此函数获取两个位置的经度和纬度,并返回它们之间的距离为(公里)
函数calcCrow(coords1,coords2)
{
// var R = 6.371; // km
var R = 6371000;
var dLat = toRad(coords2.lat-coords1.lat);
var dLon = toRad(coords2.lng-coords1.lng);
var lat1 = toRad(coords1.lat);
var lat2 = toRad(coords2.lat);

var a = Math.sin(dLat / 2)* Math.sin(dLat / 2)+
Math.sin(dLon / 2)* Math.sin(dLon / 2) * Math.cos(lat1)* Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d = R * c;
return d;
}

//将数值度数转换为弧度
函数toRad(值)
{
返回值* Math.PI / 180;
}

我相信这段代码可能来自这里:



我看到的唯一优化是添加切线对于经纬度来说,将搜索区域外的结果剪掉。

PS我非常喜欢ForerunnerDB,并且迫不及待地想看看与地理相关的功能。

Using only pseudo-code or JavaScript, can anyone describe the best way to determine which items in array of objects composed of:

{
"lat": float,
"lng": float
}

are within a given radius in either miles or kilometers?

I am adding geo-location-based queries to ForerunnerDB (https://github.com/irrelon/ForerunnerDB) and would like to be able to produce fast results from the search.

Bonus points if you can describe an indexing strategy that will speed up the query over the array. I have written the ForerunnerDB database from the ground up so can be flexible with integrating the answer into the code, but the main concern is query performance.

While the question pertains to a new feature of ForerunnerDB, it does not require that you go and read that project's source or familiarise yourself with that system and a pseudo-code or stand-alone JS example would be very welcome!

解决方案

Here is a simple "direct" approach using Haversine formula:

//This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
function calcCrow(coords1, coords2)
{
  // var R = 6.371; // km
  var R = 6371000;
  var dLat = toRad(coords2.lat-coords1.lat);
  var dLon = toRad(coords2.lng-coords1.lng);
  var lat1 = toRad(coords1.lat);
  var lat2 = toRad(coords2.lat);

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c;
  return d;
}

// Converts numeric degrees to radians
function toRad(Value)
{
    return Value * Math.PI / 180;
}

I believe this code might have come from here: Function to calculate distance between two coordinates shows wrong

The only optimisation I see is adding tangents for both latitude and longitude to cut out the results that are far outside the search region.

P.S. I really like ForerunnerDB and can't wait to see geo-related functionality

这篇关于确定是否经度&纬度坐标以英里和公里为半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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