<?php
/**
- @desc 根据两点间的经纬度计算距离
- @param float $lat 纬度值
- @param float $lng 经度值
*/
function getDistance($lat1, $lng1, $lat2, $lng2){
$earthRadius = 6367000; //approximate radius of earth in meters
$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;
$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * pi() ) / 180;
$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);
return $calculatedDistance;
}
//循环调用然后依次返回距离
/**
*计算某个经纬度的周围某段距离的正方形的四个点
*
*@param lng float 经度
*@param lat float 纬度
*@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为5千米
*@return array 正方形的四个点的经纬度坐标
*/
function returnSquarePoint($lng, $lat,$distance = 5)
{
$dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
$dlat = $distance/6371;//地球半径,平均半径为6371km
$dlat = rad2deg($dlat);
return array(
'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
);
}
//计算附近五公里的经纬度范围
//根据这个范围到数据库查询满足条件的店面
/**
- 获取一定范围内的地点
*/
function getSiteData($lng,$lat,$userType)
{
//使用此函数计算得到结果后,带入sql查询。
$squares = $this->returnSquarePoint($lng, $lat);
if($squares['left-top']['lng'] > $squares['right-bottom']['lng']){
$lngMin = $squares['right-bottom']['lng'];
$lngMax = $squares['left-top']['lng'];
}else{
$lngMin = $squares['left-bottom']['lng'];
$lngMax = $squares['right-top']['lng'];
}
if($squares['left-top']['lat'] > $squares['right-bottom']['lat']){
$latMin = $squares['right-bottom']['lat'];
$latMax = $squares['left-top']['lat'];
}else{
$latMin = $squares['left-top']['lat'];
$latMax = $squares['right-bottom']['lat'];
}
$where = "lng<>0 and lng>=$lngMin and lng<=$lngMax and lat>=$latMin and lat<=$latMax and status=1";
if($userType){
$where .= " AND type = 2";
}else{
$where .= " AND type = 1";
}
$site_info = M('Site')->field('id,address,lat,lng')->where($where)->select();
return $site_info;
}
/**
*根据地理位置获取经纬度(高德地图)
*/
function getLngLat()
{
//获取地址所属经纬度
//$latLng = addressToLatLng($address);
$url="https://restapi.amap.com/v3/geocode/geo?address=".$address."&key=45f048d3ba68c3e4c1a80f38d9ab96a8";
$result=file_get_contents($url);
$data=json_decode($result,true);
if($data['status']!=1){
$this->ajaxReturn(array('code'=>404,'msg'=>$data['info']));
}
$location =explode(',',$data['geocodes']['0']['location']);
$lat = $location['1'];
$lng=$location['0'];
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。