6

I want to build a map with a search input. The user should be able to search for zip or city with that input. This appears not so be well documented in the Google Geocoding API or in the Javascript API reference for the Geocoder.

I tried to use the geocode function as follows:

geocode({ address: inputValue })

but this will give results in the whole world.

Then I used the region and the bounds:

geocode({ address: this.value, region: 'XX', bounds: myCountryBounds })

This will still give results from the entire world. (I read on one of the documentation pages that the region and bounds are only informative and not strict restrictions, but I cannot find the page anymore.)

Then I used only componentRestrictions:

geocode({ address: this.value, componentRestrictions: { country: 'XX' })

This now gives me only results in country XX. But it also gives me street and company names.

Now I want the search only to match zips and cities. Because if I search for 2000 I find lots of company names that have 2000 in their names. So how can I make sure the search is only performed in zips and city names?

Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
  • I've added a solution below, it should address your needs, but it's not strictly using geocoder. The key is the stackoverflow link in step 1. – Zachary Moshansky Mar 14 '14 at 00:47

2 Answers2

0

As an alternative to geocoder, using the places API may achieve the same end goal (Needs API Key). My apologies if you need something specific to the geocoding api and the geocoder library. (The reason I answer with this alternative is because I'm struggling to achieve the exact same and have been really stuck).


TL-DR; Alternative Solution:

0.) Get "places" Api key https://code.google.com/apis/console

1.) Make a request that only returns high level regions (cities).
Google Maps places API to only return cities?

2a.) You can follow up by issuing a request with the "places details api" using the reference returned in the first request. That will then return lat/long. https://developers.google.com/places/documentation/details#PlaceDetailsRequests

2b.) Or use the nicely formed "City, Country" from part 1 to query the geocoder api with and get your lat/long with the benefit of saving places API calls. I'm not sure why the "type=(regions)" isn't available on geocoding API, but it would help (Or maybe I missed it).


This method would allow for text or zip code search, and return a lat/long as well as address details. I also found that using component filters with the places api yielded much better results than with the geocoding api. You can use this with up to 100,000 requests/day for free with a verified account.

ex.) Needs API Key
1.) https://maps.googleapis.com/maps/api/place/autocomplete/json?input=va&sensor=false&types=(regions)&key=YOUR_API_KEY

2b.) https://maps.googleapis.com/maps/api/place/details/json?reference=CjQtAAAA7uEa6LT_DWvJ-erYl51zOqxcDHT9DMzo6LIQLRIF-g0HekbODavL5hHDleYSioGYEhCULLbxKZBlYZXs-I-EWkGQGhRiXW7LMokjjTp7WZjXE02wNzHkzQ&sensor=false&key=YOUR_API_KEY

Community
  • 1
  • 1
Zachary Moshansky
  • 1,673
  • 18
  • 32
0

I've added the word 'City' to the search phrase when doing a geocode request and it seems to work