0

我正在使用 google maps api v2 并将坐标发送到我的程序。地图上的本地化工作正常,但我从来没有得到地址......

这是我的课:

class reverseGeoCoding {
    //put your code here
    private $result;
    private $latitude=null;
    private $longitude=null;
    private $adresse=null;
    private $config;
    public $contents;

    function xml2ary(&$string) {
        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parse_into_struct($parser, $string, $vals, $index);
        xml_parser_free($parser);

        $mnary=array();
        $ary=&$mnary;
        foreach ($vals as $r) {
            $t=$r['tag'];
            if ($r['type']=='open') {
                if (isset($ary[$t])) {
                    if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
                    $cv=&$ary[$t][count($ary[$t])-1];
                } else $cv=&$ary[$t];
                if (isset($r['attributes'])) {
                    foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
                }
                $cv['_c']=array();
                $cv['_c']['_p']=&$ary;
                $ary=&$cv['_c'];

            } elseif ($r['type']=='complete') {
                if (isset($ary[$t])) { // same as open
                    if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
                    $cv=&$ary[$t][count($ary[$t])-1];
                } else $cv=&$ary[$t];
                if (isset($r['attributes'])) {
                    foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
                }
                $cv['_v']=(isset($r['value']) ? $r['value'] : '');

            } elseif ($r['type']=='close') {
                $ary=&$ary['_p'];
            }
        }

        $this->del_p($mnary);
        return $mnary;
    }

    // _Internal: Remove recursion in result array
    function del_p(&$ary) {
        foreach ($ary as $k=>$v) {
            if ($k==='_p') unset($ary[$k]);
            elseif (is_array($ary[$k])) $this->del_p($ary[$k]);
        }
    }

    // Array to XML
    function ary2xml($cary, $d=0, $forcetag='') {
        $res=array();
        foreach ($cary as $tag=>$r) {
            if (isset($r[0])) {
                $res[]=ary2xml($r, $d, $tag);
            } else {
                if ($forcetag) $tag=$forcetag;
                $sp=str_repeat("\t", $d);
                $res[]="$sp<$tag";
                if (isset($r['_a'])) {
                    foreach ($r['_a'] as $at=>$av) $res[]=" $at=\"$av\"";
                }
                $res[]=">".((isset($r['_c'])) ? "\n" : '');
                if (isset($r['_c'])) $res[]=ary2xml($r['_c'], $d+1);
                elseif (isset($r['_v'])) $res[]=((!is_numeric($r['_v'])&&$r['_v']) ? '<![CDATA[' : false).$r['_v'].((!is_numeric($r['_v'])&&$r['_v']) ? ']]>' : false);
                $res[]=(isset($r['_c']) ? $sp : '')."</$tag>\n";
            }

        }
        return implode('', $res);
    }



    function formatGmapXML($xml,$returnXML=false,$error=false) {
        $tmp = array();
        $tmp['request']= $xml['kml']['_c']['Response']['_c']['name']['_v'];
        $tmp['status']= $xml['kml']['_c']['Response']['_c']['Status']['_c']['code']['_v'];

        // check our Response code to ensure success
        if($tmp['status']=='200') {

            $tmp['addressFull'] = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['address']['_v'];
            $tmp['accuracy']    = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['AddressDetails']['_a']['Accuracy'];
            $tmp['country']     = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['AddressDetails']['_c']['Country']['_c']['CountryName']['_v'];
            $tmp['state']       = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['AdministrativeAreaName']['_v'];
            $tmp['suburb']      = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['Locality']['_c']['LocalityName']['_v'];
            $tmp['address']     = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['Locality']['_c']['Thoroughfare']['_c']['ThoroughfareName']['_v'];
            $tmp['postcode']    = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['Locality']['_c']['PostalCode']['_c']['PostalCodeNumber']['_v'];
            $tmp['coordinates'] = $xml['kml']['_c']['Response']['_c']['Placemark']['_c']['Point']['_c']['coordinates']['_v'];
            if($tmp['addressFull']==null) {
                $tmp['addressFull'] = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['address']['_v'];
                $tmp['accuracy']    = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['AddressDetails']['_a']['Accuracy'];
                $tmp['country']     = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['AddressDetails']['_c']['Country']['_c']['CountryName']['_v'];
                $tmp['state']       = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['AdministrativeAreaName']['_v'];
                $tmp['suburb']      = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['Locality']['_c']['LocalityName']['_v'];
                $tmp['address']     = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['Locality']['_c']['Thoroughfare']['_c']['ThoroughfareName']['_v'];
                $tmp['postcode']    = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['AddressDetails']['_c']['Country']['_c']['AdministrativeArea']['_c']['Locality']['_c']['PostalCode']['_c']['PostalCodeNumber']['_v'];
                $tmp['coordinates'] = $xml['kml']['_c']['Response']['_c']['Placemark'][0]['_c']['Point']['_c']['coordinates']['_v'];
            }
            $latlng = explode(',',$tmp['coordinates']);

            $tmp['latitude']    = $latlng[1];
            $tmp['longitude']   = $latlng[0];

        }

        // optional outputs
        if($error)      $tmp['error'] = $error;
        if($returnXML)  $tmp['xml'] = ary2xml($xml);

        return $tmp;

    }



    /*
$q=$_GET['q'];
$latitude=$_GET['latitude'];
$longitude=$_GET['longitude'];
    */

    function reverseGeoCoding() {
        $this->config=new configInt();

    }
    function setLongitude($longitude) {
        $this->longitude=$longitude;
    }
    function setLatitude($latitude) {
        $this->latitude=$latitude;
    }
    function setadresse($adresse) {
        $this->adresse=$adresse;
    }
    function getLongitude() {
        return $this->longitude;
    }
    function getLatitude() {
        return $this->latitude;
    }
    function getadresse() {

        return $this->adresse;
    }

    function exec() {

        if($this->longitude && $this->latitude ) {
            $ch = curl_init("http://maps.google.com/maps/geo?output=xml&ll={$this->latitude},{$this->longitude}&key={$this->config->clefGoogle}");

        }else {

            $adresse=rawurlencode($this->adresse);
            $ch = curl_init("http://maps.google.com/maps/geo?output=xml&q={$adresse}&key={$this->config->clefGoogle}");


        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        //curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_setopt($ch, CURLOPT_VERBOSE, false);
        $this->contents=curl_exec($ch);

        $this->contents=$this->xml2ary($this->contents);

        $this->result=$this->formatGmapXML($this->contents,false);
        curl_close($ch);

        if($this->longitude && $this->latitude ) {

            $this->adresse=utf8_decode($this->result['addressFull']);

        }else {
            $this->latitude=$this->result['latitude'];
            $this->longitude=$this->result['longitude'];
        }

        return $this->result;

    }
}
?>

我在我的程序中使用 getAdress 将地址保存在数据库中,但即使坐标正确,它似乎也无法正常工作......

4

2 回答 2

1

试试这个:

$result = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true'));   
echo $result->results[0]->formatted_address;
于 2013-04-24T09:22:44.797 回答
0
function getaddress($lat, $lng) {

    $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . trim($lat) . ',' . trim($lng) . '&sensor=false';

    $json = @file_get_contents($url);
    $data = json_decode($json);
    $status = $data->status;
    if ($status == "OK")
        return $data->results[0]->formatted_address;
    else
        return false;
}
于 2019-06-12T09:25:27.280 回答