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!
Related
I want to get the current location for android with out using Google APIs Key that's why
I'm Using the Geocoder.
It is working absolutely for many devices but it not for some selected devices like Latest android version_12 and in OnePlus_v10,11,12.
if (Geocoder.isPresent())
{
try
{
Geocoder geocoder = new Geocoder(contex, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
cityName = addresses.get(0).getAddressLine(0);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
Please provide the solution for the same share with someone you know who can fix it.
Also is there any changes in new android device regarding GPS,WI-FI, Network Checker.
Sometimes I get the IOException when I try to get address through latitude and longitude via the geocoder class of android.
This occurs rarely and force closes my application.
private void locAdd(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(ReportActivity.this,
Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
Log.e("Addresses", "-->" + addresses);
} catch (IOException e) {
e.printStackTrace();
}
}
This is the code I use to get location. Is there any issue with the code?
How do I solve it?
Logcat
java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
at com.pstpl.crimeverify.CrimeReportActivity.locAdd(CrimeReportActivity.java:554)
at com.pstpl.crimeverify.CrimeReportActivity.access$10(CrimeReportActivity.java:549)
The API documentation,
http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation%28double,%20double,%20int%29
says that IOException will occur "if the network is unavailable or any other I/O problem occurs".
I don't think there is any problem with the code, since it works for "sometimes".
BTW, you are passing latitude1 and longitude as method arguments, but you are using latitude and longitude.
I need to fetch longitude and latitude by street name. I saw that Geocoder can do that job. The documentation tells us that "the Geocoder class requires a backend service that is not included in the core android framework". That means that I can't be sure that Geocoder is implemented in Android devices.
My question is, how widespread is the Geocoder implementation. If there are only very few devices without Geocoder, I can live with that, but if there are a lot of devices, I have to rethink this functionality of my app.
We should use isPresent() method of GeoCoder class to decide so.
http://developer.android.com/reference/android/location/Geocoder.html#isPresent()
A GeoCoder client is included along with Google Maps, so any device with Google Maps should have a working GeoCoder implementation. By and large, Android devices have Google Maps installed. Other than that, there could be no Google Maps, and some other GeoCoder backend available. In such a case, you can use the following code to determine if the device has a working GeoCoder implementation:
final Geocoder geocoder = new Geocoder(this);
final String locName = "1600 Amphitheatre Parkway, Mountain View, CA";
try {
final List<Address> list = geocoder.getFromLocationName(locName, 1);
if ( ! (list == null || list.isEmpty()) ) {
final Address address = list.get(0);
System.out.println("Geocoder backend present, (lat,lon) = (" + address.getLatitude() + ", " + address.getLongitude() + ")");
}
else {
System.out.println("Geocoder backend not present");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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/
i want to get the latitude and longitude using the zipcode for android application
This is a much simpler solution, assuming the geocoder services are present:
final Geocoder geocoder = new Geocoder(this);
final String zip = "90210";
try {
List<Address> addresses = geocoder.getFromLocationName(zipCode, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
// Use the address as needed
String message = String.format("Latitude: %f, Longitude: %f",
address.getLatitude(), address.getLongitude());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
// Display appropriate message when Geocoder services are not available
Toast.makeToast(this, "Unable to geocode zipcode", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// handle exception
}
There is no rival of Google in GeoCoding :)
You have to just make an http Get request for the following link.
Here's a link
Just change the postal code Element According to your requirements. And Enjoy.
For further reference follow the link.Maps Geocoding Reference
You could use YQL like so: select centroid from geo.places where text="ZIPCODE_HERE"
You will have to use a Geocoding Service, like the ones provided by Google or the freely available Nominatim. Note that since you will be just providing a ZIP code, you might not get the results you want due to the inaccuracy of the address itself.