Javascript 地理定位与距离计算
我有一些计划。我想读取手机的当前位置,并计算从数据库加载的一些位置之间的距离。每个部分都能完美分开,但我不能让它配合。当我计算距离时,似乎还没有得到手机的位置(至少我认为是这个问题) 请帮我找到解决办法!我将非常感激 TrackPosition.js:Javascript 地理定位与距离计算,javascript,php,html,gps,geolocation,Javascript,Php,Html,Gps,Geolocation,我有一些计划。我想读取手机的当前位置,并计算从数据库加载的一些位置之间的距离。每个部分都能完美分开,但我不能让它配合。当我计算距离时,似乎还没有得到手机的位置(至少我认为是这个问题) 请帮我找到解决办法!我将非常感激 TrackPosition.js: var op, prev_lat, prev_long, info_string="", current_lat=0, current_long=0, wpid; function format_time_component(time_com
var op, prev_lat, prev_long, info_string="", current_lat=0, current_long=0, wpid;
function format_time_component(time_component){
op=document.getElementById("output");
if(time_component<10)
time_component="0"+time_component;
else if(time_component.length<2)
time_component=time_component+"0";
return time_component;
}
function geo_success(position){
info_string="";
var d=new Date(); // Date object, used below for output messahe
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
var current_datetime=format_time_component(h)+":"+format_time_component(m)+":"+format_time_component(s);
if(prev_lat!=position.coords.latitude || prev_long!=position.coords.longitude){
prev_lat=position.coords.latitude;
prev_long=position.coords.longitude;
current_lat=position.coords.latitude;
current_long=position.coords.longitude;
info_string="Current positon: lat="+current_lat+", long="+current_long+
" (accuracy "+Math.round(position.coords.accuracy, 1) +"m) " + current_datetime + "<br>";
}
if(info_string)
op.innerHTML=info_string;
}
function geo_error(error) {
switch(error.code)
{
case error.TIMEOUT:
op.innerHTML="Timeout!";
break;
};
}
function posInit()
{
if(!!navigator.geolocation){
wpid=navigator.geolocation.watchPosition(geo_success, geo_error, {
enableHighAccuracy:true,
maximumAge:30000,
timeout:27000});
} else{
op.innerHTML="ERROR: Your Browser doesnt support the Geo Location API";
}
}
window.onload=posInit();
var op,prev_lat,prev_long,info_string=“”,current_lat=0,current_long=0,wpid;
函数格式\u时间\u组件(时间\u组件){
op=document.getElementById(“输出”);
if(时间)组件
var s=;
文件写入(距离(当前纬度、当前纬度、s.lat、s.long));
我想您需要在geolocation回调函数中计算距离,但如何从geolocation回调函数中的数据库中获取某个位置的坐标呢?geolocation函数运行良好。我想PHP脚本也可以运行良好,您可以使用它来填充javascript变量“s”-PHP脚本将始终s在页面呈现之前完成,以便可以将javascript变量写入页面(如上所述)在包含其他javascript文件之前。然后加载页面,geolocation函数关闭并执行它的操作-由于您全局声明变量“s”,回调函数应该可以访问“s”-这意味着回调函数可以将“s”的值传递给距离计算函数,但它没有。Whe我从未将“s”传递给回调函数,因为它被视为未定义。我尝试了几种组合来实现这一点,但没有任何成功。这非常奇怪……我在声明“s”后加载TrackPosition.js。我没有收到任何关于未声明变量的错误,但仍然没有填充值。:/将其指定为其他变量名-可能是y你有冲突吗?当我像你一样尝试使用“s”时,我也得到了一个数字-因此我将其改为“sdata”,似乎一切正常。我看到了从现在开始得到的数字-在geo_成功函数中,你将“s”指定为秒
<?php
class Package{
public $id;
public $names;
public $surname;
public $street;
public $city;
public $country;
public $long;
public $lat;
public function __construct($id, $names, $surname, $street, $city, $country) {
$this->id = $id;
$this->names = $names;
$this->surname = $surname;
$this->street = str_replace(' ', '+', $street);
$this->city = $city;
$this->country = $country;
$this->getPosition();
}
public function getPosition(){
$address = $this->street . "+" . $this->city . "+" .$this->country;
$url = "http://maps.google.com/maps/api/geocode/json?address=" . $address . "&sensor=false®ion=UK";
$response = file_get_contents($url);
$response = json_decode($response, true);
$this->lat = $response['results'][0]['geometry']['location']['lat'];
$this->long = $response['results'][0]['geometry']['location']['lng'];
}
public function toJSON(){
$json = array(
'id' => $this->id,
'names' => $this->names,
'surname' => $this->surname,
'street' => $this->street,
'city' => $this->city,
'country' => $this->country,
'lat' => $this->lat,
'longs' => $this->long
);
return json_encode($json);
}
}
$link = mysql_connect("localhost", "root", "");
mysql_select_db("COURIERKIT", $link);
$result = mysql_query("SELECT * FROM packages");
$packages = array();
while ($row = mysql_fetch_array($result)) {
$packages[] = new Package($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]);
}
mysql_free_result($result);
?>
<html>
<head>
<script src="TrackPosition.js" type="text/javascript"></script>
<script src="Distance.js" type="text/javascript"></script>
</head>
<body>
<div class="output" id="output"></div>
<script type="text/javascript">
var s = <?php print $packages[0]->toJSON(); ?>;
document.write(distance(current_lat, current_long, s.lat, s.longs));
</script>
</body>
</html>