gps坐标转百度地图坐标 接口

@fright  March 19, 2018
/**
     * 通过坐标查找地址
     * @param $lat
     * @param $lng
     * @param string $type
     * @return bool
     */
    public static function getInfoByLocation($lat,$lng,$type='wgs84ll'){
        if(!$lat || !$lng) return false;
        $ak = C('baiduAk');
        $url = 'http://api.map.baidu.com/geocoder/v2/?callback=&output=json&pois=1&ak='.$ak.'&location='.$lat.','.$lng.
        '&coordtype='.$type.'&ret_coordtype=bd09ll';

        $location = ToolModel::getHTTPS($url);
        $location = json_decode($location,true);
        if ($location['status']>0){
            return false;
        }
        $result['lat'] = $location['result']['location']['lat'];
        $result['lng'] = $location['result']['location']['lng'];
        $result['address'] = $location['result']['formatted_address'];
        $result['baidu_code'] = $location['result']['addressComponent']['adcode'];
        $result['province'] = $location['result']['addressComponent']['province'];
        $result['city'] = $location['result']['addressComponent']['city'];
        $result['area'] = $location['result']['addressComponent']['district'];

        $format = self::_formatByCode($result['baidu_code']);
        $result['province_id'] = $format['province_id'];
        $result['city_id'] = $format['city_id'];
        $result['area_id'] = $format['area_id'];
        return $result;

    }

/**

 * 格式化城市 通过百度编号 获取数据库中的省市区
 * @param $code
 * @return mixed
 */
public static function _formatByCode($code){
    $area = self::getInfoByBaiduId($code);
    if($area && $area['parent_id']){
        $city = self::getInfoById($area['parent_id']);
        if($city && $city['parent_id']){
            $result['province_id'] = $city['parent_id'];
            $result['city_id'] = $city['id'];
            $result['area_id'] = $area['id'];
        }else{
            $result['province_id'] = $city['id'];
            $result['city_id'] = $area['id'];
            $result['area_id'] = 0;
        }
    }else{
        $result['province_id'] = $area['id'];
        $result['city_id'] = 0;
        $result['area_id'] = 0;
    }
    return $result;
}

评论已关闭