# 根据地理位置来获取最近的商家

①首先采用微信或者是腾讯地图拉取到用户当前的经纬度 lbsyun.baidu.com/index.php?t… 基于浏览器定位

②调用 squarePoint来获取

define('EARTH_RADIUS', 6378.137);//地球半径
public function squarePoint($lng,$lat, $distance = 0.5) {$dlng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));

$dlng = rad2deg($dlng);

$dlat =$distance / EARTH_RADIUS; //EARTH_RADIUS地球半径

$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)

);
}

③条件判断 进行sql查询

$condition .="AND lat<>0 AND lat >= '{$point['right-bottom']['lat']}' AND lat <= '{$point['left-top']['lat']}' AND lng >= '{$point['left-top']['lng']}' AND lng <= '{$point['right-bottom']['lng']}'"; 复制代码 ④@param $lat$lng 是当前定位的经纬度 @param $row['lat'],$row['lng'] 是附近店铺的距离 算出的是km单位 $this->GetDistance($lat,$lng, $row['lat'],$row['lng']);

define ('M_PI', 3.1415926535898);

function getDistance($lat1,$lng1, $lat2,$lng2, $len_type = 1,$decimal = 2)
{

$radLat1 =$lat1 * M_PI / 180;

$radLat2 =$lat2 * M_PI / 180;

$a =$lat1 * M_PI / 180 - $lat2 * M_PI / 180;$b = $lng1 * M_PI / 180 -$lng2 * M_PI / 180;

$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));$s = $s * EARTH_RADIUS;$s = round($s * 1000); if ($len_type > 1) {

$s /= 1000; } return round($s, $decimal); } 复制代码 floor($temp['distance'])/1000;    转换成公里单位

sprintf('%.1f', (float)$juli)、 复制代码 通过jssdk 获取经纬度 调用百度转换详细位置 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=spsbwrDkEer15ikuKLG75UfKsHRM8Gse"></script> function getaddress(latitude,longitude){ var geoc = new BMap.Geocoder(); var point = new BMap.Point(longitude,latitude); geoc.getLocation(point, function (rs) { var addComp = rs.addressComponents; console.log(rs.addressComponents)$("input[name='address_now']").val(addComp.city)
placeSearch(addComp.city)
window.test=addComp.province + "" + addComp.city + "" + addComp.district + "" + addComp.street + "" +  addComp.streetNumber;
});
return window.test;
}