所以我尝试使用多个变量进行半径类型搜索,我已经为我的数据库中的每个城市存储了Lat / Long,但我注意到我想要做的大多数解决方案都要求我查询整个数据库(最终可能超过50,000行)使用函数中存储的lat / long的值。
无论如何都要使用这个存储的数据库值而不先从查询中获取它,并在查询期间使用它。
这就是我现在的工作方式,但我认为这需要很多,特别是一旦数据库达到100,000 +行,可能需要一些时间才能完成每一行,如果我能检查距离会更好我认为这样的查询可以更好地过滤结果,而无需检查每一行。
function getLocation($address) {
if (!empty($address)) {
//Formatted address
$formattedAddr = str_replace(' ', '+', $address);
//Send request and receive json data by address
$geocodeFromAddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . $formattedAddr . '&sensor=true_or_false');
$output1 = json_decode($geocodeFromAddr);
//Get latitude and longitute from json data
$latitude = $output1->results[0]->geometry->location->lat;
$longitude = $output1->results[0]->geometry->location->lng;
return "$latitude,$longitude";
}
else {
return false;
}
}
function GetDrivingDistance($lat1, $lat2, $long1, $long2) {
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" . $lat1 . "," . $long1 . "&destinations=" . $lat2 . "," . $long2 . "&mode=driving&units=imperial";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
return $dist;
//return array('distance' => $dist, 'time' => $time);
}
$sql = "SELECT * FROM mytable WHERE `date` >= '$selected_date'";
if ($length && !empty($length)) {
$sql.= " AND `length`<='$length'";
}
$sql.= " ORDER BY date_created DESC";
$CITY = "DALLAS, TX";
// FOR TESTING
$SLAT = getLocation($CITY);
$oparr = split("\,", $SLAT);
$FLAT = $oparr[0];
$FLONG = $oparr[1];
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$city = $row['city'];
$state = $row['state'];
$lat = $row['lats'];
$long = $row['longs'];
$distance = GetDrivingDistance($FLAT, $lat, $FLONG, $long);
if ($distance <= 200) {
// SHOW TABLE RESULTS
}
else {
echo "No results";
}
}
答案 0 :(得分:0)
基于来自Fastest Way to Find Distance Between Two Lat/Long Points的查询,我发现您可以计算MySQL中的距离,从而减少在调用Google Maps API之前从数据库中读取记录的需要。请参阅下面的代码示例。
看一下表中有3条记录的this SQLFiddle,其中两条距离德克萨斯州达拉斯200英里(另一条在休斯顿附近)。
//get location for Dallas
//getLocation('Dallas, TX') //
$lat = 32.7856717;
$lng = -96.77326000000001;
$query = 'SELECT city, ( 3959 * acos( cos( radians('.$lat.') ) * cos( radians( mytable.lats ) )
* cos( radians(mytable.longs) - radians('.$lng.')) + sin(radians('.$lat.'))
* sin( radians(mytable.lats)))) AS distance FROM mytable having distance < 200';
//$connection initialized earlier via mysqli_connect()
$results = $connection->query($query);
while($row = $results->fetch_object()) {
//show table results
echo 'record: '.$row->city.' - distance: '.$row->distance.'<br />';
}
您可能还考虑在构建中添加this plugin,以便可以使用用户定义的函数(UDF) hasrsine_distance() - 然后您的查询可以简化为:< / p>
$query = 'SELECT city, haversine_distance('.$lat.','.$lng.',mytable.lats,mytable.longs) AS distance FROM mytable having distance < 200';
答案 1 :(得分:-1)
$ sql =“ SELECT caid
,months
,visa
,tickets
,package
,income
,expenses
cash
按日期(现金月)分组;
$ result = $ conn->查询($ sql);
$result=mysqli_query($conn, $sql);
$rows=mysqli_fetch_array($result);
// $d = $data["expenses"];
$res=mysqli_query($conn, $sql);
$data=mysqli_fetch_array($res);
if ($result-> num_rows >0){
while($row = $result-> fetch_assoc()){
//$expenses = $row["expenses"];
// echo $expenses;
echo $row["visa"]."</br>" ;
}
}