本文整理汇总了Java中com.google.code.geocoder.Geocoder类的典型用法代码示例。如果您正苦于以下问题:Java Geocoder类的具体用法?Java Geocoder怎么用?Java Geocoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Geocoder类属于com.google.code.geocoder包,在下文中一共展示了Geocoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateCoordinateForAddress
import com.google.code.geocoder.Geocoder; //导入依赖的package包/类
/**
* calls the google geocode api and enrich the given address with latitude
* and longitude information
*
* It expects a valid german address. There is not check if the address is correct.
*
* @param address
* @return
*/
public static Coordinate calculateCoordinateForAddress(Address address) {
String requestAddress = "";
requestAddress += address.getStreet() != null ? address.getStreet() + " " : "";
requestAddress += (address.getStreet() != null && address.getStreetNumber() != null) ? address.getStreetNumber() + " " : "";
requestAddress += address.getZipCode() != null ? address.getZipCode() + " " : "";
requestAddress += address.getCity()!= null ? address.getCity() + " " : "";
requestAddress += ", Deutschland";
try {
final Geocoder geocoder = new Geocoder();
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(requestAddress).setLanguage("de").getGeocoderRequest();
List<GeocoderResult> geocoderResults = geocoder.geocode(geocoderRequest).getResults();
if (geocoderResults.isEmpty()) {
throw new RuntimeException("Google geocode could not find any results for: " + requestAddress); //TODO for testing purposes an exception is fine, how about in production?
}
//use first result, hopefully it is the best
LatLng location = geocoderResults.get(0).getGeometry().getLocation();
return new Coordinate(location.getLat().doubleValue(), location.getLng().doubleValue());
} catch (IOException e) {
throw new RuntimeException("Could not gather google geocode api for parameter: " + requestAddress); //TODO for testing purposes an exception is fine, how about in production?
}
}
示例2: 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);
}
}
}
示例3: 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;
}
}
示例4: getGeocoderService
import com.google.code.geocoder.Geocoder; //导入依赖的package包/类
/**
* Gets the service to access geo localization methods; using this service does not require authentication.
*
* @return The service created, this is on demand created.
*/
public synchronized GoogleGeocoderService getGeocoderService() {
if (geoService == null) {
geoService = new GoogleGeocoderService(new Geocoder());
}
return geoService;
}
示例5: 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);
}
示例6: 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;
}
示例7: setGoogleLocation
import com.google.code.geocoder.Geocoder; //导入依赖的package包/类
public static void setGoogleLocation(Pub pub, boolean isLatLng) {
final Geocoder geocoder = new Geocoder();
if (isLatLng) {
getGeoByPubLatLng(pub, geocoder);
} else {
getGeoByPubAddress(pub, geocoder);
}
}
示例8: 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());
}
}
示例9: 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());
}
}
示例10: 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;
}
示例11: 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());
}
}
}
}
}
示例12: 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;
}
示例13: 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;
}
}
示例14: GoogleGeocoderService
import com.google.code.geocoder.Geocoder; //导入依赖的package包/类
GoogleGeocoderService(Geocoder geocoder) {
this.geocoder = geocoder;
}
示例15: setUp
import com.google.code.geocoder.Geocoder; //导入依赖的package包/类
@BeforeClass
public static void setUp() {
geocoder = new Geocoder();
}