我使用HTML5 Geolocation API来获取经度和经度,然后我想将这些变量传递给外部PHP文件。该PHP文件(getPosition.php)从我的数据库返回JSON变量,然后我在geo.js文件上获取它以在Google Maps上创建标记。 在geo.js上,我还调用getPosition.php在Google Maps上创建标记
geo.js(获取PHP文件的变量)
GET /abc/api/Payment HTTP/1.1
Content-Type: application/x-www-form-urlencoded
X-PSK: [App Key]
X-Stamp: [UTC Timestamp]
X-Signature: [HMACSHA256 base 64 string]
Body:
var1, var1
getPosition.php
//这条线让我最近的商业回报(1公里),我需要我的位置才能获得商业信息。
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.getJSON('js/getPosition.php', function (data) {
我在geo.js上试过这个:
$lat = HERE I WANT LATITUDE FROM geo.js;
$lng = HERE I WANT LONGITUDE FROM geo.js;
$distance = 1;//Kms
$all = connectDB() -> query('SELECT commerce_name,commerce_offer,commerce_icon,commerce_lat,commerce_lng, (6371 * ACOS(SIN(RADIANS(commerce_lat)) * SIN(RADIANS(' . $lat . ')) + COS(RADIANS(commerce_lng - ' . $lng . ')) * COS(RADIANS(commerce_lat)) * COS(RADIANS(' . $lat . ')))) AS distance FROM commerce WHERE (commerce_lat BETWEEN ' . $box['min_lat']. ' AND ' . $box['max_lat'] . ') AND (commerce_lng BETWEEN ' . $box['min_lng']. ' AND ' . $box['max_lng']. ') HAVING distance < ' . $distance . ' ORDER BY distance ASC ');
echo json_encode($to_encode);
但是它一直循环,我也尝试通过AJAX获取变量但是没有工作...... 我迷失了
答案 0 :(得分:2)
使用ajax发布数据。
$.ajax({
type: "POST",
data: {lat:latitude,log:longitude},
url: "js/getPosition.php",
success: function(data){
}
});
js / getPosition.php文件:
$lat = $_REQUEST['lat'];
$lng = $_REQUEST['log'];