百度地图获取经纬度后,获取区域代码和地址

 百度地图获取经纬度后,获取区域代码和地址:


function getdistrct_code($location){

 		$url ="api.map.baidu.com/geocoder/v2/?ak=jhPGtfGrTzyoMNBRi1PVtkODgfelXofF&output=json&pois=1";
      
        $post_msg =array(
            "location"=>$location
        );
        $ret_json =httpsPost($url, $post_msg);
        $res = json_decode($ret_json);
          $district = $res->result->addressComponent->adcode;
  	return $district;
}


function httpsPost($url, $data = null)
{
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $temp=curl_exec ($ch);
    curl_close ($ch);
    return $temp;
}

调用方法 : eg : http://api.map.baidu.com/geocoder/v2/?ak=jhPGtfGrTzyoMNBRi1PVtkODgfelXofF&output=json&pois=1&location=34.247986,108.716148

$location=$lat.','.$lng;
$district = getdistrct_code($location);

得到结果:

{
	"status": 0,
	"result": {
		"location": {
			"lng": 108.71614799999995,
			"lat": 34.24798603293311
		},
		"formatted_address": "陕西省西安市长安区",
		"business": "",
		"addressComponent": {
			"country": "中国",
			"country_code": 0,
			"country_code_iso": "CHN",
			"country_code_iso2": "CN",
			"province": "陕西省",
			"city": "西安市",
			"city_level": 2,
			"district": "长安区",
			"town": "",
			"adcode": "610116",
			"street": "",
			"street_number": "",
			"direction": "",
			"distance": ""
		},
		"pois": [{
			"addr": "西安市长安区三二七乡道",
			"cp": " ",
			"direction": "东南",
			"distance": "122",
			"name": "阴水坊村",
			"poiType": "行政地标",
			"point": {
				"x": 108.71567262951756,
				"y": 34.24880657494323
			},
			"tag": "行政地标;村庄",
			"tel": "",
			"uid": "161a0ed2b1d038505cd4fedf",
			"zip": "",
			"parent_poi": {
				"name": "",
				"tag": "",
				"addr": "",
				"point": {
					"x": 0.0,
					"y": 0.0
				},
				"direction": "",
				"distance": "",
				"uid": ""
			}
		}, {
			"addr": "陕西省西安市长安区计家王马路西安市浙瑞编织袋厂",
			"cp": " ",
			"direction": "西北",
			"distance": "613",
			"name": "西安市浙瑞编织袋厂",
			"poiType": "公司企业",
			"point": {
				"x": 108.72059534371224,
				"y": 34.245285389256618
			},
			"tag": "公司企业;厂矿",
			"tel": "",
			"uid": "750ec16553912f564c3a590b",
			"zip": "",
			"parent_poi": {
				"name": "",
				"tag": "",
				"addr": "",
				"point": {
					"x": 0.0,
					"y": 0.0
				},
				"direction": "",
				"distance": "",
				"uid": ""
			}
		}, {
			"addr": "西宝公路",
			"cp": " ",
			"direction": "西南",
			"distance": "633",
			"name": "开心果幼儿园",
			"poiType": "教育培训",
			"point": {
				"x": 108.71949941099007,
				"y": 34.251805433637517
			},
			"tag": "教育培训;幼儿园",
			"tel": "",
			"uid": "669775d0c8b99826fd0eb039",
			"zip": "",
			"parent_poi": {
				"name": "",
				"tag": "",
				"addr": "",
				"point": {
					"x": 0.0,
					"y": 0.0
				},
				"direction": "",
				"distance": "",
				"uid": ""
			}
		}, {
			"addr": "西安市长安区",
			"cp": " ",
			"direction": "东",
			"distance": "755",
			"name": "后村",
			"poiType": "行政地标",
			"point": {
				"x": 108.70937550789262,
				"y": 34.24835897484259
			},
			"tag": "行政地标;村庄",
			"tel": "",
			"uid": "f4a33da9ed3637093010afdf",
			"zip": "",
			"parent_poi": {
				"name": "",
				"tag": "",
				"addr": "",
				"point": {
					"x": 0.0,
					"y": 0.0
				},
				"direction": "",
				"distance": "",
				"uid": ""
			}
		}, {
			"addr": "陕西省西安市长安区曹坊王马路高桥乡金太阳幼儿园",
			"cp": " ",
			"direction": "西南",
			"distance": "633",
			"name": "西安市长安区高桥乡中心幼儿园",
			"poiType": "教育培训",
			"point": {
				"x": 108.71949941099007,
				"y": 34.251805433637517
			},
			"tag": "教育培训;幼儿园",
			"tel": "",
			"uid": "a61a59beb80e72c20d0a7093",
			"zip": "",
			"parent_poi": {
				"name": "",
				"tag": "",
				"addr": "",
				"point": {
					"x": 0.0,
					"y": 0.0
				},
				"direction": "",
				"distance": "",
				"uid": ""
			}
		}],
		"roads": [],
		"poiRegions": [],
		"sematic_description": "阴水坊村东南122米",
		"cityCode": 233
	}
}
发布了250 篇原创文章 · 获赞 102 · 访问量 98万+

猜你喜欢

转载自blog.csdn.net/fengqingtao2008/article/details/84260134