Geocoder getFromLocationName() returns just 1 result - android

Android Geocoder always returns just 1 possible address
val addressesList = Geocoder(this.context).getFromLocationName("Toledo", 5)
Expected : I want to get 5 possible addresses, which has Toledo in their names (
there are
Toledo, USA;
Toledo, Spain;
Toledo, Columbia, etc.)
Actual : Always get 1 address.

The Geocoding API is not meant to be used for ambiguous queries such as "Toledo". It can be expected that the API won't necessarily return all possible matching results.
I suggest using Autocomplete instead to get multiple results for broad inputs. Google's documentation states:
In general, use the Geocoding API when geocoding complete addresses
(for example, “48 Pirrama Rd, Pyrmont, NSW, Australia”). Use the
Places API Place Autocomplete service when geocoding ambiguous
(incomplete) addresses.
Hope this helps!

Related

How to use Google geocoding API?

I have an Android application and I am using the Google geocoding API to get GPS coordinates for an address. Currently I am using the following URL which works correctly:
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA
The problem is, will the above API continue to work after the Google's 'new pricing changes' starting July 16, 2018? According to the Google API documentation here, https://developers.google.com/maps/documentation/geocoding/intro#GeocodingResponses, the correct way to perform geocoding is using the following format:
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
I have tried using the suggested format (created a project, enabled billing, enabled the Places and Maps APIs, created an API key, added security to the key so that only my app can access the key) but when I use the suggested format I get the error,
This IP, site or mobile application is not authorized to use this API
key
Reading other questions on StackOverflow, I found the following suggestions, among others:
Creating a server key and using it instead of the API key
Not adding restrictions to the API key
Using the first format of the API request (without the API_KEY)
Wait for 10 minutes until the new KEY becomes active
I tried all of them except 1 and none works. Regarding solution #1, according to the documentation, the API needs an API key so I cannot see why a Server key would work.
Solution 2 is risky, solution 3 is not certain that it will continue to work with the pricing changes and solution 4 does not work either (I waited for hours without success).
I even created a new API KEY and added no restrictions to it and I got a limit exceeded error.
Can anyone provide any help on this?
Based on your comment above then the quickest approach would be to reverse geocode the address with Geocoder:
if(Geocoder.isPresentt()){
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocationName(addressName, 3);
for(Address address : addresses){
Log.d(TAG,"Lat:" + address.getLatitude() +", Lng: " + address.getLongitude());
}
}
Using a proxy service (Solution #1) is the way to go if you are restricted to using the google Geocoding API.
Your app is being denied because google has no way of knowing what app is making the request with just the API key. If you look at the available API's, some say "for Android" and "for iOS", these are the ones that you can restrict and use natively.
Using a proxy will work (your assumption it won't is wrong) because you can restrict your API key to the IP of your service, and then require authorization from your app to use the proxy endpoint. This will protect your API key from being stolen and abused.

Google directions api return zero results for some Addresses

I have studied all previous questions but did not found solution. I'm working on Google Directions API,to draw route in android. I am getting addresses from Google Places and it draws a route but there are many places where Directions returns no result.While I have checked google map and some other apps they render the same address.Then I tried the simple query in browser but the problem remains same.Help me please I am stucked here, I will be very thankful to you.Origin address is problamatic
Here is my query
https://maps.googleapis.com/maps/api/directions/json?origin=University%20of%20gujrat,gujrat,punjab,pakistan&destination=gujranwala,punjab,pakistan&key=
And the example address is "University of Gujrat, Gujrat, Punjab,Pakistan"
I am attaching browser output. Thanks in advance.
browser output
I actually tried your given query and it returned a "status" : "OK".
However, for some addresses that you got zer0_results, as discussed in Directions Response Elements, the following are the indications of values in the status field within the Directions response:
NOT_FOUND indicates at least one of the locations specified in the request's origin, destination, or waypoints could not be geocoded.
ZERO_RESULTS indicates no route could be found between the origin and destination.
To avoid these error codes, please check parameter values that you have entered in your HTTPS Directions requests. As standard in URLs, all parameters are separated using the ampersand (&) character. The list of parameters and their possible values are enumerated in Request Parameters.

Missing Pin Codes in Google Maps API

I have an android app that handles logistics for me in my city (Mumbai, India). The user types in his address followed by his pin code. I use google maps API to find the (lat, long) of the area marked by the pin code and calculate which one of my stores is closest to it. That store fulfills the customer's order. So far it has been fairly successful but recently I discovered some users orders were not tended to because an error was returned on querying the pin code's location.
This is the url i send an HttpRequest to
"http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:"+uri[0]+"|country:india"
So for example if I type the link below. I get the approximate location of the vicinity marked as 400006
"http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:400006|country:india"
But If i input some pin codes such 400036, where I have been getting orders from the JSON I get returned is this
{
"results" : [],
"status" : "ZERO_RESULTS"
}
Could someone please help me out/ explain what exactly is happening. There are a few other pin codes where I am not getting a result.

Android reverse geocoding requires either an Internet connection or a backend data provider

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

Geocoding API vs 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).

Categories

Resources