这是loc\u坐标
表格结构:
下面是从数据库中获取最近位置并显示存储在数据库中的地名的代码。
<?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'];
}
?>
显示的错误如下:
致命错误:在第8行的C:\wamp\www\mysite\by_coor.php
PDO异常:在C:\wamp\www\mysite\by\u coor中。php第8行
echo$sql
显示以下内容:
选择((ACOS(SIN(3.107685*PI()/180)*SIN(lat*PI()/180)COS(3.107685*PI()/180)*COS(lat*PI()/180)*COS((101.7624521)*PI()60 * 1.1515)距离loc_coordinate有距离
我不确定为什么会出现这个错误。这是我在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替换为表中的经度列名。
下面是SQL语句,用于查找距离给定坐标10英里半径内的最近位置。它根据该行的纬度/经度和目标纬度/经度计算距离,然后只询问距离值小于等于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;
这是使用Google Maps API v3和MySQL后端-
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
数字用于转换为km