Reputation: 115
I am trying to get locations from the string which is being searched. But, in some cases addressList is returning size 0(i.e. H.M. education centre, kolkata). I don't have latitude and longitude to search the place. Needed help.
String addressString = (String) adapterView.getItemAtPosition(position);
String addressString1 = (String) adapterView.getItemAtPosition(position);
List<Address> addressList = null;
Address address = null;
if (!TextUtils.isEmpty(addressString))
{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try
{
addressList = geocoder.getFromLocationName(addressString, 1);
}
catch (IOException e)
{
e.printStackTrace();
addressString = addressString1;
}
//noinspection ConstantConditions
if(addressList != null && addressList.size() > 0)
{
address = addressList.get(0);
}
}
Upvotes: 3
Views: 3506
Reputation: 32138
As alternative you can use Geocoding API or Places API web services. I checked your address 'H.M. education centre, kolkata' in Geocoding API web service request and figured out that coordinate can be found. Have a look at Geocoder tool with this address:
In your Java code I can suggest using the Java Client for Google Maps Services hosted at Github:
https://github.com/googlemaps/google-maps-services-java
The code snippet to execute web service request with this client library is the following
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIza...")
.build();
GeocodingResult[] results = GeocodingApi.geocode(context,
"H.M. education centre, kolkata").await();
The Javadoc for the current version of the library can be found at
https://googlemaps.github.io/google-maps-services-java/v0.2.7/javadoc/
Note also that places like this one can be found via Places API web service. In this case the code snippet will be
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIza...")
.build();
TextSearchRequest req = PlacesApi.textSearchQuery(context, "H.M. education centre, kolkata");
try {
PlacesSearchResponse resp = req.await();
if (resp.results != null && resp.results.length > 0) {
//Process your results here
}
} catch(Exception e) {
Log.e(TAG, "Error getting places", e);
}
I hope this helps!
Upvotes: 1