How to use Google geocoding API? - android

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.

Related

android-reverse geocoding-Google Map API (geocoding)

I'm developing android app where I use google reverse geocoding API webservice in the user side, for the user to read his address in text format.
HttpGet httpGet = new HttpGet(
"http://maps.googleapis.com/maps/api/geocode/json?latlng="
+ lat + "," + lng + "&sensor=true");
I have the following three questions:
1-Do I have to use a map in my app?, as I'm only displaying the location in text to User and I don't want a map in my app?
https://developers.google.com/maps/terms#section_10_1
"(h) No Use of Content without a Google Map. You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation"
2-is it a must that I include API key at app side part of calling the webservice?
3-am I expected to establish enterprise agreement with Google, and get client_ID and include it in the request sent by the end user?
https://developers.google.com/maps/terms#section_9_1
To answer the questions:
1) I think you can use this without using the maps, for, in the API doc it said:
The Google Geocoding API provides a direct way to access a these services via an HTTP request.
https://developers.google.com/maps/documentation/geocoding/#Geocoding
2) You need an API key for using the service, even it works without using one (not recommended). Refer to this section for more information.
All Maps API applications* should load the Maps API using an API key. Using an API key enables you to monitor your application's Maps API usage, and ensures that Google can contact you about your application if necessary.
https://developers.google.com/maps/documentation/javascript/tutorial#api_key
3) for client_id, I think you don't need to unless you have to..
https://developers.google.com/maps/documentation/business/

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).

Why is my api key not accepted by the Google Directions (Android) request?

Whenever I include my Google Play Services API key along with my request for Google Directions like in the code below:
private String makeDirectionsURL(double originLat, double originLong, double destLat, double destLong)
{
StringBuilder url = new StringBuilder();
//first part of url//
url.append("https://maps.googleapis.com/maps/api/directions/json?");
//start adding parameters//
//origin coordinates
url.append("origin="+originLat+","+originLong);
//destiniation coordinates
url.append("&destination=");
url.append(destLat+","+destLong);
//api key
url.append("&key=");
url.append(getResources().getString(R.string.google_api_key));
//NOTE: for some reason, the request suceeds when leaving out the api key
return url.toString();
}
When I include the api key as a parameter in the request, the json response shows that my request has been denied. The response reads:
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key.",
"routes" : [],
"status" : "REQUEST_DENIED"
}
Yet the same api key works for my google map view - and my api console registers the accesses for the map view quota limit. Even odder yet, when I leave out the key parameter in the Directions request I get a valid JSON response.
I have a feeling that I have to generate another different api key for just the Google Directions service - but I'm not sure how to. The documentation here:
https://developers.google.com/maps/documentation/directions/#api_key
Says to visit the console and activate the Directions API service - I did.
Then it says my api key "will be available from the API Access page, in the Simple API Access section. Directions API applications use the Key for server apps." Now is where I'm confused - how can I use the key for server apps if I am accessing from a mobile device - I'm assuming this is for any webpages that wish to use the service - but what do I do for an android app. As I said, I already tried using my Simple API Access key for Android apps, which I know works, yet when I pass the same key to Google Directions - it mysteriously doesn't work...
Any help, vague guidance, or links to read up on would be really appreciated.
PS: If I can't figure this out - am I allowed to keep sending requests w/o a api key?
I think the issue is because you are trying to pass a server key which is directly tied to your server's IP address, when you should be passing an android app key.
In your Google APIs console, navigate to API's and Auth. From here you can create a new public API key.
When prompted, select Android Application
You will need to enter your device's SHA1 Certificate Fingerprint
This comes from the keystore used to sign the apk. When running an app in debug, you will most likely be using your IDE's debugging keystore. Google has a nice write up on how to get this information.
When releasing the app, you will need to sign it with your own key. You will either need to edit your API key to the new keystore SHA1 fingerprint or create a new API key.
Additionally, according to Google, you should be using a key.
All Directions API applications should use an API key. Including a key
in your request:
Allows you to monitor your application's API usage in the APIs
Console. Enables per-key instead of per-IP-address quota limits.
Ensures that Google can contact you about your application if necessary.
While I can't comment on how strictly they enforce this, I strongly encourage you to do so to avoid any issues down the road.

Google Places API autocomplete - REQUEST_DENIED

I'm having a difficult time getting a valid autocomplete response from the Google Places API. I'm using code based on [their example}(https://developers.google.com/places/training/autocomplete-android) (which, interestingly is using a seemingly invalid country code of "uk").
I have enabled Places API in my Google Code Console.
My API key was generated when I first enabled the Maps API a couple of weeks ago, and the MapFragment I'm using is working perfectly, so I have no reason to suspect that the API key is invalid.
The URL I'm using is https://maps.googleapis.com/maps/api/place/autocomplete/json?input=New+York&sensor=false&key=my_valid_api_key&components=country:us
I always get {"status":"REQUEST_DENIED","predictions":[]} as my response.
I have verified multiple times that my url is formed correctly and adheres to the requirements of the Places API autocomplete endpoint, yet I do not get the response I'm expecting.
I have seen a number of suggestions of how to fix this on SO and elsewhere, but none of them have worked for me (i.e.-using http instead of https, use port 443, etc.) I have tried these in all reasonable combinations with no change in the response.
Please help me find what I'm doing wrong.
I tried the URL you provided with my key and it returned the expected results. I suspect you are using an Android key instead of a Browser key. Try creating and using a Browser key (under "Simple API Access").
Use the server or browser key instead of the iOS key. Keep the refers field blank to allow all refers.
Here is a sample project which does the same :https://github.com/manishnath/Autocomplete
Try this way https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Aus&types=geocode&language=eng&key=your_APIKEY
also refer this like
http://codetheory.in/google-place-api-autocomplete-service-in-android-application/
REQUEST_DENIED indicates that your request was denied, generally because of lack of a sensor parameter.try this
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&sensor=true&key=AddYourOwnKeyHere

Google places autocomplete api - REQUEST_DENIED

I'm creating app that use autocomplete places google api.
And I don't know what is wrong but all the time I got responses like:
{
"predictions" : [],
"status" : "REQUEST_DENIED"
}
I send req : https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Paris&types=geocode&language=fr&sensor=true&key=mykey
Documentation says :
REQUEST_DENIED indicates that your request was denied, generally because of lack of a sensor parameter.
I have sensor parameter, maybe it is something with api key.
Is there any page where can I get google's log :), to see why I got REQUEST_DENIED?
I use api key from :
http://code.google.com/intl/pl-PL/android/maps-api-signup.html
Maybe I have to get an api key from somewhere else.
You should be able to view your API Key by going to the API Console first:
https://code.google.com/apis/console
If you haven't yet, you will need to create a new "Project" and then enable the Predictions services first by using the On/Off switch.
Please post your results, I have a similar problem with getting the REQUEST_DENIED response, but I've already got the API key and I'm using the sensor parameter like their documentation states.
It is frustrating that Google doesn't give any sort of response code along with REQUEST_DENIED so you can see why it's denying it. This will be difficult to support and troubleshoot in a production environment if this randomly goes out without providing any details as to why.
Update:
I just found this works for me now, but only after changing my requests to GET requests instead of POST. For reasons unknown, Google returns REQUEST_DENIED for POST requests but allows the same request via GET. A quick test for you to see if this is your problem would be to put the URL together with your API key and other parameters and try it through your browser instead. If that works then just rewrite your Android code to use GET instead of POST and you should be all set.
It was solved for me when I exchanged the API key. I was using the one under Android device, I changed it to the one "Key for browser apps (with referers)" and it worked for me although I'm using Android Device.
I recommend using .getJson() to get an error message along with your response to know what is exactly wrong.
For my case in flutter: I used googlePlace!.autocomplete.getJson(value);
and i got the following response.
{ "error_message" : "You must enable Billing on the Google Cloud Project at https://console.cloud.google.com/project/_/billing/enable Learn more at https://developers.google.com/maps/gmp-get-started", "predictions" : [], "status" : "REQUEST_DENIED" }
That's the wrong API key. Follow the steps here to get the right one.
Make sure you API keys are generated correctly and that you have enabled the necessary APIS.
In my case, when designing iOS application - I need to enable Google Maps SDK for iOS, Google Places API for iOS
And most importantly, and without being mentioned by Google (in any other place as well) enable Google Places API Web Service.
With this, I ended a 2 day search for my pesky problem.
And, of course, make sure you use Key for server applications

Categories

Resources