本文整理汇总了Java中com.google.code.geocoder.model.GeocodeResponse.getStatus方法的典型用法代码示例。如果您正苦于以下问题:Java GeocodeResponse.getStatus方法的具体用法?Java GeocodeResponse.getStatus怎么用?Java GeocodeResponse.getStatus使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.code.geocoder.model.GeocodeResponse
的用法示例。
在下文中一共展示了GeocodeResponse.getStatus方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: locationToCoordinate
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
public GeocoderGeometry locationToCoordinate(String location) throws IOException {
GeocoderGeometry coordinate = null;
if (location != null && !location.isEmpty()) {
GeocoderRequest request = new GeocoderRequest();
request.setAddress(location);
GeocodeResponse response = geocoder.geocode(request);
if (response.getStatus() == GeocoderStatus.OK) {
List<GeocoderResult> results = response.getResults();
for (GeocoderResult result : results) {
GeocoderGeometry geometry = result.getGeometry();
coordinate = geometry;
break;
}
}
}
return coordinate;
}
示例2: coordinateToLocation
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
public String coordinateToLocation(GeocoderGeometry coordinate) throws IOException {
String location = null;
if (coordinate != null) {
GeocoderRequest request = new GeocoderRequest();
request.setLocation(coordinate.getLocation());
request.setBounds(coordinate.getBounds());
GeocodeResponse response = geocoder.geocode(request);
if (response.getStatus() == GeocoderStatus.OK) {
List<GeocoderResult> results = response.getResults();
for (GeocoderResult result : results) {
location = result.getFormattedAddress();
break;
}
}
}
return location;
}
示例3: getCoordinates
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
@Override
public synchronized Coordinates getCoordinates(final String address) throws GeocoderException {
ensureInitialized();
final GeocoderRequest request = new GeocoderRequestBuilder().setAddress(address).setLanguage("en").getGeocoderRequest();
final GeocodeResponse response = m_geocoder.geocode(request);
switch (response.getStatus()) {
case OK:
return new GoogleCoordinates(response.getResults().get(0));
case OVER_QUERY_LIMIT:
throw new TemporaryGeocoderException("Failed to get coordinates for " + address + " using the Google Geocoder. You have exceeded the daily usage limit.");
case ERROR:
case INVALID_REQUEST:
case REQUEST_DENIED:
case UNKNOWN_ERROR:
case ZERO_RESULTS:
default:
throw new GeocoderException("Failed to get coordinates for " + address + " using Google Geocoder. Response was: " + response.getStatus().toString());
}
}
示例4: getGeoPt
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
@Nullable
public static GeoPt getGeoPt(String addressStr) {
// TODO(avaliani): use an api key to avoid geocoding quota limits
final Geocoder geocoder = new Geocoder();
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder()
.setAddress(addressStr)
.setLanguage("en")
.getGeocoderRequest();
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
if (geocoderResponse.getStatus() == GeocoderStatus.OK) {
GeocoderResult firstResult = geocoderResponse.getResults().get(0);
return new GeoPt(
firstResult.getGeometry().getLocation().getLat().floatValue(),
firstResult.getGeometry().getLocation().getLng().floatValue());
} else {
log.log(GEOCODE_LOG_LEVEL,
"Geocoding failed: status=" + geocoderResponse.getStatus() + ", " +
"response=" + geocoderResponse);
// TODO(avaliani): Properly handle geopt encoding failures. Retrying in cases where
// the error is over quota.
return null;
}
}
示例5: extractGeoResult
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
protected void extractGeoResult(GeocodeResponse res, Exchange exchange) {
exchange.getIn().setHeader(GeoCoderConstants.STATUS, res.getStatus());
// should we include body
if (!endpoint.isHeadersOnly()) {
exchange.getIn().setBody(res);
}
if (res.getStatus() == GeocoderStatus.OK) {
exchange.getIn().setHeader(GeoCoderConstants.ADDRESS, res.getResults().get(0).getFormattedAddress());
// just grab the first element and its lat and lon
BigDecimal resLat = res.getResults().get(0).getGeometry().getLocation().getLat();
BigDecimal resLon = res.getResults().get(0).getGeometry().getLocation().getLng();
exchange.getIn().setHeader(GeoCoderConstants.LAT, resLat.toPlainString());
exchange.getIn().setHeader(GeoCoderConstants.LNG, resLon.toPlainString());
String resLatlng = resLat.toPlainString() + "," + resLon.toPlainString();
exchange.getIn().setHeader(GeoCoderConstants.LATLNG, resLatlng);
GeocoderAddressComponent country = getCountry(res);
if (country != null) {
exchange.getIn().setHeader(GeoCoderConstants.COUNTRY_SHORT, country.getShortName());
exchange.getIn().setHeader(GeoCoderConstants.COUNTRY_LONG, country.getLongName());
}
GeocoderAddressComponent city = getCity(res);
if (city != null) {
exchange.getIn().setHeader(GeoCoderConstants.CITY, city.getLongName());
}
}
}
示例6: getGeocode
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
static public SearchNode getGeocode(String location) {
if (location == null)
return null;
Geocoder geocoder = new Geocoder();
LatLngBounds bounds = new LatLngBounds(
new LatLng(new BigDecimal(BOUNDS_SOUTHWEST_LAT), new BigDecimal(BOUNDS_SOUTHWEST_LON)),
new LatLng(new BigDecimal(BOUNDS_NORTHEAST_LAT), new BigDecimal(BOUNDS_NORTHEAST_LON)));
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder()
.setAddress(location)
.setLanguage("de")
.setBounds(bounds)
.getGeocoderRequest();
GeocodeResponse geocoderResponse;
try {
geocoderResponse = geocoder.geocode(geocoderRequest);
if (geocoderResponse.getStatus() == GeocoderStatus.OK
& !geocoderResponse.getResults().isEmpty()) {
GeocoderResult geocoderResult =
geocoderResponse.getResults().iterator().next();
LatLng latitudeLongitude =
geocoderResult.getGeometry().getLocation();
// Only use first part of the address.
Scanner lineScanner = new Scanner(geocoderResult.getFormattedAddress());
lineScanner.useDelimiter(",");
return new SearchNode(
lineScanner.next(),
latitudeLongitude.getLat().doubleValue(),
latitudeLongitude.getLng().doubleValue());
}
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
示例7: getLatLongZip
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
private LatLong getLatLongZip( String zipcode ) {
final Geocoder geocoder = new Geocoder();
GeocoderRequest request = new GeocoderRequestBuilder().setAddress(zipcode).setLanguage("en").getGeocoderRequest();
GeocodeResponse response = geocoder.geocode(request);
LatLong ll = null;
if( response != null && response.getStatus() == GeocoderStatus.OK ) {
for( GeocoderResult geoResult : response.getResults() ) {
LatLng googLatLong = geoResult.getGeometry().getLocation();
ll = new LatLong( googLatLong.getLat().floatValue(), googLatLong.getLng().floatValue() );
}
}
return ll;
}
示例8: geocode
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
public static GeoPt geocode(String address) throws GeocodeFailedException, GeocodeNotFoundException {
// The underlying geocoder library fails if it receives an empty address.
if (address.trim().isEmpty()) {
throw new GeocodeFailedException(address, GeocoderStatus.INVALID_REQUEST);
}
String cacheKey = "geocode:" + address;
CacheProxy cache = new CacheProxy();
Object cacheEntry = cache.get(cacheKey);
// Backwards comparison -- cacheEntry may be null
if (INVALID_LOCATION.equals(cacheEntry)) {
throw new GeocodeNotFoundException(address);
}
if (cacheEntry != null) {
return (GeoPt) cacheEntry;
}
Geocoder geocoder = new Geocoder(); // TODO: Use Maps for Business?
GeocoderRequest request = new GeocoderRequestBuilder().setAddress(address).getGeocoderRequest();
GeocodeResponse response = geocoder.geocode(request);
GeocoderStatus status = response.getStatus();
if (status == GeocoderStatus.ZERO_RESULTS) {
cache.put(cacheKey, INVALID_LOCATION);
throw new GeocodeNotFoundException(address);
} else if (status != GeocoderStatus.OK) {
// We've encountered a temporary error, so return without caching the missing point.
throw new GeocodeFailedException(address, response.getStatus());
} else {
LatLng location = response.getResults().get(0).getGeometry().getLocation();
GeoPt geoPt = new GeoPt(location.getLat().floatValue(), location.getLng().floatValue());
cache.put(cacheKey, geoPt);
return geoPt;
}
}
示例9: setGeoLocation
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
/**
* I am currently calling the Google Geolocation API.
* The limit for this api is 2400 per day.
*
* @author lancepoehler
* @throws InterruptedException
*
*/
public static boolean setGeoLocation(Location location) throws InterruptedException {
LOG.debug("Calling Google GeoLocation API");
try {
final Geocoder geocoder = new Geocoder();
GeocoderRequest geocoderRequest;
GeocodeResponse geocoderResponse=null;
//This will fail if it is not synchronized. The classes are not threadsafe
synchronized(GoogleGeocoderApiHelper.class) {
try {
final String address = buildAddressString(location);
if(address==null || StringUtils.isBlank(address)) {
LOG.error("Geocoder called with blank address, aborting call");
return false;
}
geocoderRequest = new GeocoderRequestBuilder()
.setAddress(address)
.setLanguage("en")
.getGeocoderRequest();
geocoderResponse = geocoder.geocode(geocoderRequest);
} catch(Throwable oops) {
LOG.error("Error calling geocoder service", oops);
return false;
}
}
if (geocoderResponse==null ||
geocoderResponse.getStatus()==null ||
geocoderResponse.getStatus() != GeocoderStatus.OK) {
return false;
}
String fullAddress = geocoderResponse.getResults().get(0).getFormattedAddress();
if (geocoderResponse.getResults().get(0).isPartialMatch())
{
if (!fullAddress.toLowerCase().contains(location.city.toLowerCase()) ||
!fullAddress.toLowerCase().contains(", "+location.state.toLowerCase())) {
return false;
}
} else if (StringUtils.isNotBlank(location.streetAddress) && //This will check for crazy addresses that are not returned with an address
fullAddress.toLowerCase().startsWith(location.city.toLowerCase() + ", " + location.state.toLowerCase())) {
return false;
}
GeoLoc geoLoc = new GeoLoc();
geoLoc.latitude = geocoderResponse.getResults().get(0).getGeometry().getLocation().getLat();
geoLoc.longitude = geocoderResponse.getResults().get(0).getGeometry().getLocation().getLng();
location.geoLoc = (geoLoc);
location.formattedAddress = fullAddress;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例10: testGeocoderComponent
import com.google.code.geocoder.model.GeocodeResponse; //导入方法依赖的package包/类
@Test
public void testGeocoderComponent() throws Exception {
HttpClientConfigurer configurer = new GeocoderHttpClientConfigurer();
initialContext.bind("httpClientConfigurer", configurer);
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("geocoder:address:London, England?httpClientConfigurer=#httpClientConfigurer");
}
});
camelctx.start();
try {
// Geocoder API is sometimes flaky so retry the request if necessary
GeocodeResponse result = null;
int count = 0;
while (count < 5) {
ProducerTemplate template = camelctx.createProducerTemplate();
result = template.requestBody("direct:start", null, GeocodeResponse.class);
Assert.assertNotNull("Geocoder response is null", result);
// Skip further testing if we exceeded the API rate limit
GeocoderStatus status = result.getStatus();
Assume.assumeFalse("Geocoder API rate limit exceeded", status.equals(OVER_QUERY_LIMIT));
if (status.equals(OK)) {
break;
}
Thread.sleep(1000);
count++;
}
List<GeocoderResult> results = result.getResults();
Assert.assertNotNull("Geocoder results is null", result);
Assert.assertEquals("Expected 1 GeocoderResult to be returned", 1, results.size());
LatLng location = results.get(0).getGeometry().getLocation();
Assert.assertEquals("51.5073509", location.getLat().toPlainString());
Assert.assertEquals("-0.1277583", location.getLng().toPlainString());
} finally {
camelctx.stop();
initialContext.unbind("httpClientConfigurer");
}
}