PHP教程

php地址与经纬度之间转换

本文主要是介绍php地址与经纬度之间转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 /**
     * 根据地址获取经纬度
     */
    public static function getLoaction($local)
    {
        $address = urlencode($local[0].$local[1]);
        $url = "https://apis.map.qq.com/jsapi?qt=geoc&addr={$address}&key=TU5BZ-MKD3W-L43RW-O3ZBW-GWMZK-QBB25&output=jsonp&pf=jsapi&ref=jsapi";
        $res = file_get_contents($url);
        $content = iconv("GBK", "UTF-8//IGNORE", $res);
        $array = @json_decode($content,true);
        $data['lat'] = $array['detail']['pointy'];
        $data['lon'] = $array['detail']['pointx'];
        return $data;
    }

    /**
     * 根据经纬度获取地址
     * @param $ip
     * @param string $memberInfo
     * @return array
     */
    private static function GetIpLookup($ip)
    {
        $url = 'https://apis.map.qq.com/jsapi?qt=rgeoc&lnglat='.urlencode($ip).'&key=TU5BZ-MKD3W-L43RW-O3ZBW-GWMZK-QBB25&output=jsonp&pf=jsapi&ref=jsapi';
        $res = file_get_contents($url);
        $content = iconv("GBK", "UTF-8//IGNORE", $res);
        $array = @json_decode($content,true);
        if (!is_array($array)) {
            return [];
        }
        $addr_info = [];
        foreach ($array['detail']['poilist'] as $k => $v) {
            if (!empty($v['addr_info']['c']) && !empty($v['pointx'])) {
                $addr_info[$k]['pointx'] = $v['pointx'];
                $addr_info[$k]['pointy'] = $v['pointy'];
            }
        }
        if (empty($addr_info)) {
            foreach ($array['detail']['results'] as $k => $v) {
                $addr_info[$k]['pointx'] = $v['pointx'];
                $addr_info[$k]['pointy'] = $v['pointy'];
                break;
            }
        }
        return $addr_info;
    }

 

这篇关于php地址与经纬度之间转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!