Java源码示例:com.google.maps.GeocodingApi
示例1
public boolean parseLocation(String locationName){
String ERROR_MESSAGE = "Couldn't find specified custom location, falling back to co-ordinates";
if (locationName == null || locationName.equals("")) {
System.out.println(ERROR_MESSAGE);
return false;
}
GeoApiContext context = new GeoApiContext().setApiKey(GoogleApiKey);
try {
GeocodingResult[] request = GeocodingApi.newRequest(context).address(locationName).await();
LatLng location = request[0].geometry.location;
latitude = location.lat;
longitude = location.lng;
System.out.println("Found custom location to be: " + request[0].formattedAddress);
return true;
} catch (Exception e) {
System.out.println(ERROR_MESSAGE);
return false;
}
}
示例2
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;
}
}
示例3
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;
}
}
示例4
/**
* 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;
}