GlisWeb framework
_mapquest.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
23  function mapquestGetCoords( $key, $civico, $indirizzo, $citta, $stato, $url = 'http://www.mapquestapi.com/geocoding/v1/address' ) {
24 
25  if( ! empty( $key ) ) {
26 
27  $result = restCall(
28  $url . '?key=' . $key,
30  array(
31  'location' => $civico . ', ' . $indirizzo . ', ' . $citta . ', ' . $stato
32  )
33  );
34 
35  // print_r( $result );
36 
37  writeToFile( $key . PHP_EOL . print_r( $result, true ), 'var/log/mapquest/' . string2urlRewrite( $civico . ', ' . $indirizzo . ', ' . $citta . ', ' . $stato ) . '.log' );
38 
39  // TODO qui loggare in caso di problemi
40 
41  foreach( $result['results'][0]['locations'] as $location ) {
42  if( $location['geocodeQuality'] == 'POINT' || $location['geocodeQuality'] == 'ADDRESS' || $location['geocodeQuality'] == 'STREET' ) {
43 # if( $location['geocodeQuality'] == 'POINT' || $location['geocodeQuality'] == 'ADDRESS' ) {
44  $qResults[ substr( $location['geocodeQualityCode'], 2, 3 ) ] = $location;
45  }
46  }
47 
48  if( ! empty( $qResults ) ) {
49 
50  ksort( $qResults );
51 
52  // print_r( $qResults );
53 
54  $bResult = array_shift( $qResults );
55 
56  // return $result['results'][0]['locations'][0]['latLng'];
57  return array_merge( $bResult['latLng'], array( 'cap' => $bResult['postalCode'] ) );
58 
59  } else {
60 
61  return NULL;
62 
63  }
64 
65  } else {
66 
67  logWrite( 'nessuna chiave Mapquest impostata per lo stage corrente', 'geocode', LOG_CRIT );
68 
69  return false;
70 
71  }
72 
73  }
74 
83  function mapquestGetCachedCoords( $m, $key, $civico, $indirizzo, $citta, $stato, $t = MEMCACHE_DEFAULT_TTL, $url = 'http://www.mapquestapi.com/geocoding/v1/address' ) {
84 
85  // calcolo la chiave della query
86  $k = 'MAPQUEST_' . md5( $civico . $indirizzo . $citta . $stato );
87 
88  // cerco il valore in cache
89  $r = memcacheRead( $m, $k );
90 
91  // se il valore non รจ stato trovato
92  if( empty( $r ) || $t === false ) {
93  $r = mapquestGetCoords( $key, $civico, $indirizzo, $citta, $stato, $url );
94  memcacheWrite( $m, $k, $r, $t );
95  }
96 
97  // restituisco il risultato
98  return $r;
99 
100  }
101 
102 ?>
mapquestGetCachedCoords( $m, $key, $civico, $indirizzo, $citta, $stato, $t=MEMCACHE_DEFAULT_TTL, $url='http://www.mapquestapi.com/geocoding/v1/address')
string2urlRewrite( $t)
sostituisce i caratteri in modo da rendere una stringa utilizzabile in un url
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
restCall( $url, $method=METHOD_GET, $data=NULL, $datatype=MIME_APPLICATION_JSON, $answertype=MIME_APPLICATION_JSON, &$status=NULL, $headers=array(), $user=NULL, $pasw=NULL, &$error=NULL)
esegue una chiamata REST
Definition: _rest.tools.php:23
writeToFile( $t, $f, &$e=NULL)
scrive una stringa su un file
memcacheRead( $conn, $key)
const METHOD_POST
Definition: _config.php:282
$r
Definition: _osm.php:25
mapquestGetCoords( $key, $civico, $indirizzo, $citta, $stato, $url='http://www.mapquestapi.com/geocoding/v1/address')
memcacheWrite( $conn, $key, $data, $ttl=MEMCACHE_DEFAULT_TTL, $seed=MEMCACHE_UNIQUE_SEED)
const MEMCACHE_DEFAULT_TTL
Definition: _045.cache.php:16