我试图通过它们到搜索条件中给定位置的距离来过滤搜索结果。每个优惠都有自己的位置,包括(德国)邮政编码和属于它的城市。搜索条件包括相同的原则。
因此,最终,搜索者的位置和报价的多个位置。
此外,所有报价都有他们可以操作的半径。
然后我想过滤掉与搜索者位置相距更远的所有报价,而不是他们的操作半径。
我使用过这个PHP -code:
$position = $_GET['position']!='' ? $_GET['position'] : '';
从我的网址中获取搜索者的位置。
$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';
}
}
这就是这个功能,我在这个论坛中找到了两个地址之间的距离。
它大部分时间都有效,我得到了正确的距离,例如:77.4640983104 km。但是每运行两到三次,我就会收到这个错误:
注意:尝试获取非对象的属性
和
注意:未定义的偏移量:0
在&#34;从地理数据中获取纬度和经度&#34;截面和0公里的错误距离作为输出。
我真的不知道我在做什么错,但我希望有人可以向我解释,应该改变什么,或者说是什么整个事情以错误的方式接近。
提前非常感谢你,祝你有个美好的一天!
答案 0 :(得分:0)
注意:强>
如果将Google API-Key添加到网址,该功能确实有效。 好像我发现的功能是从使用Maps商业化之前的时间开始。