1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| $url='http://maps.google.com/maps/api/geocode/xml?address='.rawurlencode(htmlentities($_PARAMS['adresse'])).'&sensor=false';
$cachename=$_CONFIG['abs_path'].'data/cache/'.md5($url);
if(!is_file($cachename)){
$s = curl_init();
sleep(1);
googlog($url.'&key=ABQIAAAAEMxCg9BDREz4rWjsNytwWBQf1n1DndsbAgStce_U6lWS2QvklxRib6CIKt7fe97lJ0L_MxJQAbX5_A');
curl_setopt($s,CURLOPT_URL,$url.'&key=ABQIAAAAEMxCg9BDREz4rWjsNytwWBQf1n1DndsbAgStce_U6lWS2QvklxRib6CIKt7fe97lJ0L_MxJQAbX5_A');
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
$aa = curl_exec($s);
curl_close($s);
$docx = new DOMDocument('1.0', 'ISO-8859-1');
if($aa==''){
error('googlebug','Empty XML returned');
}
$docx->loadXML($aa);
$fp=fopen($cachename,'w');
fwrite($fp,$aa);
fclose($fp);
}else{
$docx =new DOMDocument('1.0', 'ISO-8859-1');
$docx->load($cachename);
}
if($docx->getElementsByTagName('status')->item(0)->nodeValue!='OK'){
error('googlebug',$docx->getElementsByTagName('status')->item(0)->nodeValue);
}
$loc=$docx->getElementsByTagName('GeocodeResponse')->item(0)->getElementsByTagName('location')->item(0);
$location=$XML->addNode('location');
foreach($docx->getElementsByTagName('address_component') as $compo){
$type=$compo->getElementsByTagName('type')->item(0)->nodeValue;
if($type=='locality'){
$location->addAttribute('ville',utf8_decode($compo->getElementsByTagName('long_name')->item(0)->nodeValue));
}else if($type=='postal_code'){
$location->addAttribute('cp',$compo->getElementsByTagName('long_name')->item(0)->nodeValue);
}else if($type=='street_number'){
$num=$compo->getElementsByTagName('long_name')->item(0)->nodeValue;
}else if($type=='route'){
$addr=utf8_decode($compo->getElementsByTagName('long_name')->item(0)->nodeValue);
}
}
$location->addAttribute('lat',$loc->getElementsByTagName('lat')->item(0)->nodeValue);
$location->addAttribute('lng',$loc->getElementsByTagName('lng')->item(0)->nodeValue);
$location->addAttribute('formatted_address',utf8_decode($docx->getElementsByTagName('formatted_address')->item(0)->nodeValue));
if(!isset($addr)){
error('googlebug','Unknown route');
}
if(!isset($num)){
$location->addAttribute('address',$addr);
}else{
$location->addAttribute('address',$num.' '.$addr);
}
?> |
Partager