Reputation: 13
I want to get latitude and longitude from a city name in java project(eclipse) with GoogleMaps. I tried with this code but I find multiple errors. I think because it is for Android. I wonder if you can help me??
public boolean getLatitudeAndLongitudeFromGoogleMapForAddress(String searchedAddress){
Geocoder coder = new Geocoder(IPlant.iPlantActivity);
List<Address> address;
try
{
address = coder.getFromLocationName(searchedAddress,5);
if (address == null) {
Log.d(TAG, "############Address not correct #########");
}
Address location = address.get(0);
Log.d(TAG, "Address Latitude : "+ location.getLatitude();+ "Address Longitude : "+ location.getLongitude());
return true;
}
catch(Exception e)
{
Log.d(TAG, "MY_ERROR : ############Address Not Found");
return false;
}
}
Upvotes: 0
Views: 1772
Reputation: 5691
The code you've posted is indeed for Android as it's using Android's Geocoder class.
If you're trying to geocode addresses server-side in a web application, then you should use the client libraries for web services instead. The Java client can be found here and it provides the following code example for Geocoding API:
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIza...")
.build();
GeocodingResult[] results = GeocodingApi.geocode(context,
"1600 Amphitheatre Parkway Mountain View, CA 94043").await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results[0].addressComponents));
Note that you'll need a valid API key; I recommend you go through Google's get started guide.
Hope this helps you.
Upvotes: 1