地址转经纬度(百度)function

来源:互联网 发布:ip域名查询 编辑:程序博客网 时间:2024/06/15 17:00
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;    }}
原创粉丝点击