If someone could help me. Is using geocoder free for android, currently i'm only using google maps. I see that it says that its only 2500 requests per day. Or is this only for web and something entirely different from mobile?
I am using the following code to get a zip code with a latitude and longitude.
final Geocoder geocoder = new Geocoder(getActivity());
try {
List<Address> addresses = geocoder.getFromLocation(mLocationClient.getLatitudeCoordinate(), mLocationClient.getLongitudeCordinate(),5);
for (Address address: addresses){
if(address.getLocality()!=null && address.getPostalCode()!=null){
Log.v("Zip code", " " + address.getPostalCode());
}
}
} catch (IOException e) {
e.printStackTrace();
}
https://developers.google.com/maps/pricing-and-plans/#details
Just trying to make sure. I'm super confused on this and some clarification could help :) Thanks
The Geocoder use is totally free.
The link you wrote is related to Google Maps and Google Places API, not the Geocoding API. This is the correct link:
https://developers.google.com/maps/documentation/geocoding/usage-limits
Anyway you are not using the Geocoding API with this code, you just using the Geocoder of Android.
This is the documentation to use the Geocoding API if you want:
https://developers.google.com/maps/documentation/geocoding/intro
The Geocoding API works better than Geocoder normally but has usage limits and the implementation is bigger.
Not any more. Google just decided to 10x their price for geocoding https://cloud.google.com/maps-platform/pricing/sheet/
Related
According to the maps optimisations guide it is possible to reduce usage of Google Places by using the GeoCoding API to retrieve a place location by place Id.
Geocoding API
If your application handles user-typed addresses, the addresses are sometimes ambiguous (incomplete, misspelled, or poorly formatted). You can disambiguate addresses using Autocomplete. Then, use the place IDs to get the place locations.
Mobile
Maps
For mobile applications, use Maps SDK for Android or Maps SDK for iOS when displaying a map. The mobile SDKs are free of charge and have unlimited quota. Use Maps Static API or Maps JavaScript API when requirements rule out using the mobile SDKs.
Is such optimisation possible using Android?
The Geocoder in Android Location package doesn't expose any method to retrieve an address from a placeId only from lat,long or name:
List<Address> getFromLocation(double latitude, double longitude, int maxResults);
List<Address> getFromLocationName(String locationName, int maxResults);
List<Address> getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude);
Javascript Code:
// This function is called when the user clicks the UI button requesting
// a geocode of a place ID.
function geocodePlaceId(geocoder, map, infowindow) {
var placeId = document.getElementById('place-id').value;
geocoder.geocode({'placeId': placeId}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(11);
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
The native Android API geocoder doesn't support getting addresses by place ID. Unfortunately, Google Maps Android SDK doesn't provide built-in Geocoder neither. The feature request exists for a long time, but it looks like it doesn't have high priority:
https://issuetracker.google.com/issues/35823852
So, to use place ID in requests you are stick to REST API. There is a Java client library for Google Maps API Web Services that you can find on github:
https://github.com/googlemaps/google-maps-services-java
You can use this library to call Geocoding API from your Java code in Android.
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIza...")
.build();
GeocodingResult[] results = GeocodingApi.newRequest(context)
.place("ChIJHzwQtJeLGGARxaSLI71pDSY").await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results[0].addressComponents));
Note that API key for web services must be different from an API key that you used in Android app, because web services don't support Android app restriction.
I hope this helps!
I've just delved into the world of Global Positioning System (GPS) and found the following interesting facts:
1) The Android class android.location.Geocoder always returns null when getting address via reverse geocoding. The code I used is:
Geocoder mGeocoder = new Geocoder(context, locale);
List<Address> addresses = mGeocoder.getFromLocation(latitude, longitude, 1);
if (!addresses.isEmpty()) {
// do something.
} else {
// Display a message regarding no address available.
}
The reason is stated here:
The Geocoder class requires a backend service that is not included in
the core android framework.
So, essentially I would either have to provide offline geolocation data myself for reverse geocoding, or my Android app will have to connect to the Internet and look up geolocation data from Google (via http://maps.googleapis.com/maps/api/geocode/json?latlng= for example). Or use alternative geolocation providers like OpenStreetMap, etc.
2) Android apps like Google Maps requires an Internet connection; while offline provider like TomTom bundled offline geolocation data together with their app so an Internet connection is not needed.
Beside the stated facts above, is there another alternative for retrieving geolocation address without using offline reverse geographic data or using an Internet connection?
If you have some experience with geocoding and reverse geocoding in Android, then please post your thoughts.
Thanks.
There is at least one way for each.
For reverse-geocoding, you let the user mark the geolocation of the address himself using gps.
For geocoding, you let the user hunt down a set of coordinates and then you have him read the street name and the address he finds himself at.
There are few offline libraries available to reverse Geocode offilne. Suggest you to go through these libraries.Personally i have not used these libraries.Let me know if you still face any problems.
https://developers.arcgis.com/android/
Sample code for link:
https://github.com/Esri/arcgis-runtime-samples-android/tree/master
Can anybody tell me exact difference between Android Geocoder and Android Google Geocoder API
As far I know, Android Geocoder is platform in-built class and gives less result compare to APIs and also less reliable.
Is there any hard limit/quota for in-built Geocoder class ?
Android Geocoder is built in class and has no quota limits.
Geocoding API is a http request and has 2500 QPD quota. Geocoding seem to be more reliable.
Geocoder is just "a class for handling geocoding and reverse geocoding".
According to the documentation:
"The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists."
Google Geocoding API is the API, the backend service in the platform. You can use it without Geocoder class.
Reference: https://developer.android.com/reference/android/location/Geocoder
In my application I need to use the geocoding, but I am not quite clear which method to use. Until yesterday I added the parameters to the URL maps.googleapis.com/maps/api/geocode/json?address=myparameter&sensor=false, but Google blocked my requests for a day, making the application crash because it did not return any results from the request for geocoding.
Now I am using the Geocoder class that does the same thing and also I have seen that you can create and use an API key for Geocoding.
Which method do you recommend to use? What is the difference between the two methods, apart from the limitations of the requests?
If you need to use a map in your app, you should use the API called: Google Maps Android API v2.
If you need to manage geocoded data (address to lat/lng or viceversa) then enable Geocoding API and start using the class Geocoder.
For example (lat/lng to address):
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
Geocoder is a built-in API in the Android framework that is free. Geocoding API is a rest API that is paid. Geocoder uses a different search stack internally and this leads to different results comparing to the Geocoding rest API. The Geocoding rest API works better than Geocoder normally but has usage limits and the implementation is bigger.
Users of free API have following limits:
2,500 requests per 24 hour period.
5 requests per second.
You can include API key in web-request also like:
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY
Not sure about android Geocoder class, maybe someone else can help you out in that matter. But i assume same limits would apply.
Geocoder API is built into the system and it's easy to use. From my experience, it's working in most cases, although:
in some cases it fails and throws grpc failed IOException
Non-fatal Exception: java.io.IOException: grpc failed
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
it may not be even available on some devices (use isPresent() method to verify this).
With above, I don't think Geocoder API is super reliable, so if geocoding is a critical part of your app, you may look into other options (like Google's Geocoding API, which is not free).
The app uses the Geocoder object. It works fine on my stock Froyo Nexus One. But then I run the exact same app on a different device (an Advent Vega 10" tablet also running Froyo) and I get this exception: Service not Available. The method I'm using is getFromLocationName(), I'm building against the Android 1.6 Google API.
I'm aware of an issue where this exception is thrown on the emulator, but I suspect this is different. Why would it be thrown on one device running Froyo but not another?
The app is a location app, and as the tablet has no GPS or mobile network, in a scenario where the Wi-Fi connection doesn't provide a location, the user must manually specify it, so not being able to use the Geocoder object is bad news.
I could add a way for the user to select the location on a map, but it's not ideal. Possibly I could use the Google Maps API directly, but I'm keen to understand the nature of the issue first as would be nice to have an easier solution.
Hopefully in a future release Android will include an OS-level "default location" for non-Geocoder devices, so location-aware apps work out of the box on devices like Google TV.
I asked Google's Reto Meier to confirm my theory was correct and he said "Correct. The Geocoder is part of the Google API add-on that isn't part of the AOSP."
So any device that doesn't come with the Play Store, GMail apps etc… will also be missing the Geocoder back-end.
There seems to be another possible workaround for this problem, which is unfortunately marked as a duplicate question, and therefore might be missed. Essentially, a reboot of the device clears up the problem. Note I called it a "workaround" and not a "solution". :(
For those who searching alternative, Hopefully, my answer in another post is useful.
You can use Google Geocoding API when caught error in geocoding.
For more code => Get current location using json
Some devices do not have suport for Geocoder, so what you need to do is create your own geocoder.
Basicaly you need create a async task to request google for the address and treat the json response.
Using aquery, i do something like this:
public void asyncJson(String address){
address = address.replace(" ", "+");
String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+ address +"&sensor=true";
aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {
#Override
public void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
//here you work with the response json
JSONArray results = json.getJSONArray("results");
Toast.makeText(context, results.getJSONObject(1).getString("formatted_address"));
}else{
//ajax error, show error code
Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
}
});
}
After wasting several hours, I got a simplest solution.
I Just restarted my device, and it started working.
It seems, the problem is due to some OS level caching problem. Hope it will also work for your..
I had the same issue. I used the following function.
Note:
Use context of your Activity and don't use getApplicationContext() to the following function
public static Address getLocalityFrmGeoCoder(Context context, Location mLocation) {
try {
if(mLocation!=null){
Geocoder gCoder = new Geocoder(context, Locale.getDefault());
List<Address> address = gCoder.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1);
if (address.size() > 0) {
return address.get(0);
}
}
} catch (Exception e) {
Log.d("GEOCODER", "GEOCODER EXCEPTION");
e.printStackTrace();
}
return null;
}