/** * Returns a Lat and Lng from an Address using Google Geocoder API. It does not require any Google API Key * originially based on allow_url_fopen-scripty by Abdullah Rubiyath */ function lmm_getLatLng($address) { global $locale; $protocol_handler = substr($locale, 0, 2) == 'zh' ? 'http' : 'https'; //info: conditional ssl loading for Google js (performance issues in China) $address_to_geocode = lmm_accent_folding($address); $url = $protocol_handler . '://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address_to_geocode); $xml_raw = wp_remote_get($url, array('sslverify' => false, 'timeout' => 10)); $xml = simplexml_load_string($xml_raw['body']); $response = array(); $statusCode = $xml->status; if ($statusCode != false && $statusCode != NULL) { if ($statusCode == 'OK') { $latDom = $xml->result[0]->geometry->location->lat; $lonDom = $xml->result[0]->geometry->location->lng; $addressDom = $xml->result[0]->formatted_address; if ($latDom != NULL) { $response = array('success' => true, 'lat' => $latDom, 'lon' => $lonDom, 'address' => $addressDom); return $response; } } else { if ($statusCode == 'OVER_QUERY_LIMIT') { //info: wait 1.5sec and try again once usleep(1500000); $xml_raw = wp_remote_get($url, array('sslverify' => false, 'timeout' => 10)); $xml = simplexml_load_string($xml_raw['body']); $response = array(); $statusCode = $xml->status; if ($statusCode != false && $statusCode != NULL) { if ($statusCode == 'OK') { $latDom = $xml->result[0]->geometry->location->lat; $lonDom = $xml->result[0]->geometry->location->lng; $addressDom = $xml->result[0]->formatted_address; if ($latDom != NULL) { $response = array('success' => true, 'lat' => $latDom, 'lon' => $lonDom, 'address' => $addressDom); return $response; } } } } } } $response = array('success' => false, 'message' => $statusCode); return $response; }
/** * Returns a Lat and Lng from an Address using Google Geocoder API. It does not require any Google API Key * originially based on allow_url_fopen-scripty by Abdullah Rubiyath */ function lmm_getLatLng($address) { $address_to_geocode = lmm_accent_folding($address); $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address_to_geocode) . '&sensor=false'; $xml_raw = wp_remote_get($url, array('sslverify' => false, 'timeout' => 10)); $xml = simplexml_load_string($xml_raw['body']); $response = array(); $statusCode = $xml->status; if ($statusCode != false && $statusCode != NULL) { if ($statusCode == 'OK') { $latDom = $xml->result[0]->geometry->location->lat; $lonDom = $xml->result[0]->geometry->location->lng; $addressDom = $xml->result[0]->formatted_address; if ($latDom != NULL) { $response = array('success' => true, 'lat' => $latDom, 'lon' => $lonDom, 'address' => $addressDom); return $response; } } else { if ($statusCode == 'OVER_QUERY_LIMIT') { //info: wait 1.5sec and try again once usleep(1500000); $xml_raw = wp_remote_get($url, array('sslverify' => false, 'timeout' => 10)); $xml = simplexml_load_string($xml_raw['body']); $response = array(); $statusCode = $xml->status; if ($statusCode != false && $statusCode != NULL) { if ($statusCode == 'OK') { $latDom = $xml->result[0]->geometry->location->lat; $lonDom = $xml->result[0]->geometry->location->lng; $addressDom = $xml->result[0]->formatted_address; if ($latDom != NULL) { $response = array('success' => true, 'lat' => $latDom, 'lon' => $lonDom, 'address' => $addressDom); return $response; } } } } } } $response = array('success' => false, 'message' => $statusCode); return $response; }