검색 조건에서 주어진 위치까지의 거리를 기준으로 검색 결과를 필터링하려고합니다. 각 제안은 (독일어) 우편 번호와 그 도시에 속한 자체 위치를 포함합니다. 검색 조건은 동일한 원칙을 포함합니다.
그래서 End에는 검색 자의 위치와 오퍼의 여러 위치가 있습니다.
또한 모든 오퍼는 작동 할 수있는 반지름을 가지고 있습니다.
그러면 검색 자 위치까지의 거리가 더 넓은 모든 오퍼링을 오퍼레이션 반경보다 필터링하려고합니다. 나는이 PHP 코드를 사용했다
:Google GetDistance 기능이 제대로 작동하지 않습니다.
$position = $_GET['position']!='' ? $_GET['position'] : '';
내 URL에서 수색자의 위치를 얻을 수 있습니다.
$Results = $database->SearchResults('Conditions of the database Query');
URL의 정보에서 쿼리를 작성하고 내 데이터베이스를 검색합니다.
while ($row = mysqli_fetch_array($Results)) {
echo $functions->GetDistance($position, $row[1], "K")."<br>";
}
이 while 루프이 검색
function getDistance($addressFrom, $addressTo, $unit){
//Change address format
$formattedAddrFrom = str_replace(' ','+',$addressFrom);
$formattedAddrTo = str_replace(' ','+',$addressTo);
//Send request and receive json data
$geocodeFrom = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false');
$outputFrom = json_decode($geocodeFrom);
$geocodeTo = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false');
$outputTo = json_decode($geocodeTo);
//Get latitude and longitude from geo data
$latitudeFrom = $outputFrom->results[0]->geometry->location->lat;
$longitudeFrom = $outputFrom->results[0]->geometry->location->lng;
$latitudeTo = $outputTo->results[0]->geometry->location->lat;
$longitudeTo = $outputTo->results[0]->geometry->location->lng;
//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) + cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344).' km';
}
else if ($unit == "N") {
return ($miles * 0.8684).' nm';
}
else {
return $miles.' mi';
}
}
의 postition 나에게 각각 제공의 거리를 표시하도록되어 그리고이 기능, 나는 두 개의 주소 사이의 거리를 얻을 수있는이 포럼에서 발견 .
그것은 대부분의 시간 동안 작동하고 나는 적당한 거리를 얻습니다. 예를 들면 : 77.4640983104 km입니다.
그러나 매 2 또는 3 실점, 나는이 오류가 :
Notice: Trying to get property of non-object
하고 "지리 데이터에서 위도와 경도를 가져 오기"섹션 및 출력으로 0km의 잘못된 거리
.Notice: Undefined offset: 0
나는 어떤 실수를하고 있는지 정말로 모르지만, 누군가가 나에게 설명 할 수 있고, 무엇을 바꿔야하는지, 모든 일이 틀린 방식으로 접근되는지를 희망한다.
미리 감사드립니다. 좋은 하루 되세요!
'file_get_contents'가 오류가 발생하지 않았는지 확인해야합니다. – geocodezip
오류를 삭제하지 않습니다. –