本文整理汇总了Java中com.google.maps.GeocodingApi类的典型用法代码示例。如果您正苦于以下问题:Java GeocodingApi类的具体用法?Java GeocodingApi怎么用?Java GeocodingApi使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GeocodingApi类属于com.google.maps包,在下文中一共展示了GeocodingApi类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: locate
import com.google.maps.GeocodingApi; //导入依赖的package包/类
@Override
public GeoLocationResponse locate(final Double latitude, final Double longitude) {
if (latitude == null || longitude == null) {
LOGGER.debug("latitude/longitude must not be null in order for geolocation to proceed");
return null;
}
final GeoLocationResponse r = new GeoLocationResponse();
r.setLatitude(latitude);
r.setLongitude(longitude);
final LatLng latlng = new LatLng(latitude, longitude);
try {
final GeocodingResult[] results = GeocodingApi.reverseGeocode(this.context, latlng).await();
if (results != null && results.length > 0) {
Arrays.stream(results)
.map(result -> result.formattedAddress)
.forEach(r::addAddress);
return r;
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return r;
}
示例2: lookupAddr
import com.google.maps.GeocodingApi; //导入依赖的package包/类
public static String lookupAddr(String establishment) throws ApiException, InterruptedException, IOException {
//set up key
GeoApiContext lookupDoodad = new GeoApiContext.Builder()
.apiKey(API_KEY)
.build();
GeocodingResult[] results = GeocodingApi.geocode(lookupDoodad,
establishment).await();
//converts results into usable address
String address = (results[0].formattedAddress);
return address;
}
示例3: lookupCoord
import com.google.maps.GeocodingApi; //导入依赖的package包/类
public static LatLng lookupCoord(String establishment) throws ApiException, InterruptedException, IOException {
//set up key
GeoApiContext lookupDoodad = new GeoApiContext.Builder()
.apiKey(API_KEY)
.build();
GeocodingResult[] results = GeocodingApi.geocode(lookupDoodad,
establishment).await();
//converts results into usable Coordinates
LatLng coords = (results[0].geometry.location);
//System.out.println(results[0].geometry.location);
return coords;
}
示例4: Place
import com.google.maps.GeocodingApi; //导入依赖的package包/类
public Place(int n, String address, double demand, double tis, double tie, double st) {
ParseLocalDB();
JSONArray coords = getPlaceCoordsFromLocalDB(address);
if (coords == null) {
GeocodingResult[] results = new GeocodingResult[0];
try {
results = GeocodingApi.geocode(Place.context, address).language("ru").await();
this.lat = results[0].geometry.location.lat;
this.lng = results[0].geometry.location.lng;
this.address = results[0].formattedAddress;
this.xs = this.xs_display = GoogleMapsProjection.latToYWorld(this.lat);
this.ys = this.ys_display = GoogleMapsProjection.lonToXWorld(this.lng);
} catch (Exception e) {
e.printStackTrace();
this.address = address;
this.xs = this.xs_display = 0;
this.ys = this.ys_display = 0;
}
} else {
this.lat = Double.parseDouble((String) coords.get(0));
this.lng = Double.parseDouble((String) coords.get(1));
this.address = address;
this.xs = this.xs_display = GoogleMapsProjection.latToYWorld(this.lat);
this.ys = this.ys_display = GoogleMapsProjection.lonToXWorld(this.lng);
}
this.demand = demand;
this.time_interval_start = tis;
this.time_interval_end = tie;
this.service_time = st;
this.number = n;
}
示例5: Address
import com.google.maps.GeocodingApi; //导入依赖的package包/类
@SneakyThrows
public Address(String address) {
this.address = address;
if (address != null && geoApiContext != null) {
GeocodingResult[] results = GeocodingApi.geocode(geoApiContext, address).await();
if (results.length > 0) {
GeocodingResult result = results[0];
for (AddressComponent component : result.addressComponents) {
List<AddressComponentType> types = Arrays.asList(component.types);
if (types.contains(AddressComponentType.COUNTRY)) {
this.country = component.longName;
}
if (types.contains(AddressComponentType.LOCALITY)) {
this.city = component.longName;
}
if (types.contains(AddressComponentType.POSTAL_CODE)) {
this.postalCode = component.longName;
}
}
this.latitude = result.geometry.location.lat;
this.longitude = result.geometry.location.lng;
}
}
}
示例6: getGeoCodedLocation
import com.google.maps.GeocodingApi; //导入依赖的package包/类
/**
* With the supplied address, this method uses the Google Maps API to retrieve geocoded
* information, such as coordinates, postalcode, city, country etc.
*
* @param address The address to geocode.
* @return An EventLocation object containing all geocoded data.
*/
static EventLocation getGeoCodedLocation(String address) {
EventLocation eventLocation = new EventLocation();
// Temporary API key
final String API_KEY = "AIzaSyAM9tW28Kcfem-zAIyyPnnPnyqL1WY5TGo";
GeoApiContext context = new GeoApiContext().setApiKey(API_KEY);
GeocodingApiRequest request = GeocodingApi.newRequest(context).address(address);
try {
GeocodingResult[] results = request.await();
List<Double> latlng = new ArrayList<>();
latlng.add(results[0].geometry.location.lng);
latlng.add(results[0].geometry.location.lat);
EventLocationCoordinates eventLocationCoordinates = new EventLocationCoordinates();
eventLocationCoordinates.setCoordinates(latlng);
eventLocation.setCoordinates(eventLocationCoordinates);
eventLocation.setAddress(address);
eventLocation.setParsedAddress(results[0].formattedAddress);
for (AddressComponent addressComponent : results[0].addressComponents) {
switch (addressComponent.types[0]) {
case POSTAL_CODE:
// Remove any white space from postal code
String postalCode = addressComponent.longName
.replaceAll("\\s+","");
eventLocation.setPostalCode(postalCode);
break;
case LOCALITY:
eventLocation.setCity(addressComponent.longName);
break;
case POSTAL_TOWN:
eventLocation.setCity(addressComponent.longName);
break;
case ADMINISTRATIVE_AREA_LEVEL_1:
eventLocation.setCounty(addressComponent.longName);
break;
case COUNTRY:
eventLocation.setCountry(addressComponent.shortName);
break;
default:
break;
}
}
} catch (Exception e) {
// Handle error
}
return eventLocation;
}
示例7: reverseGeocode
import com.google.maps.GeocodingApi; //导入依赖的package包/类
/**
* Sends a reverse geocode request obtaining the district identifier.
*
* @param latitude latitude value
* @param longitude longitude value
* @throws Exception if a geocoding error occurred
*/
private void reverseGeocode(final double latitude, final double longitude) throws Exception {
final GeoApiContext context = new GeoApiContext.Builder()
.apiKey(KEY)
.build();
final LatLng latlng = new LatLng(latitude, longitude);
final GeocodingResult[] results;
try {
results = GeocodingApi.reverseGeocode(context, latlng).await();
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
this.response = gson.toJson(results[0].addressComponents);
} catch (final Exception e) {
throw e;
}
}
示例8: getLocationOfMentor
import com.google.maps.GeocodingApi; //导入依赖的package包/类
public Point getLocationOfMentor(Mentor mentor) {
GeoApiContext context = new GeoApiContext().setApiKey("AIzaSyC9hT7x8gTBdXcTSEy6XU_EWpr_WDe8lSY");
try {
String address = "";
if (mentor.getAddress1() != null && mentor.getAddress1().length() > 0 ) {
address += (address.length() > 0 ? ", " : "") + mentor.getAddress1();
}
if (mentor.getAddress2() != null && mentor.getAddress2().length() > 0 ) {
address += (address.length() > 0 ? ", " : "") + mentor.getAddress2();
}
if (mentor.getZip() != null && mentor.getZip().length() > 0 ) {
address += (address.length() > 0 ? ", " : "") + mentor.getZip();
}
if (mentor.getCity() != null && mentor.getCity().length() > 0 ) {
address += (address.length() > 0 ? ", " : "") + mentor.getCity();
}
if (mentor.getState() != null && mentor.getState().length() > 0 ) {
address += (address.length() > 0 ? ", " : "") + mentor.getState();
}
if (mentor.getCountryId() != null && mentor.getCountryId().getName() != null && mentor.getCountryId().getName().length() > 0 ) {
address += (address.length() > 0 ? ", " : "") + mentor.getCountryId().getName();
}
if (address.length() > 0) {
GeocodingResult[] results = GeocodingApi.geocode(context, address).await();
return new Point(results[0].geometry.location.lat, results[0].geometry.location.lng);
} else {
log.error("Unable to geocode address of " + mentor.getFullName() + ": No address available");
return null;
}
} catch (Exception e) {
log.error("Unable to geocode address of " + mentor.getFullName() + ": " + e.toString());
return null;
}
}
示例9: getPublicLocationOfMentor
import com.google.maps.GeocodingApi; //导入依赖的package包/类
public Point getPublicLocationOfMentor(Mentor mentor) {
GeoApiContext context = new GeoApiContext().setApiKey("AIzaSyC9hT7x8gTBdXcTSEy6XU_EWpr_WDe8lSY");
try {
String address = "";
if (mentor.getAddress1() != null && mentor.getAddress1().length() > 0 && mentor.getAddress1Public()) {
address += (address.length() > 0 ? ", " : "") + mentor.getAddress1();
}
if (mentor.getAddress2() != null && mentor.getAddress2().length() > 0 && mentor.getAddress2Public()) {
address += (address.length() > 0 ? ", " : "") + mentor.getAddress2();
}
if (mentor.getZip() != null && mentor.getZip().length() > 0 && mentor.getZipPublic()) {
address += (address.length() > 0 ? ", " : "") + mentor.getZip();
}
if (mentor.getCity() != null && mentor.getCity().length() > 0 && mentor.getCityPublic()) {
address += (address.length() > 0 ? ", " : "") + mentor.getCity();
}
if (mentor.getState() != null && mentor.getState().length() > 0 && mentor.getStatePublic()) {
address += (address.length() > 0 ? ", " : "") + mentor.getState();
}
if (mentor.getCountryId() != null && mentor.getCountryId().getName() != null && mentor.getCountryId().getName().length() > 0 && mentor.getCountryPublic()) {
address += (address.length() > 0 ? ", " : "") + mentor.getCountryId().getName();
}
if (address.length() > 0) {
GeocodingResult[] results = GeocodingApi.geocode(context, address).await();
return new Point(results[0].geometry.location.lat, results[0].geometry.location.lng);
} else {
log.error("Unable to geocode address of " + mentor.getFullName() + ": No address available");
return null;
}
} catch (Exception e) {
log.error("Unable to geocode address of " + mentor.getFullName() + ": " + e.toString());
return null;
}
}
示例10: getLocationOfRestaurant
import com.google.maps.GeocodingApi; //导入依赖的package包/类
/**
* Gets the location of restaurant using the Google Geocoding API.
*
* @param restaurant
* Restaurant from which to get the location.
* @return the location of restaurant
*/
private String getLocationOfRestaurant(Restaurant restaurant, HttpServletRequest request) {
// Replace the API key below with a valid API key.
GeoApiContext context = new GeoApiContext().setApiKey("AIzaSyAvO9bl1Yi2hn7mkTSniv5lXaPRii1JxjI");
GeocodingApiRequest req = GeocodingApi.newRequest(context).address(String.format("%1$s %2$s, %3$s %4$s", restaurant.getStreetNumber(), restaurant.getStreet(), restaurant.getZip(), restaurant.getCity()));
try {
GeocodingResult[] result = req.await();
if (result != null && result.length > 0) {
// Handle successful request.
GeocodingResult firstMatch = result[0];
if (firstMatch.geometry != null && firstMatch.geometry.location != null) {
restaurant.setLocationLatitude((float) firstMatch.geometry.location.lat);
restaurant.setLocationLongitude((float) firstMatch.geometry.location.lng);
} else {
return messageSource.getMessage("restaurant.addressNotResolveable", null, Locale.getDefault());
}
} else {
return messageSource.getMessage("restaurant.addressNotFound", null, Locale.getDefault());
}
} catch (Exception e) {
LOGGER.error(LogUtils.getExceptionMessage(request, Thread.currentThread().getStackTrace()[1].getMethodName(), e));
return messageSource.getMessage("restaurant.googleApiError", new String[] { e.getMessage() }, Locale.getDefault());
}
return null;
}
示例11: geocode
import com.google.maps.GeocodingApi; //导入依赖的package包/类
@Timed
@Override
public GeocodingResult[] geocode(
@Nonnull String address
) throws Exception {
LOGGER.debug("geocode called: address={}", address);
requireNonNull(address, "address cannot be null");
return GeocodingApi.geocode(context, address).await();
}
示例12: updateMarkerButtonClick
import com.google.maps.GeocodingApi; //导入依赖的package包/类
private void updateMarkerButtonClick(boolean isFrom){
String primaryText;
String secondaryText;
String placeId = null;
if (currInfoMarker.getTitle().equals("TelOFun")){
int stationId = Integer.parseInt(currInfoMarker.getSnippet());
String stationName = IriaData.getTelOfanStationsDict().get(stationId).getStationName();
primaryText = String.format("TelOFun (%s): %s", currInfoMarker.getSnippet(), stationName);
secondaryText = "";
}
else {
try {
GeocodingResult[] results = GeocodingApi.newRequest(geoApiContext)
.latlng(MapUtils.getModelLatLngFromGms(currInfoMarker.getPosition())).await();
primaryText = results[0].formattedAddress;
secondaryText = "";
placeId = results[0].placeId;
} catch (Exception e) {
primaryText = "Custom Origin";
secondaryText = String.format("(%f ,%f)", currInfoMarker.getPosition()
.latitude, currInfoMarker.getPosition().longitude);
e.printStackTrace();
}
}
CustomAutoCompletePrediction newPrediction =
new CustomAutoCompletePrediction(primaryText, secondaryText, placeId);
if (isFrom){
from.setPrediction(newPrediction, true);
}
else{
to.setPrediction(newPrediction, true);
}
directionsManager.setNewMarkerByCustomPrediction(isFrom, currInfoMarker.getPosition(), newPrediction);
updateMapToNewMArkerState();
}
示例13: geocodeSnappedPoint
import com.google.maps.GeocodingApi; //导入依赖的package包/类
/**
* Geocodes a Snapped Point using the Place ID.
*/
private GeocodingResult geocodeSnappedPoint(GeoApiContext context, SnappedPoint point) throws Exception {
GeocodingResult[] results = GeocodingApi.newRequest(context)
.place(point.placeId)
.await();
if (results.length > 0) {
return results[0];
}
return null;
}
示例14: geocode
import com.google.maps.GeocodingApi; //导入依赖的package包/类
/**
* Sends a geocode request obtaining latitude and longitude values.
*
* @param address a string representing the address
* @throws Exception if a geocoding error occurred
*/
private void geocode(final String address) throws Exception {
final GeoApiContext context = new GeoApiContext.Builder()
.apiKey(KEY)
.build();
final GeocodingResult[] results;
try {
results = GeocodingApi.geocode(context, address).await();
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
this.response = gson.toJson(results[0].geometry.location);
} catch (final Exception e) {
throw e;
}
}
示例15: getLocationName
import com.google.maps.GeocodingApi; //导入依赖的package包/类
public String getLocationName(double latitudeDeg, double longitudeDeg) {
myLogger.trace("Reverse geocoding lat/long (" + latitudeDeg + ", " + longitudeDeg + ")");
String key = myConfigService.getConfiguration().getGoogleApiKey();
if (key == null || key.isEmpty()) {
myLogger.warn("Google API Key has not been set.");
return null;
}
GeoApiContext context = new GeoApiContext().setApiKey(key);
String name = "(Unknown)";
try {
GeocodingResult[] results = GeocodingApi
.reverseGeocode(context, new LatLng(latitudeDeg, longitudeDeg)).await();
myLogger.trace("Number of results: " + results.length);
// There may be multiple results with sequentially less data in each;
// just return the first one, since it should have the most info.
for (GeocodingResult result : results) {
myLogger.debug("Location for (" + latitudeDeg + ", " +
longitudeDeg + "): " + result.formattedAddress);
return result.formattedAddress;
}
}
catch (Exception e) {
myLogger.error("Reverse geocoding failed for (" + latitudeDeg +
", " + longitudeDeg + "):", e);
}
return name;
}