百度地图api地址正逆解析

百度地图api地址正逆解析

类封装

<?php
namespace app\common\util;


/**
 * @author jason
 * @date 2021-04-28
 * @descript 百度地图接口方法封装类
 * Class BaiduApi
 * @package app\common\util
 */
class BaiduMapApi
{
    // 百度geocoder API
    const API = "http://api.map.baidu.com/geocoder/v2/";
    // 百度reverse geocoder API
    const REVERSE_API = "http://api.map.baidu.com/geocoder/v2/";

    /**
     * @author jason
     * @date 2021-04-28
     * @descript 地址解析
     * @param $address //地址信息(如:南京市栖霞区迈皋桥街道迈越路18号)
     * @param $ak //ak密钥
     * @return array 包含经纬度
     */
    public static function geocoder($address, $ak)
    {
        $address_data = urlencode($address);
        $url = self::API."?address=".$address_data."&output=json&ak=".$ak;
        $address_map_data = file_get_contents($url);
        $json_data = json_decode($address_map_data, true);

        return $json_data;
    }

    /**
     * @author jason
     * @date 2021-04-28
     * @descript 逆地址解析
     * @param $lng //经度 如:116.302509
     * @param $lat //纬度 如:39.991041
     * @param $ak //ak密钥
     * @return mixed
     */
    public static function reverseGeocoder($lng, $lat, $ak)
    {
        $url = self::REVERSE_API."?ak=$ak&output=json&coordtype=bd09ll&location=$lat,$lng";
        $address_data = file_get_contents($url);
        $json_data = json_decode($address_data, true);

        return $json_data;
    }

}

类调用一

$address = '南京市栖霞区迈皋桥街道迈越路18号';
$location = BaiduMapApi::geocoder($address, env('BAIDU_MAP.AK'));

响应一

Array
(
    [status] => 0
    [result] => Array
        (
            [location] => Array
                (
                    [lng] => 118.86707868773
                    [lat] => 32.113371513019
                )

            [precise] => 1
            [confidence] => 80
            [comprehension] => 100
            [level] => 门址
        )

)

调用二

$lng = 116.302509;
$lat = 39.991041;
$address = BaiduMapApi::reverseGeocoder($lng, $lat, env('BAIDU_MAP.AK'));

##响应二

Array
(
    [status] => 0
    [result] => Array
        (
            [location] => Array
                (
                    [lng] => 116.302509
                    [lat] => 39.991041070751
                )

            [formatted_address] => 北京市海淀区海淀三山五园绿道
            [business] => 万泉河,万柳,中关村
            [addressComponent] => Array
                (
                    [country] => 中国
                    [country_code] => 0
                    [country_code_iso] => CHN
                    [country_code_iso2] => CN
                    [province] => 北京市
                    [city] => 北京市
                    [city_level] => 2
                    [district] => 海淀区
                    [town] => 
                    [town_code] => 
                    [adcode] => 110108
                    [street] => 海淀三山五园绿道
                    [street_number] => 
                    [direction] => 
                    [distance] => 
                )

            [pois] => Array
                (
                )

            [roads] => Array
                (
                )

            [poiRegions] => Array
                (
                    [0] => Array
                        (
                            [direction_desc] =>[name] => 海淀公园
                            [tag] => 旅游景点;公园
                            [uid] => 92df8c20d3c448cf52e5c5f5
                            [distance] => 0
                        )

                )

            [sematic_description] => 海淀公园内,西苑草场-188号东412[cityCode] => 131
        )

)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大叔是90后大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值