地点输入提示
百度地图 官方文档
用户可通过该服务,匹配用户输入关键词的地点推荐列表。在应用方面,可将地点推荐列表展示给用户,用户可通过点击等交互方式,结合地点检索服务,检索用户点击的POI信息,实现地点详情检索功能
基本说明:
接口地址:http://api.map.baidu.com/place/v2/suggestion
返回格式:json/xml
请求方式:get
请求示例:http://api.map.baidu.com/place/v2/suggestion?query=天安门&region=北京&city_limit=true&output=json&ak=你的ak
请求参数说明:
名称 类型 必填 说明
ak string 必填 开发者访问密钥,必选 扫码关注公众号
q/query string 必填 输入建议关键字(支持拼音)
region string 必填 支持全国、省、城市及对应百度编码(Citycode)。(指定的区域的返回结果加权,可能返回其他城市高权重结果。若要对返回结果区域严格限制,请使用city_limit参数)
city_limit string 选填 取值为"true",仅返回region中指定城市检索结果
location string 选填 传入location参数后,返回结果将以距离进行排序
coord_type int 选填 坐标类型(默认为3):1(WGS84ll即GPS经纬度),2(GCJ02ll即国测局经纬度坐标),3(BD09ll即百度经纬度坐标),4(BD09mc即百度米制坐标)。可选参数,用于标注请求中「location」参数使用的坐标类型
ret_coordtype string 选填 可选参数,添加后POI返回国测局经纬度坐标,若不传该参数,返回结果默认bd09ll(百度经纬度)
output string 选填 返回数据格式,可选json、xml两种
sn string 选填 用户的权限签名
timestamp string 选填 设置sn后该值必选
返回参数说明:
名称 类型 说明
status int 本次API访问状态,如果成功返回0,如果失败返回其他数字
message string 对API访问状态值的英文说明,如果成功返回"ok",并返回结果字段,如果失败返回错误说明
name string poi名称
location object poi经纬度坐标
uid string poi的唯一标示,ID
province string 省
city string 城市
district string 区县
tag string poi分类。默认不召回,若有需求请联系我们(完成企业认证后,才能开通权限)
address string poi地址。默认不召回,若有需求请联系我们(完成企业认证后,才能开通权限)
children poi子点。默认不召回,若有需求请联系我们;地点输入提示中的子点为简要信息,无poi坐标,如需要坐标,可通过返回的子点uid,使用地点检索中的地点详情检索功能检索坐标信息。(完成企业认证后,才能开通权限)
uid string poi子点ID,可用于poi详情检索。默认不召回,若有需求请联系我们(完成企业认证后,才能开通权限)
name string poi子点名称。默认不召回,若有需求请联系我们(完成企业认证后,才能开通权限)
show_name string poi子点简称。默认不召回,若有需求请联系我们(完成企业认证后,才能开通权限)
JSON返回示例:
{
	"status": 0,
	"message": "ok",
	"result": [{
			"name": "天安门",
			"location": {
				"lat": 39.915526,
				"lng": 116.403847
			},
			"uid": "65e1ee886c885190f60e77ff",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门广场",
			"location": {
				"lat": 39.909652,
				"lng": 116.404177
			},
			"uid": "c9b5fb91d49345bc5d0d0262",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门东-地铁站",
			"location": {
				"lat": 39.91408,
				"lng": 116.407851
			},
			"uid": "940aeb3c98d5a0218a2fb5de",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门广场-国旗",
			"location": {
				"lat": 39.91328,
				"lng": 116.403931
			},
			"uid": "4ae2adcf574bcd2b38221c66",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门西-地铁站",
			"location": {
				"lat": 39.913776,
				"lng": 116.39805
			},
			"uid": "002975204d3b1e9b9968b4de",
			"province": "北京市",
			"city": "北京市",
			"district": "西城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门-华表",
			"location": {
				"lat": 39.91433,
				"lng": 116.404413
			},
			"uid": "3ff8be14bf465f3682288ac1",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门东-公交车站",
			"location": {
				"lat": 39.914086,
				"lng": 116.408016
			},
			"uid": "e1ee3a5e7be676c8213bf257",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门西-公交车站",
			"location": {
				"lat": 39.913723,
				"lng": 116.398056
			},
			"uid": "a8e23c622c11486eae045714",
			"province": "北京市",
			"city": "北京市",
			"district": "西城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门-出口",
			"location": {
				"lat": 39.91543,
				"lng": 116.404241
			},
			"uid": "e93b3a20e96f63d55cd4fe95",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		},
		{
			"name": "天安门广场-出入口",
			"location": {
				"lat": 39.911425,
				"lng": 116.402508
			},
			"uid": "797c4f6023068d0ceedfcaf9",
			"province": "北京市",
			"city": "北京市",
			"district": "东城区",
			"business": "",
			"cityid": "131"
		}
	]
}
服务级错误码参照
错误码 说明
0 服务请求正常召回
1 服务器内部错误
2 请求参数非法
3 权限校验失败
4 配额校验失败
5 ak不存在或者非法
102 不通过白名单或者安全码不对
2xx 无权限
3xx 配额错误
完整教学代码示例
<?php
/**
 * Created by PhpStorm.
 * User: FZS
 * Time: 2019/9/3 23:10
 */
//----------------------------------
// 地点输入提示 调用类
//----------------------------------
class freeApi{
    private $ak;
    private $apiUrl;

    public function __construct($ak){
        $this->ak = $ak;
        $this->apiUrl = 'http://api.map.baidu.com/place/v2/suggestion?query=天安门&region=北京&city_limit=true&output=json&ak='.$this->ak;
    }
    /**
     * 获取结果
     * @return array
     */
    public function getResult(){
        return $this->freeApiCurl($this->apiUrl);
    }
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function freeApiCurl($url,$params=false,$ispost=0){
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }
        curl_close( $ch );
        return $response;
    }
}