Places API for Android: get address lines from Place.getAddress() - android

In my android app, I find the address of locations using Geocoder.getFromLocation(). I can get an Address object and get the address lines of an address with Address.getAddressLine().
I am only interested in the first address line, which I can get easily with Address.getAddressLine(0).
Also, app allows user to choose locations using PlaceAutocomplete. When user chooses a location, I receive a Place object and I can get the address by Place.getAddress().
The problem is, Place.getAddress() returns a string instead of an Address object. This string contains the full address, so I cannot easily get the first address line, as I did with Address object.
How should I find the first address line from string returned by Place.getAddress()?

Short answer is that you can't get structured data from that API. For some reason, Google has decided that is not something people would want on Android.
A colleague of mine has logged this issue with Google to request the same level of information as is provided to iOS and JavaScript users:
Issue 10019: Expose address components in Place object
Longer answer is that your options are:
You figure out a clever way to parse that string. Unfortunately the actual street/suburb/town/province structure is not consistent across different areas (e.g. Cape Town in South Africa has a different sentence structure to Johannesburg in South Africa). So your parsing rules need to be very clever.
You use a different Google API. The JavaScript API provides structured data for the related call. This related question shows more details on that API. Unfortunately Google recommends against using this technique.
I believe that Google only intends to give us this sentence for the purposes of picking an address, rather than for us to get structured information about the address. In order to get better information, you need to get the lat/lng values by querying the place ID.
From those lat/lng values, you can then reverse-geolocate to get the correct address.
Unfortunately in this technique, Google's APIs fail us once more.
often you can take address A, resolve it to a given lat/lng, and resolve that to another address B, which might be close to A, but is not. The API calls are not commutative as one would expect
I have had a fair amount of experience with these calls, and I wish there was another answer I could provide. If you wish to stick with Google's location APIs, then you can't get what you are asking for.

Now it is impossible with this API only.
But you can use Geocoder with latitude and longitude from Place object

Note: This information was observed at this post. Repeated below.
Actually you can achieve this, you will just need to use the GeoCoder to do the lookup. Then simply use the Address object to get the data you need.
Here is some Java code to showcase the solution.
List<Address> addresses = null;
addresses = geocoder.getFromLocation(latitude, longitude,1);
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
// Thoroughfare seems to be the street name without numbers
String street = address.getThoroughfare();
}

Related

Getting address fields from a Place object

We are using Google places android API to allow our users to use the autocomplete widget in order to quickly select addresses.
The widget itself returns a Google Place object which exposes a getAddress() method to return a human readable address string.
Is there a way to extract the address fields (street, city, state etc') from the Place object?
Using the lat/long for reverse geocoding is not 100% accurate (we saw cases where the house number was different).
Thank you.

Google maps api geocode returns zero results for valid address

http://maps.google.com/maps/api/geocode/json?sensor=false&address=
I was looking at the above api and trying to find the location of the address "Grand Copthorne Hotel"..
However the result I am getting in the api is as follows
"status" : "ZERO_RESULTS"
Please let me know why this is taking place as it is affecting my entire platform and if its a problem from your end when should I expect the issue to be solved
Please let me know about the above issue
Geocoding API filters out businesses, it works only with street addresses.
If you need the location of the business you have to use Places API search.
E.g.
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Grand%20Copthorne%20Hotel&key=YOUR_API_KEY
Please refer to the documentation for further details:
https://developers.google.com/places/web-service/search
The Google Geocode API filters out businesses and only works on addresses. You said you have a valid address, but "Grand Copthorne Hotel" is not a valid address. An address provides enough information to mail something to a specific person or business.
If you want to retrieve location information with a generic search term, you should use a different API.
You can try some searches in the Google Places API search box; maybe that is a good API for your situation. It returns lots of information, including latitude and longitude. Check out the documentation.
The first thing you should do is discover and decide what kind of API you actually need. Do you need information for product shipping? Do you need to show a map to a user? Do you need to retrieve businesses' information?
Keep in mind that there are many kinds of location searches and varying services that return latitude and longitude. For instance, there is geocoding (generally matches addresses or partial addresses to a fairly accurate latitude and longitude pair); there is address validation (matches addresses to a database of information—probably postal service information—to provide very accurate location information for real addresses); there is places search (search for names, icons, titles, or business terms and retrieve location information). There are still other options. Just take a look at one list of mapping-related APIs from Google...
Full disclosure: I work for SmartyStreets, an address validation service.

Get a list of street addresses within a lat/lng radius

I'm developing a android application that uses the user location to show a list of street name suggestions around their coordinates, since location retrieval is not going to be accurate enough, and in my application i need the best accurate position.
I tried a lot of approaches, tested google reverse geocoder, but it only gives me only one address, i tried google places api but it's not intended to return addresses, but places, what's not i'm lookin for, i tried findNearBy streets of geonames, but it only gives me street names, i need full data of the streets, like, city, state, country... i tried to use both geonames and google geocoder, getting a list of names, and converting each name into reverse geocoder request, but some street names are also available in another countries, i dont know how to specify a name and a latlng in geocoder request to filter results near specific coordinate.
I tried apis on server-side, which is a django-python server, the scenario is: the user sends their coordinate to server, which will use user location to request to another api's and format the result to the android, but didn't manage to found a good and fast solution.
I've seen a lot of some similar questions on SO but none had the answers I am looking for.

Geocoder returns empty for only one address

I'm using the Geocoder to retrieve lat and long coordinates from a list of addresses. It works perfect with all of them except for one, where it returns empty. I've tried the address on Google Maps (on device app as well as web browser) and it is a valid address, showing its exact location.
Any ideas of how to mitigate this?
This is the address that doesn't get processed: "315 Iron Horse Way, Ste. 101, Providence, RI 02908"
One option is to call the Geocoding Web Service directly.
It does return a valid response for that address, check for yourself:
http://maps.googleapis.com/maps/api/geocode/json?address=315%20Iron%20Horse%20Way,%20Ste.%20101,%20Providence,%20RI%2002908&sensor=false
Find more info here.
By the way, check also the usage limits:
Use of the Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day. (User of Google Maps API for Business may perform up to 100,000 requests per day.)

Geocoder getFromLocationName is not finding address

Geocoder getFromLocationName is not returning anything if I'm searching for business places. Here are my finding so far:
grocery places, commercial buildings -> no result
schools -> finding results, but only returning Latitude and Longitude. I have to do reverse geocoding in order to get the address based on the coordinates, but the address wouldn't be accurate.
complete address -> same as school, only returning Latitude and Longitude
hospital and pharmacy -> returning Latitude and Longitude
I've tested this on 1.6, 2.1 and 2.2 devices that has Google Maps.
I'm trying to find if there's a limitation explained somewhere on the SDK, but there is none except:
The amount of detail in a reverse
geocoded location description may
vary, for example one might contain
the full street address of the closest
building, while another might contain
only a city name and postal code. 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
(http://developer.android.com/reference/android/location/Geocoder.html)
[update]
So after nights of research, I can't seems to make it work - Google simply wont give me the result. Some discussion on android-developers points a fact that getFromBusinessName was omitted when Google Navigation came out. So I'm starting to assume that Google is pulling back some of their geocoding service.
I can always create a webpage that use Google Geocoder API, but its limited to 2500 api call - and I dont think it will be legal (lol). So what are my alternatives here?
I've hit the same problem. I'm finding that the geocoding API often returns no results if I include the business/venue name, but by missing it off, the lat/long can be much less accurate.
I've implemented a pragmatic solution:
1. Call the API with a string including the business/venue name
2. If it fails, call it a second time without the business/venue name, just the address
I've yet to see a case where no location is returned, so it seems a decent compromise as it means I do at least get location data, if sometimes less accurate, and the cost of a second API call is mitigated with threaded calls.
I couldn't find the button to reply but, like you all, I have just run into the same problem. I haven't tried it yet but it sounds pretty solid. A potential workaround is located here:
Using Google Maps API to get Address of Business
Happy coding :)
Edit: Forget that link and check this one out: http://code.google.com/apis/maps/documentation/places/
Try this approach:
1) Use Geocoder to convert latitude-longitude to address
2) Make an API call to Whitepages (or similar service), specifically the reverse_address method which will get you business name for the address mentioned.

Categories

Resources