コード例 #1
0
 function test_location_fields()
 {
     $blank_location = GeoMashupDB::blank_location();
     $this->assertObjectHasAttribute('lat', $blank_location);
     $this->assertTrue(GeoMashupDB::are_any_location_fields_empty($blank_location));
     $test_location = $this->get_nv_test_location();
     $this->assertTrue(GeoMashupDB::are_any_location_fields_empty($test_location));
     $this->assertFalse(GeoMashupDB::are_any_location_fields_empty($test_location, array('lat', 'lng')));
 }
コード例 #2
0
 public function reverse_geocode($lat, $lng)
 {
     global $geo_mashup_options;
     if (!is_numeric($lat) or !is_numeric($lng)) {
         // Bad Request
         return new WP_Error('bad_reverse_geocode_request', __('Reverse geocoding requires numeric coordinates.', 'GeoMashup'));
     }
     $status = null;
     $url = 'http://api.geonames.org/countrySubdivisionJSON?style=FULL&username='******'&lat=' . urlencode($lat) . '&lng=' . urlencode($lng);
     $response = $this->http->get($url, $this->request_params);
     if (is_wp_error($response)) {
         return $response;
     }
     $status = $response['response']['code'];
     $data = json_decode($response['body']);
     if (empty($data) or !empty($data->status)) {
         return array();
     }
     $location = GeoMashupDB::blank_location();
     $location->lat = $lat;
     $location->lng = $lng;
     if (!empty($data->countryCode)) {
         $location->country_code = $data->countryCode;
     }
     if (!empty($data->adminCode1)) {
         $location->admin_code = $data->adminCode1;
     }
     // Look up more things, postal code, locality or address in US
     if ('US' == $location->country_code and GeoMashupDB::are_any_location_fields_empty($location, array('address', 'locality_name', 'postal_code'))) {
         $url = 'http://api.geonames.org/findNearestAddressJSON?style=FULL&username='******'&lat=' . urlencode($lat) . '&lng=' . urlencode($lat);
         $response = $this->http->get($url, $this->request_params);
         if (!is_wp_error($response)) {
             $status = $response['response']['code'];
             $data = json_decode($response['body']);
             if (!empty($data->address)) {
                 $address_parts = array();
                 if (!empty($data->address->street)) {
                     $address_parts[] = (empty($data->address->streetNumber) ? '' : $data->address->streetNumber . ' ') . $data->address->street;
                 }
                 if (!empty($data->address->adminName1)) {
                     $address_parts[] = $data->address->adminName1;
                 }
                 if (!empty($data->address->postalcode)) {
                     $address_parts[] = $data->address->postalcode;
                 }
                 $address_parts[] = $data->address->countryCode;
                 $location->address = implode(', ', $address_parts);
                 $location->locality_name = $data->address->placename;
                 $location->postal_code = $data->address->postalcode;
             }
         }
     }
     if (GeoMashupDB::are_any_location_fields_empty($location, array('address', 'locality_name', 'postal_code'))) {
         // Just look for a postal code
         $url = 'http://api.geonames.org/findNearbyPostalCodesJSON?username='******'&maxRows=1&lat=' . urlencode($lat) . '&lng=' . urlencode($lng);
         $response = $this->http->get($url, $this->request_params);
         if (!is_wp_error($response)) {
             $status = $response['response']['code'];
             $data = json_decode($response['body']);
             if (!empty($data->postalCodes)) {
                 $postal_code = $data->postalCodes[0];
                 $admin_name = empty($postal_code->adminName1) ? '' : $postal_code->adminName1;
                 $location->address = $postal_code->placeName . ', ' . $admin_name . ', ' . $postal_code->postalCode . ', ' . $postal_code->countryCode;
                 $location->locality_name = $postal_code->placeName;
                 $location->postal_code = $postal_code->postalCode;
             }
         }
     }
     return array($location);
 }