地址转经纬度(百度)function

function addr2LatLng($addr){
    //http://218.202.235.66:8887/HNGeocodSolve/GeocodServlet?province=上海市&city=上海市&geocodedata=上海市徐汇区钦州南路81号&type=1
    //       province、city参数:为省份、城市名称,此处默认全部传入上海市即可。(参数值需要经过utf-8编码后传递)
    //       type:编码类型,默认传1
    //     geocodedata:待编码的文本描述,可以是名称或者地址描述。(参数值需要经过utf-8编码后传递)
        $addr = str_replace(" ","",$addr);
        $url = "http://218.202.235.66:8887/HNGeocodSolve/GeocodServlet?province=上海市&city=上海市&geocodedata=" . $addr . "&type=1";
        $httpres = http($url, false, 'GET');
        $xmlarr = xml_to_array($httpres);
        //dump($xmlarr);
        if (count($xmlarr['App_Results']) >= 1) {
            $data['lat'] = $xmlarr['App_Results']['Coordinate']['X'];
            $data['lng'] = $xmlarr['App_Results']['Coordinate']['Y'];
        }
        return $data;
}
/*  HTTP 请求  post/get*/
function http($url, $data, $type)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//    $this_header = array(
//        "Content-Type:text/html; charset=utf-8"
//    );

    //curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);

    if ($type == 'POST') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function xml_to_array($xml)
{
    $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
    if (preg_match_all($reg, $xml, $matches)) {
        $count = count($matches[0]);
        $arr = array();
        for ($i = 0; $i < $count; $i++) {
            $key = $matches[1][$i];
            $val = xml_to_array($matches[2][$i]);  // 递归
            if (array_key_exists($key, $arr)) {
                if (is_array($arr[$key])) {
                    if (!array_key_exists(0, $arr[$key])) {
                        $arr[$key] = array($arr[$key]);
                    }
                } else {
                    $arr[$key] = array($arr[$key]);
                }
                $arr[$key][] = $val;
            } else {
                $arr[$key] = $val;
            }
        }
        return $arr;
    } else {
        return $xml;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值