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();
}
Related
I have an application that must collect address from a marker on Google Maps API. I tested this code and it works fine on devices that run Android 6.0 :
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addressList1 = geocoder.getFromLocation(currentLatitude,currentLongitude,1);
Address addressCurrent = addressList1.get(0);
full_address = addressCurrent.getAddressLine(0); //Detail address
country = addressCurrent.getCountryName();
//locality = addressCurrent.getLocality(); //sub-district
admin_area = addressCurrent.getAdminArea(); //City
sub_admin_area = addressCurrent.getSubAdminArea(); //District
road_name = addressCurrent.getThoroughfare(); // road
Toast.makeText(this, full_address, Toast.LENGTH_SHORT).show();
}catch (IOException e){
Toast.makeText(this, "No Data", Toast.LENGTH_SHORT).show();
Log.e("error geocoder", e.toString());
}
But when I try to run on Android 8.1 devices it returns an error log:
E/errorĀ geocoder: java.io.IOException: NetCode:-1000,NetRes:{}
I tried to search for it but found no solution.
How can I fix this?
As Gencoder doc say: Use the isPresent() method to determine whether a Geocoder implementation exists.
so call isPresent() method firstly, and manually check device network status.
If still failed, maybe you need create custom Geocoder implementation, this link is helpful.
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!
My current code use FusedLocationProviderApi to get user's current city/locality. This code is in my Service :
try {
Geocoder gcd = new Geocoder(LocationUpdateService.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0)
{
txtStation.setText("Locality : " + addresses.get(0).getLocality() + " Sublocal : " + addresses.get(0).getSubLocality());
}
}catch (Exception e) {
Toast.makeText(LocationUpdateService.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
However i need to show user's more specific place (bus station, gas station, etc) and i think i must use Places API.
How to integrate the Places API to my current code (which use FusedLocationProviderApi)?
What i think now is to use FusedLocationProviderApi to get the basic latitude & longitude, and then use Places API with those coordinates.
Please kindly help me/show me some clue
Thanks a lot for your help
How about the PlaceDetectionApi?
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.
According to the Geocoder documentation, the class provides methods into a backend service which may or may not exist on the device. If the geocoder doesn't exist on the device, theoretically a geocode request will return no results. However, how can one differentiate between no results due to there being no backend geocoder service vs. there being no results because the location makes no sense?
My app already requires the Google Maps API, so perhaps this is a moot point; as a secondary question, does the Google Maps API always include a Geocoder?
Strictly speaking there could be another backend installed, just try if a geocoder is present
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();
}
According to the Google Maps docs, "The Google Maps API provides a client geocoder for geocoding addresses". So I would say that since you are requiring the google maps API, you are requiring a geocoder.