Find city in Google Geocoding API - google-maps

I am using google Geocoding API to store the cities in the DB on demand.I provided an input field for the user to enter their city.I used googles places autocomplete for that.
I would like to store the city and its country what ever enter ed in the input field. Calling google geocoding API with the with data from input field, returned data in JSON.
I looked into address_components got country in it. Then I took object in address_components where types = locality. The problem is,For some locations typed in the input field, The JSON from google geocoding API doesn't have types = locality. So in these cases, which one among the objects in address_components should I store as city in my DB.
this website is exampale of google geocoding api https://findcity.in/
Need suggestions. The question may not be clear earlier. So I edited it. Thanks

You are right, the type locality is not always present in the address components array. Depending on the country or even regions of the same country the city name might be found in different address components. This depends on the data modelling for certain area and address templates that uses Google. Unfortunately, there is no documentation for this on per country basis.
In the official documentation you can find the following explanation
The selection of specific address components in this example is based on a typical address format. You may need to select different components to align with the postal address formats of different countries. For example, the code selects the locality component, which often represents the city part of the address. However, note the following examples of how the components may differ:
In the UK and Sweden the correct component to display the city is postal_town.
In Japan, the component differs across prefectures.
Brooklyn and other parts of New York City do not include the city as part of the address. They use sublocality_level_1 instead.
source: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
I hope this helps a bit.

administrative_area_3 indicates a third-order civil entity below the country level.
An example of this would be a search query for Montgomery returning Montgomery County in your specific state before Montgomery, AL. This is partially due to region biasing returning specified results based on your current location.
Here is some documentation from Google Which goes over the GeocoderResult.
I'd like to encourage you to make sure to take a look at the Google Maps APIs reference guide before asking a question directly. It is rather robust and you can find a lot of useful information there.

Related

How do I find city type in Google Autocomplete API?

I'm retrieving Google autocomplete data in JSON format, and I'm keen to know in which Object Array does Google sends "City" data because when I analyze the address types sometimes the city is found in post_town other times it is found in locality, sub-locality, administrative_area_level_1, administrative_area_level_2, administrative_area_level_3, administrative_area_level_4, administrative_area_level_5 .
The reason to find the city in correct type is that I need to write a code where I'll be using Google-type to identify the city and then storing into my database.
Can someone please help me identify to solve this issue?
Note: If you see the image below the city is identified in type: postal_town:
enter image description here
enter image description here
Unfortunately, there is no complete documentation about address components structure based on each country.
The only place I know in the documentation that mentions address components for cities is the following page
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
It reads
the sample code selects the locality component, which often represents the city part of the address. Examples of how components can differ include:
In the UK and in Sweden, the component to display the city is postal_town.
In Japan, components differ across prefectures.
Brooklyn and other parts of New York City do not include the city as part of the address. Instead, they use sublocality_level_1.
For other countries you should figure it out yourself based on the Google Maps API responses.
I hope my answer clarifies your doubt.

What is the recommended format for search query in Google and Apple maps?

I want to show the direction from my React Native app to the specific address in Google or Apple maps application depending on the Platform. Are there any requirements for address query format so I can get better search results?
I've found following for Google https://developers.google.com/maps/faq#geocoder_queryformat. Any ideas for Apple Maps?
Here what I’ve found in official docs
Google
How should I format my geocoder queries to maximise the number of successful requests?
The geocoder is designed to map street addresses to geographical coordinates. We therefore recommend that you format geocoder requests in accordance with the following guidelines to maximize the likelihood of a successful query:
Specify addresses in accordance with the format used by the national postal service of the country concerned.
Do not specify additional address elements such as business names, unit numbers, floor numbers, or suite numbers that are not included in the address as defined by the postal service of the country concerned. Doing so may result in responses with ZERO_RESULTS.
Use the street number of a premise in preference to the building name where possible.
Use street number addressing in preference to specifying cross streets where possible.
Do not provide ‘hints’ such as nearby landmarks.
https://developers.google.com/maps/faq#geocoder_queryformat
Apple
Use the CLGeocoder class with a dictionary of Address Book information or a simple string to initiate forward-geocoding requests. There is no designated format for string-based requests: Delimiter characters are welcome, but not required, and the geocoder server treats the string as case-insensitive. For example, any of the following strings would yield results:
“Apple Inc”
“1 Infinite Loop”
“1 Infinite Loop, Cupertino, CA USA”
The more information you can provide to the forward geocoder, the better the results returned to you. The geocoder object parses the information you give it and if it finds a match, returns some number of placemark objects. The number of returned placemark objects depends greatly on the specificity of the information you provide. For this reason, providing street, city, province, and country information is much more likely to return a single address than providing only street and city information. The completion handler block you pass to the geocoder should be prepared to handle multiple placemarks, as shown below.
[geocoder geocodeAddressString:#"1 Infinite Loop"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
}
}];
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html

Google Geocode API Address Formatting United States

This question is similar in spirit to. http: //stackoverflow.com/questions/7764244/correct-address-format-to-get-the-most-accurate-results-from-google-geocoding-ap
I've read the FAQ on the google geocode developers guidelines and it states that the suggested format for addresses is based off the country's postal service. In some of my testing this is not always working and sometimes I'm seeing better results putting the street at the end of query.
example being: http://maps.google.com/maps/api/geocode/xml?address=+3700%20W%20FLAMINGO%20RD++LAS%20VEGAS+NV
This returns zero results, but swapping road to the end returns a correct result:
http://maps.google.com/maps/api/geocode/xml?address=++LAS%20VEGAS+NV+3700%20W%20FLAMINGO%20RD
On top of that if I include the zipcode (89103) to the second request, that one will return no results as well.
Can anyone help me to understand what the appropriate format should look like for US addresses?
Thanks much!
-Chris
The answer you can find in this document
https://developers.google.com/maps/faq#geocoder_queryformat
How should I format my geocoder queries to maximise the number of successful requests?
Specify addresses in accordance with the format used by the national postal service of the country concerned.
Do not specify additional address elements such as business names, unit numbers, floor numbers, or suite numbers that are not included in the address as defined by the postal service of the country concerned.
Use the street number of a premise in preference to the building name where possible.
Use street number addressing in preference to specifying cross streets where possible.
Do not provide 'hints' such as nearby landmarks

Is there any Google API to check if an input is a city or not?

Is there any Google API / classes, that can be used to check if a text input is a city or not a city?
I'm developing a real estate iOS app. My app uses GoogleMapSDK for iOS v1.8.1.
if you are just after US cities , there are many places on the internet that you can get every city and zip code for free - like the US Post Office. If you can't store all that or don't want to and really want to use Google - then the exact API you are looking for does not exist. BUT you can actually get the info you want out of the Google API, it will just take some work. It is easy to send the input to Google Geocoding API, then parse the response and see if the response city matches what the user entered. I know... this is not ideal, but there are ways to make it look like it's working the way you want from the user's point of view even though the exact API doesn't exist.
Use place/autocomplete/json API:
It gives a list of response based on query
Simply add "types=(cities)" to your query and Google will return you a list of cities
Use geocode/json API
Good tool to get GPS location from an address (eg. New York)
Simply check the "types" field returned from API, check if it's "locality" or "administrative_area_level_3"
Note: two exceptions are Hong Kong and Macau, MO both are treated as "country" but they are cities

Google Maps Geocode - Wildcard searches beyond exact matches

Anyone know if the Google Maps Geocoding service can return results including locations that start with the search term when it finds an exact match for the search term?
Here's my example
In Australia if I search for the term "Bass", I get exactly one result because there is one town in Australia called Bass. But I also want to include any other town that may start with Bass - EG "Bass Hill" and "Bassendean".
I haven't found a reference or example - any help would be appreciated!
I doesn't see any way to do this.
Found this from google geocoding reference.
How should I format my geocoder queries to maximise the number of successful requests?
The geocoder is designed to map street addresses to geographical coordinates. We therefore recommend that you format geocoder requests in accordance with the following guidelines to maximise the likelihood of a successful query:
* Specify addresses in accordance with the format used by the national postal service of the country concerned.
* Do not specify additional address elements such as business names, unit numbers, floor numbers, or suite numbers that are not included in the address as defined by the postal service of the country concerned.
* Use the street number of a premise in preference to the building name where possible.
* Use street number addressing in preference to specifying cross streets where possible.
* Do not provide 'hints' such as nearby landmarks.
Now, given above it(google geocoding webservice) isn't going to assume the inputs given by you for some other input. It tries to map the inputs given by you to the street address, city, country or something else like that.