谷歌地图 - 使用PHP无法运行的Haversine公式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌地图 - 使用PHP无法运行的Haversine公式相关的知识,希望对你有一定的参考价值。
我对hasrsine公式的查询是:
$query = "SELECT id,
( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
FROM markers
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20;";
使用此公式时,它返回一个空结果集。未找到结果。让我知道正确的解决方案。
另外,使用
select * FROM (
SELECT *, ( 3959 * acos( cos( radians(37) ) * cos( radians( -33.898113 ) ) * cos( radians( 151.174469 ) - radians(-122) ) + sin( radians(37) ) * sin( radians( -33.898113 ) ) ) ) AS Distance FROM markers
) as T
WHERE T.Distance < 25
ORDER BY T.Distance
LIMIT 0 , 20
这一个,但返回相同的结果。
答案
我有几个Stored Procedures
找到最近的标记等似乎运作良好〜我尝试编辑一个为您的目的 - 猜测两个相关的列是lat
和lng
- 也许它会有所帮助
CREATE DEFINER=`root`@`localhost` PROCEDURE `spNearestMarkers`(
IN `p_lat` DOUBLE,
IN `p_lng` DOUBLE,
IN `p_rad` INT
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
proc:begin
declare latitude double default 0;
declare longitude double default 0;
declare radius float default 0;
declare earth_radius integer default 0;
declare longitude_1 float;
declare latitude_1 float;
declare longitude_2 float;
declare latitude_2 float;
set @latitude=p_lat;
set @longitude=p_lng;
set @radius=p_rad;
set @earth_radius=3956;
set @longitude_1 = @longitude - @radius/abs( cos( radians(@latitude) ) * 69 );
set @longitude_2 = @longitude + @radius/abs( cos( radians(@latitude) ) * 69 );
set @latitude_1 = @latitude - (@radius/69);
set @latitude_2 = @latitude + (@radius/69);
select distinct
*,
round( @earth_radius * 2 * asin( sqrt( power( sin( ( @latitude - m.`lat` ) * pi()/180 / 2 ), 2 ) + cos( @latitude * pi()/180 ) * cos( m.`lat` * pi()/180) *power( sin( ( @longitude - m.`lng`) * pi()/180 / 2 ), 2) ) ) , 2 ) as 'distance'
from `markers` m
where
( m.`lat` between @latitude_1 and @latitude_2 )
and
( m.`lng` between @longitude_1 and @longitude_2 )
group by m.`name`
having `distance` <= @radius
order by `distance`;
set @latitude=null;
set @longitude=null;
set @radius=null;
set @longitude_1 = null;
set @longitude_2 = null;
set @latitude1 = null;
set @latitude2 = null;
end
在php中调用该过程
$lat=-33.74788;
$lng=151.23526;
$radius=10;
$sql="call `spNearestMarkers`('$lat','$lng','$radius');`";
或者,在mysql(gui)中调用该过程
set @_lat=-33.74788;
set @_lng=151.23526;
set @_radius=10;
call `spNearestMarkers`(@_lat,@_lng,@_radius);
以上回报:
"id","name","address","lat","lng","distance",
"1","Heir Apparel","Crowea Pl, Frenchs Forest NSW 2086","-33.737885","151.235260","0.69",
"4","The Legacy","Charlotte Ln, Chatswood NSW 2067","-33.796669","151.183609","4.49",
"6","Trish & Tash","Lincoln St, Lane Cove West NSW 2066","-33.812222","151.143707","6.88",
"10","Moda Couture","Northcote Rd, Glebe NSW 2037","-33.873882","151.177460","9.31",
假设你的数据库有latitude
和longitude
列,你试图从PHP调用sql查询然后
/* the lat/lng of the location you are trying to find places nearby */
$lat=56.25;
$lng=-2.65;
$sql="SELECT id, ( 3959 * acos( cos( radians( $lat ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( latitude ) ) ) ) AS distance
FROM markers
HAVING distance < 25
ORDER BY distance
LIMIT 0,20;";
以上是关于谷歌地图 - 使用PHP无法运行的Haversine公式的主要内容,如果未能解决你的问题,请参考以下文章
尽管有良好的API密钥,Google地图仍无法在我的网站上运行[关闭]