[英]How to find the current location(longitude and latitude) using Google Map?
[英]How to find nearest location by latitude and longitude?
這是loc_coordinate
表結構:
下面是代碼,用於從數據庫中獲取最近的地點並顯示存儲在數據庫本身中的地點名稱。
<?php
include("config.php");
$lat = "3.107685";
$lon = "101.7624521";
$sql="SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS 'distance' FROM loc_coordinate HAVING 'distance'<='10' ORDER BY 'distance' ASC";
$stmt =$pdo->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch())
{
echo $row['place'];
}
?>
為此顯示的錯誤:
致命錯誤:在 C:\\wamp\\www\\mysite\\by_coor.php 第 8 行
PDOException: 在 C:\\wamp\\www\\mysite\\by_coor.php 第 8 行
echo $sql
顯示:
SELECT ((ACOS(SIN(3.107685 * PI() / 180) * SIN(lat * PI() / 180) + COS(3.107685 * PI() / 180) * COS(lat * PI() / 180) * COS ((101.7624521 – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS 'distance' FROM loc_coordinate HAVING 'distance'<='10' ORDER BY 'distance' ASC
我不確定為什么會收到此錯誤。 這是我為 SQL 查詢參考的站點:http: //zcentric.com/2010/03/11/calculate-distance-in-mysql-with-latitude-and-longitude/
嘗試這個
SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( $lat - LatOnTable) * pi()/180 / 2), 2) +COS( $lat * pi()/180) * COS(LatOnTable * pi()/180) * POWER(SIN(( $long - LongOnTable) * pi()/180 / 2), 2) ))) as distance
from yourTable
having distance <= 10
order by distance
用緯度表列名替換 LatOnTable,用表中的經度列名替換 longOnTable。
這是在 10 英里半徑內查找給定坐標的最近位置的 SQL 語句。 它根據該行的緯度/經度和目標緯度/經度計算距離,然后僅詢問距離值小於等於10的行,按距離對整個查詢進行排序。 要按公里而不是英里搜索,請將 3959 替換為 6371 。
SELECT
id, (
3959 * acos (
cos ( radians($lat) )
* cos( radians( tableLatColName ) )
* cos( radians( tableLogColName ) - radians($long) )
+ sin ( radians($lat) )
* sin( radians( tableLatColName ) )
)
) AS distance
FROM table_name
HAVING distance <= 10
ORDER BY distance;
這是使用帶有 MySQL 后端的 Google Maps API v3-
https://developers.google.com/maps/solutions/store-locator/clothing-store-locator#findnearsql
我正在做同樣的事情並發現了這個問題,所以只想分享它.. :)
這對我有用:
SELECT restoran.id,restoran.restoran , (6371 * 2 * ASIN(SQRT( POWER(SIN(( -6.9831375276568055 - restoran.lat) * pi()/180 / 2), 2) +COS( -6.9831375276568055 * pi()/180) * COS(restoran.lat * pi()/180) * POWER(SIN(( 110.40925562381744 - restoran.lng) * pi()/180 / 2), 2) ))) as distance from restoran having distance <= 10 order by distance
6371
數字用於轉換為公里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.