本文整理汇总了Java中com.google.code.geocoder.Geocoder.geocode方法的典型用法代码示例。如果您正苦于以下问题:Java Geocoder.geocode方法的具体用法?Java Geocoder.geocode怎么用?Java Geocoder.geocode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.code.geocoder.Geocoder
的用法示例。
在下文中一共展示了Geocoder.geocode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateGeoData
import com.google.code.geocoder.Geocoder; //导入方法依赖的package包/类
private void updateGeoData(LocationRecord locationRec) {
if (!keepManuallyUpdatedGeoData) {
try {
String address = locationRec.getAddress() + "," + (locationRec.getZip() != null ? locationRec.getZip() : "")
+ " " + locationRec.getCity() + "," + locationRec.getCountry();
final Geocoder geocoder = new Geocoder();
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(address).getGeocoderRequest();
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
if (geocoderResponse.getResults().size() != 1) {
log.error("Failed to get one result for " + address + " got " + geocoderResponse.getResults());
}
for (GeocoderResult gr : geocoderResponse.getResults()) {
LatLng latLng = gr.getGeometry().getLocation();
locationRec.setGeoLat(latLng.getLat().doubleValue());
locationRec.setGeoLng(latLng.getLng().doubleValue());
}
} catch (IOException e) {
log.error("Error accessing Geocoder API", e);
}
}
}
示例2: getGeoPt
import com.google.code.geocoder.Geocoder; //导入方法依赖的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;
}
}
示例3: testGeoCoder
import com.google.code.geocoder.Geocoder; //导入方法依赖的package包/类
@Test
public void testGeoCoder() throws Exception {
GeoCoderEndpoint endpoint = context.getEndpoint(
"geocoder:address:current?headersOnly=true&proxyHost=localhost&proxyPort=3128&proxyAuthMethod=Basic&proxyAuthUsername=proxy&proxyAuthPassword=proxy",
GeoCoderEndpoint.class);
Geocoder geocoder = endpoint.createGeocoder();
GeocoderRequest req = new GeocoderRequest();
req.setLocation(new LatLng("45.4643", "9.1895"));
GeocodeResponse res = geocoder.geocode(req);
log.info("Response {} ", res);
}
示例4: getGeocode
import com.google.code.geocoder.Geocoder; //导入方法依赖的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;
}
示例5: getGeoByPubLatLng
import com.google.code.geocoder.Geocoder; //导入方法依赖的package包/类
private static void getGeoByPubLatLng(Pub pub, final Geocoder geocoder) {
GeocoderRequest request = new GeocoderRequestBuilder()
.setAddress(pub.getLocal())
.setLocation(new LatLng(new BigDecimal(pub.getLat()), new BigDecimal(pub.getLng())))
.setLanguage("en")
.getGeocoderRequest();
GeocodeResponse response = geocoder.geocode(request);
if (response.getStatus().equals(GeocoderStatus.OK)) {
List<GeocoderResult> results = response.getResults();
for (GeocoderResult geoResult : results) {
List<GeocoderAddressComponent> addressComponents = geoResult.getAddressComponents();
for (GeocoderAddressComponent address : addressComponents) {
if (address.getTypes().contains("locality")) {
pub.setCity(address.getLongName());
}
if (address.getTypes().contains("administrative_area_level_1")) {
pub.setState(address.getLongName());
}
if (address.getTypes().contains("country")) {
pub.setCountry(address.getLongName());
}
}
}
} else {
log.info(response.getStatus().name());
}
}
示例6: getGeoByPubAddress
import com.google.code.geocoder.Geocoder; //导入方法依赖的package包/类
private static void getGeoByPubAddress(Pub pub, final Geocoder geocoder) {
GeocoderRequest request = new GeocoderRequestBuilder()
.setAddress(pub.getLocal())
.setLanguage("en")
.getGeocoderRequest();
GeocodeResponse response = geocoder.geocode(request);
if (response.getStatus().equals(GeocoderStatus.OK)) {
List<GeocoderResult> results = response.getResults();
for (GeocoderResult geoResult : results) {
BigDecimal lat = geoResult.getGeometry().getLocation().getLat();
BigDecimal lng = geoResult.getGeometry().getLocation().getLng();
pub.setLat(lat.doubleValue());
pub.setLng(lng.doubleValue());
List<GeocoderAddressComponent> addressComponents = geoResult.getAddressComponents();
for (GeocoderAddressComponent address : addressComponents) {
if (address.getTypes().contains("locality")) {
pub.setCity(address.getLongName());
}
if (address.getTypes().contains("administrative_area_level_1")) {
pub.setState(address.getLongName());
}
if (address.getTypes().contains("country")) {
pub.setCountry(address.getLongName());
}
}
}
} else {
log.info(response.getStatus().name());
}
}
示例7: getGeoByLatLng
import com.google.code.geocoder.Geocoder; //导入方法依赖的package包/类
public static Map<String, String> getGeoByLatLng(Double lat, Double lng) {
Map<String, String> map = new HashMap<String, String>();
final Geocoder geocoder = new Geocoder();
GeocoderRequest request = new GeocoderRequestBuilder()
.setLocation(new LatLng(new BigDecimal(lat), new BigDecimal(lng)))
.setLanguage("en")
.getGeocoderRequest();
GeocodeResponse response = geocoder.geocode(request);
if (response.getStatus().equals(GeocoderStatus.OK)) {
List<GeocoderResult> results = response.getResults();
for (GeocoderResult geoResult : results) {
List<GeocoderAddressComponent> addressComponents = geoResult.getAddressComponents();
for (GeocoderAddressComponent address : addressComponents) {
if (address.getTypes().contains("locality")) {
map.put("CITY", address.getLongName());
}
if (address.getTypes().contains("administrative_area_level_1")) {
map.put("STATE", address.getLongName());
}
if (address.getTypes().contains("country")) {
map.put("COUNTRY", address.getLongName());
}
}
}
} else {
log.info(response.getStatus().name());
}
return map;
}
示例8: main
import com.google.code.geocoder.Geocoder; //导入方法依赖的package包/类
public static void main(String[] args) {
final Geocoder geocoder = new Geocoder();
GeocoderRequest request = new GeocoderRequestBuilder()
.setAddress("The Promenade, Lahinch. Co. Clare. Ireland")
.setLanguage("en").getGeocoderRequest();
GeocodeResponse response = geocoder.geocode(request);
if (response.getStatus().equals(GeocoderStatus.OK)) {
List<GeocoderResult> results = response.getResults();
for (GeocoderResult geoResult : results) {
System.out.println(geoResult.getFormattedAddress());
BigDecimal lat = geoResult.getGeometry().getLocation().getLat();
BigDecimal lng = geoResult.getGeometry().getLocation().getLng();
System.out.println("lat: " + lat);
System.out.println("lng: " + lng);
List<GeocoderAddressComponent> addressComponents = geoResult.getAddressComponents();
for (GeocoderAddressComponent address : addressComponents) {
if (address.getTypes().contains("locality")) {
System.out.println("Cidade: " + address.getLongName());
}
if (address.getTypes().contains("administrative_area_level_1")) {
System.out.println("Estado: " + address.getLongName());
}
if (address.getTypes().contains("country")) {
System.out.println("País: " + address.getLongName());
}
}
}
}
}
示例9: getLatLongZip
import com.google.code.geocoder.Geocoder; //导入方法依赖的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;
}
示例10: geocode
import com.google.code.geocoder.Geocoder; //导入方法依赖的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;
}
}
示例11: setGeoLocation
import com.google.code.geocoder.Geocoder; //导入方法依赖的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;
}
}