所以我有一个PHP脚本从MySQL获取数据,msg_location
使用Point空间数据。我试图仅检索仅在10英里半径范围内的数据。以下是我到目前为止:
<?php
$lat = $_POST['latitude'];
$long = $_POST['longitude'];
require_once $_SERVER['DOCUMENT_ROOT'].'/assets/php/database/main.php';
$records = $conn->query('SELECT message, X(msg_location), Y(msg_location), loved
FROM hidyn_msg WHERE 3963 * ACOS(
SIN(RADIANS(X(msg_location))) * SIN(RADIANS(X(msg_location))) + COS(RADIANS(X(msg_location))) * COS(RADIANS(:lat)) * COS(RADIANS(:long) - RADIANS(:long))) <= 10
ORDER BY msg_date DESC');
$stmt -> bindparam(':lat', $lat);
$stmt -> bindparam(':long', $long);
try{
$records->execute();
$results = $records->fetchAll(PDO::FETCH_ASSOC);
if(count($results) > 0):
echo json_encode($results);
endif;
}catch(Exception $e){
$stmtError = $stmt->errorInfo();
$statusMSG= array('error'=>false, 'message' => "We had a issue creating and posting your message. Error: ", $e[0]);
echo json_encode(statusMSG);
}
?>
在顶部收到用户的GeoLocation并将其发布到php文件以恢复数据。
错误发生在$ records-&gt; execute();我已经尝试更改脚本以回显错误但它没有回应任何东西。 SQL可以在MySQL控制台中运行,效果很好。
工作代码: RamRider的帮助
<?php
$lat = $_POST['latitude'];
$long = $_POST['longitude'];
require_once $_SERVER['DOCUMENT_ROOT'].'/assets/php/database/main.php';
$sql = ('SELECT msg_date, message, loved
FROM hidyn_msg WHERE
msg_location = ((ACOS(SIN(:lat * PI() / 180) * SIN(X(msg_location) * PI() / 180) +
COS(:lat * PI() / 180) * COS(X(msg_location) * PI() / 180) * COS((:long - Y(msg_location)) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515) <= 10
ORDER BY msg_date DESC');
$stmt = $conn->prepare($sql);
$stmt -> bindparam(':lat', $lat);
$stmt -> bindparam(':long', $long);
try{
$results = $stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($results) > 0):
echo json_encode($results);
endif;
}catch(Exception $e){
$stmtError = $stmt->errorInfo();
$statusMSG= array('error'=>false, 'message' => "We had a issue creating and posting your message. Error: ", $e[0]);
echo json_encode(statusMSG);
}
?>
答案 0 :(得分:1)
参考我的评论(未经测试),通常更像是这样:
LatLng placemarker1 = new LatLng(41.634041, 25.356733);
mMap.addMarker(new MarkerOptions().position(placemarker1).title("This is the place"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(placemarker1));
答案 1 :(得分:0)
如果您使用的Mysql版本大于或等于5.7,那么您可以使用ST_DISTANCE_SPHERE https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html
mysql> SET @pt1 = ST_GeomFromText('POINT(0 0)');
mysql> SET @pt2 = ST_GeomFromText('POINT(180 0)');
mysql> SELECT ST_Distance_Sphere(@pt1, @pt2);
输出
ST_Distance_Sphere(@pt1, @pt2)
20015042.813723423
以米为单位的距离