<?php
$userId=4; //$_POST['userId']
$longitude='108.362073'; //用户当前位置的经度 $_POST['longitude']
$latitude='22.818088'; //用户当前位置的纬度 $_POST['latitude']
$longitude=(float)$longitude;
$latitude=(float)$latitude;
$checks=2; //1为补胎,2为停车场,3为汽修,4为配件,5为本地
$pageIndex=1; //传过来的页数(最少是1) $_POST['pageIndex']
$pageSize=1; //每页多少条数据 $_POST['pageSize']
$pages=($pageIndex-1)*$pageSize; //根据传过来的页数,计算跳过的记录数
require('DBDriver.php');
$connect =Db::getInstance()->connect();
$result = array();
$items = array();
//根据出参获取地址列表
$sqlall="select * from mh_place where type='$checks'";
$valall=sqlsrv_query($connect,$sqlall);
$itemsall = array();
while($rowall=sqlsrv_fetch_array($valall,SQLSRV_FETCH_ASSOC)){
//计算距离
$rowall["juli"]=getDistance((float)$latitude, (float)$longitude, (float)$rowall["latitude"], (float)$rowall["longtitude"]);
foreach ($rowall as $k=>$v) {
if(is_string($rowall["$k"])){
$rowall["$k"] = iconv('GBK', 'UTF-8', $v);
$rowall["$k"]=trim($rowall["$k"]);
}
}
//encode_json方法只支持utf8编码 使用前需转码
array_push($itemsall,$rowall);
}
//根据距离排序
$flag=array();
foreach($itemsall as $arr2){
$flag[]=$arr2["juli"];
$placeId[]=$arr2["placeId"];
}
array_multisort($flag, SORT_ASC, $itemsall);
$count=count($itemsall);
//利用分页取数据
$qu=$pageIndex*$pageSize;
if($qu>$count){
$qu=$count;
}
for($i=$pages;$i<$qu;$i++){
array_push($items,$itemsall[$i]);
}
$result["place"] = $items;
echo encode_json($result);
sqlsrv_close($connect);
//json_encode转换中文解决方法
function encode_json($str){
$code = json_encode($str);
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
),
$code);
}
/**
* @desc 根据两点间的经纬度计算距离
* @param float $lat 纬度值
* @param float $lng 经度值
*/
function getDistance($lat1, $lng1, $lat2, $lng2)
{
$earthRadius = 6367000; //approximate radius of earth in meters
/*
Convert these degrees to radians
to work with the formula
*/
$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;
$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * pi() ) / 180;
/*
Using the
Haversine formula
http://en.wikipedia.org/wiki/Haversine_formula
calculate the distance
*/
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;
return round($calculatedDistance);
}
?>
$userId=4; //$_POST['userId']
$longitude='108.362073'; //用户当前位置的经度 $_POST['longitude']
$latitude='22.818088'; //用户当前位置的纬度 $_POST['latitude']
$longitude=(float)$longitude;
$latitude=(float)$latitude;
$checks=2; //1为补胎,2为停车场,3为汽修,4为配件,5为本地
$pageIndex=1; //传过来的页数(最少是1) $_POST['pageIndex']
$pageSize=1; //每页多少条数据 $_POST['pageSize']
$pages=($pageIndex-1)*$pageSize; //根据传过来的页数,计算跳过的记录数
require('DBDriver.php');
$connect =Db::getInstance()->connect();
$result = array();
$items = array();
//根据出参获取地址列表
$sqlall="select * from mh_place where type='$checks'";
$valall=sqlsrv_query($connect,$sqlall);
$itemsall = array();
while($rowall=sqlsrv_fetch_array($valall,SQLSRV_FETCH_ASSOC)){
//计算距离
$rowall["juli"]=getDistance((float)$latitude, (float)$longitude, (float)$rowall["latitude"], (float)$rowall["longtitude"]);
foreach ($rowall as $k=>$v) {
if(is_string($rowall["$k"])){
$rowall["$k"] = iconv('GBK', 'UTF-8', $v);
$rowall["$k"]=trim($rowall["$k"]);
}
}
//encode_json方法只支持utf8编码 使用前需转码
array_push($itemsall,$rowall);
}
//根据距离排序
$flag=array();
foreach($itemsall as $arr2){
$flag[]=$arr2["juli"];
$placeId[]=$arr2["placeId"];
}
array_multisort($flag, SORT_ASC, $itemsall);
$count=count($itemsall);
//利用分页取数据
$qu=$pageIndex*$pageSize;
if($qu>$count){
$qu=$count;
}
for($i=$pages;$i<$qu;$i++){
array_push($items,$itemsall[$i]);
}
$result["place"] = $items;
echo encode_json($result);
sqlsrv_close($connect);
//json_encode转换中文解决方法
function encode_json($str){
$code = json_encode($str);
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
),
$code);
}
/**
* @desc 根据两点间的经纬度计算距离
* @param float $lat 纬度值
* @param float $lng 经度值
*/
function getDistance($lat1, $lng1, $lat2, $lng2)
{
$earthRadius = 6367000; //approximate radius of earth in meters
/*
Convert these degrees to radians
to work with the formula
*/
$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;
$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * pi() ) / 180;
/*
Using the
Haversine formula
http://en.wikipedia.org/wiki/Haversine_formula
calculate the distance
*/
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;
return round($calculatedDistance);
}
?>