有需求联系

Information

- 信息浏览 -

php根据位置获取经纬度(百度地图)

2023-12-31 201

  1. 根据地址获取经纬度

  /**根据位置获取经纬度
     * @param $area
     * @return mixed
     */
    protected function getLonLLat($area)
    {
        if (!$area){
            echo "地址不能为空";
        }
        $ak = "自己申请的ak";
        $url = "http://api.map.baidu.com/geocoding/v3/?";
        $params = array(
            'address'=>$area,
            'output'=>"json",
            'ak'=>$ak
        );
        $url = $url.http_build_query($params);
        $result = $this->curl_http($url,$params);
    return json_decode($result,true);
    }

2.根据经纬度获取地址

/**
     * [@getAddressByLatlng 根据经纬度获取地址]
     * @param $lat
     * @param $lng
     * @return string
     */
    public function getAddressByLatlng($lat,$lng)
    {
        $ak = "自己申请的ak";
        $address = '';
        if($lat && $lng)
        {
            $apiurl = 'http://api.map.baidu.com/geoconv/v1/?coords='.$lng.','.$lat.'&from=1&to=5&ak='.$ak;
            $file = file_get_contents($apiurl);
            $arrpoint = json_decode($file, true);
            $arr = $arrpoint['result'][0];
            $url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak='.$ak.'&output=json&coordtype=wgs84ll&location='.$arr['y'].','.$arr['x'];
            $content = file_get_contents($url);
            $place = json_decode($content,true);
            $address = $place['result']['formatted_address'];
        }
        return $address;
    }


Copyright © 2024 镇江小蚂蚁信息科技有限公司 All Rights Reserved.