按地点查找商店 [英] Find Stores by Location

查看:164
本文介绍了按地点查找商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含物理地址和地理编码的大约60家商店的列表。我想创建一个简单的界面,用户可以输入一个邮政编码,城市和州,甚至街道地址,并按照输入的地址的顺序排列列表。据我所知,这是一个非常常见的网站功能。



我的计划是使用Google Maps API来查找输入位置的地理编码,并使用毕达哥拉斯定理计算距离每个位置的距离,并根据距离对列表进行排序并返回结果(或者可能是结果集的前5个......)。

这是做到这一点的正确方法吗?有没有更好的方法或内置Google Maps API的功能?由于这是常见的事情,我会想象已经有很多方面进行过尝试和测试,可能有几个正确的答案。



谢谢。

解决方案

毕达哥拉斯定理是不够的。地球的曲线如此。它需要一点欧几里德几何。 公式和一个用于查找地球上两点之间距离的简单实现 - 作为(以PHP为单位):

  //将纬度和经度以度数$ b的形式传递$ b函数getDistance($ lat1,$ long1,$ lat2,$ long2)
{
$ r = 3963.1; //3963.1法定英里; 3443.9海里; 6378 km
$ pi = pi();

//将度数转换为弧度
$ lat1 = $ lat1 *($ pi / 180);
$ lat2 = $ lat2 *($ pi / 180);
$ long1 = $ long1 *($ pi / 180);
$ long2 = $ long2 *($ pi / 180);
$ b $ ret =(cosos(cos($ lat1)* cos($ long1)* cos($ lat2)* cos($ long2)+ cos($ lat1)* sin($ long1)* cos($ lat2)* sin($ long2)+ sin($ lat1)* sin($ lat2))* $ r);
返回$ ret;
}

您可以在代码中加入这个版本。此外,这里还有一个可能的(未经测试的)函数,它是我用于MySQL的另一个函数的衍生产品。

  DELIMITER $$ 

如果存在`FindDist` $$
CREATE FUNCTION`FindDist`(lt1 DOUBLE,lg1 DOUBLE,lt2 DOUBLE,lg2 DOUBLE)RETURNS DOUBLE
DETERMINISTIC
BEGIN
DECLARE dist,eradius DOUBLE;

SET eradius = 3963.1;
SET(ls1)* Cos(lg1)* Cos(lt2)* Cos(lg2)+ Cos(lt1)* Sin(lg1)* Cos(lt2)* Sin(lg2)+ Sin (lt1)* Sin(lt2))* eradius;
RETURN dist;
END $$

DELIMITER;


I have a list of about 60 stores with physical addresses and geocodes. I would like to make a simple interface where a user can enter a zip code, or city and state, or even a street address and have the list sort in order of proximity to the entered address. This is a very common feature of websites as I understand.

My plan to do this is to use the Google Maps API to find the geocode of the entered location and use the Pythagorean Theorem to calculate the distance from each location and sort the list by the distances and return the result (or maybe the top 5 of the result set...).

Is this the correct way to do this? Is there a more optimal method or a function built-in the Google Maps API that will do this? Since this is something so common, I would imagine there has been tried and tested in many ways and there are probably several correct answers. I am just looking for some advice if I am going about this the correct way.

Thank you.

解决方案

The Pythagorean Theorem will not be enough. The curve of the Earth makes this so. It requires a bit of Euclidian geometry. The formula and a simple implementation for finding the distance between two points on Earth - as the bird flies and not actual travel distance - is (in PHP):

// pass the latitudes and longitudes in as degrees
function getDistance($lat1,$long1,$lat2,$long2)
{
    $r = 3963.1; //3963.1 statute miles; 3443.9 nautical miles; 6378 km
    $pi = pi();

    // convert the degrees to radians
    $lat1 = $lat1*($pi/180);
    $lat2 = $lat2*($pi/180);
    $long1 = $long1*($pi/180);
    $long2 = $long2*($pi/180);

    $ret = (acos(cos($lat1)*cos($long1)*cos($lat2)*cos($long2) + cos($lat1)*sin($long1)*cos($lat2)*sin($long2) + sin($lat1)*sin($lat2)) * $r) ;
    return $ret;
}

You could incorporate a version of this in your code. In addition here is a possible (untested) function that is a derivative of another one I have used for MySQL.

DELIMITER $$

DROP FUNCTION IF EXISTS `FindDist` $$
CREATE FUNCTION `FindDist` (lt1 DOUBLE,lg1 DOUBLE,lt2 DOUBLE,lg2 DOUBLE) RETURNS DOUBLE
DETERMINISTIC
BEGIN
    DECLARE dist,eradius DOUBLE;

    SET eradius=3963.1;
    SET dist=Acos(Cos(lt1) * Cos(lg1) * Cos(lt2) * Cos(lg2) + Cos(lt1) * Sin(lg1) * Cos(lt2) * Sin(lg2) + Sin(lt1) * Sin(lt2)) * eradius;
    RETURN dist;
END $$

DELIMITER ;

这篇关于按地点查找商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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