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);
}
}
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:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3DH.M.%2520education%2520centre%252C%2520kolkata
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!
Related
I am using the goecoder to try to convert address to lat/lng. However, it seems the geocoder works for some addresses but not others. If I search the address on google maps then it pinpoint it perfectly.
Here is an example 38 Crichton Street, Ottawa, ON, Canada
This address returns 0 results when I run the code below
Location loc = null;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(addr, 1);
if (address.size() == 0) {
return null;
}
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
loc = new Location("Unknown");
loc.setLatitude(latitude);
loc.setLongitude(longitude);
} catch (IOException ioException) {
Log.e(TAG, ioException.toString());
} catch (IllegalArgumentException illegalArgumentException) {
Log.e(TAG, illegalArgumentException.toString());
}
Any idea why or how I can fix it
Thank you
Not sure why native Android API geocoder cannot find this address. I can see that this address is found in Geocoding API web service:
https://maps.googleapis.com/maps/api/geocode/json?address=38%20Crichton%20Street%2C%20Ottawa%2C%20ON%2C%20Canada&key=MY_API_KEY
You can also see it in Geocoder tool:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D38%2520Crichton%2520Street%252C%2520Ottawa%252C%2520ON%252C%2520Canada
As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:
https://github.com/googlemaps/google-maps-services-java
The Javadoc for client library is located at
https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/
I hope this helps!
I am using google's geocoder class to fetch city with given lat-long.
I am facing this problem in India where earlier city name was Banglore and now it is changed to bengaluru.
So when I fetch using geocoder.getFromLocation method I get Banglore instead of bengaluru
While using the same lat-long I call Geocoder api directly, then I get the right city value i.e bengaluru.
Following is code geocoder class code
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String city = null;
if (geocoder != null) {
if (geocoder.isPresent()) {
try {
List<Address> addressList = geocoder.getFromLocation(params[0], params[1], 1);
city = addressList.get(0).getLocality();
} catch (Exception e) {
e.printStackTrace();
}
}
}
My target and compile sdk version is 23.
Thank you in advance.
When I go maps|settings|edit home or work I see my home & work address, cool!
Anybody know how to get that same information from the Android SDK? I am working on a maps app and would like to plot those to points of reference, with out asking the user for them again?
Thanks
You will need to use reverse geocoding to convert the latitude/longitude into an address. More info about geocoding can be found here.
Here is an example:
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
Log.e(TAG, "Impossible to connect to Geocoder", e);
}
how to convert the latitude and longitude in to postal address. i am not possible to use mapview to get geopoint.I wanted to convert latitude and longitude in address without use of mapview
This is called Reverse Geocoding, there are number of services that you can use including Google, Yahoo and Bing
See this article for free Geocoding services
Android has GeoCoder class for this :
public String getAddress(double latitude, double longitude){
if (Geocoder.isPresent()) {
try {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
if(addresses != null && addresses.size() > 0) {
Address first = addresses.get(0);
StringBuilder sb = new StringBuilder();
if(first.getMaxAddressLineIndex() > 0){
sb.append(first.getAddressLine(0) + ", ");
}
sb.append(first.getLocality() + ", ");
sb.append(first.getCountryName());
return sb.toString();
} catch (Exception e) {
Log.e("MyAPP", "Reverse geo lookup failed", e);
return "Unavailable"
}
} else {
return "unavailable";
}
}
Go to project properties > Android > set project build target to Google
What I have: Latitude and Longitude.
What I want: Get weather update for these coordinates.
If you want to use the Google Weather API, you'll have to pass it either a City, State or a Zip code. To do this, you'll need to GeoCode your lat/long to get this info.
Here's the URL to the Google Weather API: http://www.google.com/ig/api?weather=Seattle,WA
Here's a sample code to take lat/long and convert to zip:
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 3);
} catch (IOException ex) {
//Handle IOException
}
for (int i = 0; i < addresses.size(); i++) {
Address address = addresses.get(i);
if (address.getPostalCode() != null)
String zipCode = address.getPostalCode();
}
Then pass the Zip Code (Or City, State) to the Google Weather API and parse the returning XML. Good luck!
google API is down, so you should look for an alternative.
http://openweathermap.org/api
You need to use an API. You'll have to do some research on your own, here's one good one.
http://www.weather.gov/xml/