我使用AJAX / jQuery从MySQL数据库中获取数据,然后在客户端进行一些计算(坐标之间的距离)。我发现这对浏览器来说非常沉重,而且宁愿在服务器端这样做。
我返回的JSON数据如下所示:
{
"result": [
["148", "osmand", "2", "2016-03-26 13:48:04", "2016-03-26 13:48:01", "2016-03-26 13:48:01", "1", "-39.094856", "46.166472", "1432.7", "0", "0", "20 Maretha street", "{\"battery\":\"0\",\"ip\":\"105.5.117.20\"}"],
["149", "osmand", "2", "2016-03-26 13:48:24", "2016-03-26 13:48:22", "2016-03-26 13:48:22", "1", "-39.099305", "46.162392", "1435.26", "0", "0", "7 Ernst street", "{\"battery\":\"0\",\"ip\":\"105.5.117.20\"}"],
["150", "osmand", "2", "2016-03-26 13:48:45", "2016-03-26 13:48:43", "2016-03-26 13:48:43", "1", "-39.099305", "46.162392", "1435.62", "0", "0", "7 Ernst street", "{\"battery\":\"0\",\"ip\":\"105.5.117.20\"}"],
],
"errors": false
}
第七和第八个值是坐标。我目前正在通过绘制坐标然后绘制折线然后计算折线在折页中的距离来计算距离。
然而,我发现了一些PHP代码示例,用于计算两点之间的距离:
class test {
public function GetDistance($lat1, $lng1, $lat2, $lng2) {
$radLat1 = $lat1*3.1415926535898/180.0;
$radLat2 = $lat2*3.1415926535898/180.0;
$a = $radLat1 - $radLat2;
$b = ($lng1*3.1415926535898/180.0) - ($lng2*3.1415926535898/180.0);
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
$s = $s * 6378.137; // EARTH_RADIUS;
$s = round($s * 1000,3);
return $s;
}
}
用法示例
$obj=new test();
$dis=$obj->GetDistance($lat1,$lon1,$lat2,$lon2);
我的PHP代码目前看起来像这样:
<?php
$inputvalues = $_POST;
$errors = false;
$result = false;
include_once 'database.php';
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
foreach ($inputvalues as $key => $value) {
if(isset($value) && !empty($value)) {
$inputvalues[$key] = $mysqli->real_escape_string( $value );
} else {
$errors[$key] = 'The field '.$key.' is empty';
}
}
if( !$errors ) {
$addresult = "
SELECT * FROM positions WHERE `fixtime` BETWEEN '" . $inputvalues['start'] . "' AND '" . $inputvalues['end'] . "' AND deviceid='" . $inputvalues['deviceid'] . "'
";
if( $result = $mysqli->query($addresult) ) {
while($row = $result->fetch_all())
{
$returnResult = $row;
}
}
}
mysqli_close($mysqli);
echo json_encode(['result' => $returnResult, 'errors' => $errors]);
exit;
?>
如何在我的代码中实现这一点?我不知道如何从mysql结果中获取坐标,对每个坐标进行计算,然后通过JSON输出。
很抱歉,如果这是一个基本或广泛的问题,我对PHP很新,我还在学习。
答案 0 :(得分:0)
这是我的功能,可以将结果保存到中间文件中。我不确定它是否适合你但可能会有所帮助。更新MY_KEY和使用模式。
function get_distance($locations, $locs){
$location_distance = array();
foreach($locations as $location_key=>$location){
$locs_keys = array_keys($locs);
$loc_lat_lng = "$location[1],$location[2]";
$locs_lat_lng = join('|',$locs_keys);
$path = './'.$location_key;
if(!file_exists($path)){
$path = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$loc_lat_lng&destinations=$locs_lat_lng&mode=walking&key=MY_KEY";
}
$map_data = file_get_contents($path);
if($path != './'.$location_key){
file_put_contents('./'.$location_key, $map_data);
}
$map_data = json_decode($map_data, true);
$distance = reslove_distance($map_data);
for($i = 0 ; $i < count($distance); $i++){
$location_distance[$location_key][$locs_keys[$i]] = $distance[$i];
}
}
return $location_distance;
}
function reslove_distance($map_data=null){
$distance = array();
foreach($map_data['rows'][0]['elements'] as $element){
$distance[] = (int)$element['distance']['value'];
}
return $distance;
}
答案 1 :(得分:0)
也许是这样的:
function getDistance($lat1, $lng1, $lat2, $lng2, $distance_unit = 'km')
{
$multiplicator = ($distance_unit == 'km' ? 6371 : 3959);
$lat1_rad = deg2rad($lat1);
$lng1_rad = deg2rad($lng1);
$lat2_rad = deg2rad($lat2);
$lng2_rad = deg2rad($lng2);
return $multiplicator * acos(cos($lat1_rad) * cos($lat2_rad) * cos($lng2 - $lng1) + sin($lat1_rad) * sin($lat2));
}
然后当您从数据库中获取结果时:
$entries = $result->fetch_all();
foreach ($entries as $index => &$entry) {
if ($index < count($entries)) {
$next = $entries[$index + 1];
$entry['distance'] = self::getDistance($entry['latitude'], $entry['longitude'], $next['latitude'], $next['longitude']);
} else {
$entry['distance'] = 0; // there is no "next" point to calculate the distance.
}
}
这应该给你一个数组,其中每个条目包含到下一个点的距离