Client.php 1.17 KB
<?php

/**
 * 
 * @author tongdesheng
 *
 */
class YHMUtils_Lbs_Client {
	
	
	/**
	 * 通过经纬度获取省、市、区
	 * @param float $lat
	 * @param float $lng
	 */
	public static function getAddrFromLatlng($lat, $lng) {
		
		$url = YHMConfig_Lbs::API_URL . '?ak=' . YHMConfig_Lbs::AK . '&location=' . $lat .',' . $lng
				. '&output=json&pois=0';
		$httpClient = new Q_Utils_Http($url);
		$httpClient->request('get');
		$retData = array();
		if($httpClient->getStatus() == 200) {
			$strRet = $httpClient->getBody();  //{"status":0,"result":{"location":{"lng":116.44897797499,"lat":39.926069092316},"formatted_address":"北京市朝阳区南营房七条7号","business":"朝外,朝阳门,东大桥","addressComponent":{"city":"北京市","district":"朝阳区","province":"北京市","street":"南营房七条","street_number":"7号"},"cityCode":131}}
			$addrData = json_decode($strRet, true);
			if($addrData['status'] == 0) {
				$retData = array(
					'province' => $addrData['result']['addressComponent']['province'],
					'city' => $addrData['result']['addressComponent']['city'],
					'area' => $addrData['result']['addressComponent']['district']
				);
			}
		}
		return $retData;
	}
	
}