/** * Geocodes the address using the Google Maps XML webservice, which has more information. * Unknown values will be set to NULL. * @todo Change to SimpleXML * @param EGMapClient $gmap_client * @return integer $accuracy * @author Fabrice Bernhard * @since 2010-12-22 modified by utf8_encode removed Antonio Ramirez * @since 2011-04-21 Matt Cheale Updated to parse API V3 JSON response. */ public function geocodeXml($gmap_client) { $raw_data = $gmap_client->getGeocodingInfo($this->getRawAddress(), 'xml'); $xml = simplexml_load_string($raw_data); if ('OK' != $xml->status) { return false; } foreach ($xml->result->address_component as $component) { $longName = (string) $component->long_name; $shortName = (string) $component->short_name; foreach ($component->type as $type) { switch ($type) { case 'street_address': case 'route': $this->geocoded_street = $longName; break; case 'country': $this->geocoded_country = $longName; $this->geocoded_country_code = $shortName; break; case 'locality': $this->geocoded_city = $shortName; break; case 'postal_code': $this->geocoded_postal_code = $longName; break; default: // Do nothing } } } $this->lat = (double) $xml->result->geometry->location->lat; $this->lng = (double) $xml->result->geometry->location->lng; $this->accuracy = (string) $xml->result->geometry->location_type; return $this->accuracy; }
/** * Geocodes the address using the Google Maps XML webservice, which has more information. * Unknown values will be set to NULL. * @todo Change to SimpleXML * @param EGMapClient $gmap_client * @return integer $accuracy * @author Fabrice Bernhard * @since 2010-12-22 modified by utf8_encode removed Antonio Ramirez */ public function geocodeXml($gmap_client) { $raw_data = $gmap_client->getGeocodingInfo($this->getRawAddress(), 'xml'); $p = xml_parser_create('UTF-8'); xml_parse_into_struct($p, $raw_data, $vals, $index); xml_parser_free($p); if ($vals[$index['CODE'][0]]['value'] != 200) { return false; } $coordinates = $vals[$index['COORDINATES'][0]]['value']; list($this->lng, $this->lat) = explode(',', $coordinates); $this->accuracy = $vals[$index['ADDRESSDETAILS'][0]]['attributes']['ACCURACY']; // We voluntarily silence errors, the values will still be set to NULL if the array indexes are not defined // @author Fabrice Bernard @($this->geocoded_address = $vals[$index['ADDRESS'][0]]['value']); @($this->geocoded_street = $vals[$index['THOROUGHFARENAME'][0]]['value']); @($this->geocoded_postal_code = $vals[$index['POSTALCODENUMBER'][0]]['value']); @($this->geocoded_country = $vals[$index['COUNTRYNAME'][0]]['value']); @($this->geocoded_country_code = $vals[$index['COUNTRYNAMECODE'][0]]['value']); @($this->geocoded_city = $vals[$index['LOCALITYNAME'][0]]['value']); if (empty($this->geocoded_city)) { @($this->geocoded_city = $vals[$index['SUBADMINISTRATIVEAREANAME'][0]]['value']); } if (empty($this->geocoded_city)) { @($this->geocoded_city = $vals[$index['ADMINISTRATIVEAREANAME'][0]]['value']); } return $this->accuracy; }