我有一些项目。我想读取手机的当前位置并计算它从数据库加载的某些位置之间的距离。每个部分完全分开,但我无法合作。似乎当我计算距离时,手机的位置尚未获得(至少我认为是问题)。
请帮我找到解决方案!我将非常感激。
TrackPosition.js:
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();
的index.php:
<?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>