2017-12-20 4 views
-2

내 쿼리를 작동하지 않습니다와 하버 사인 공식은 다음과 같습니다구글지도 - PHP는 하버 사인 공식에 대한

$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 

이것

를 사용하지만, 동일한 결과를 반환한다. 두 개의 관련 열 latlng는 추측 - -

답변

0

나는 내가 여기 당신을 위해 하나를 편집하려고 ~ 잘 작동하는 것 같다 등 가까운 마커를 찾기위한 Stored Procedures의 몇 가지있다 아마도 그것은 도움이 될 것입니다

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 

는 MySQL의에서 프로 시저를 호출, PHP

$lat=-33.74788; 
$lng=151.23526; 
$radius=10; 

$sql="call `spNearestMarkers`('$lat','$lng','$radius');`"; 

의 프로 시저를 호출하거나 (GUI)

위의 반환 :

"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", 

데이터베이스 가정은 열을 latitudelongitude을 가지고 있으며, 당신은 PHP에서 나는 워드 프레스 포스트 메타 테이블을 사용하고

/* 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;"; 
+0

을 SQL 쿼리를 실행하려고 , 어디 lat, meta_key로 긴 저장합니다. 이 쿼리를 사용하여 데이터를 가져올 수있는 방법을 제안 해주십시오. – user3178816

+0

원래의 sql 쿼리는 clumns가'lat'와'long'보다는'lat'와'lng'라고 제안합니다. 나는 중요하지 않아야 할 Wordpress ut에 대해 안다. 아마도 데이터베이스에 저장 프로 시저를 만들 수있을 것인가? 그렇다면 위와 같이 작성하여 실행하십시오 (올바르지 않은 경우 열 이름 편집). – RamRaider

+0

제 질문은이 쿼리를 사용하지 않는 이유와 빈 결과 집합이 반환 된 이유입니다. – user3178816