Google Maps API: Searching - google-maps

I'm working on a shipping system for my company where user's enter (along with a bunch of other information) the destination that something needs to get shipped to. The system that we are migrating from just used this information as static text, so people would enter shorthand addresses such as "Alsip 60801".
My original thought was that that sort of shorthand text would work perfectly with Google Maps API, because I can type shorthand things into maps.google.com, and it generally works.
So, I wrote up some code to run geocoding on their shorthand inputs. To my unfortunate surprise, the results returned from the geocode search were greatly different from those that are returned from maps.google.com.
For example, when I search for "Alsip 60801" on maps.google.com, I get Alsip, IL 60801. That's correct. But when I search using the Google Maps API, I get some Alsip up in Canada.
Is there another search function that I should be using for the Google Maps API? Or some sort of flag that I am missing?
For reference, this is the short version of my code:
var geocode = new google.maps.Geocoder();
geocode.geocode( {'address': 'Alsip 60801' }, function(results, status) {
console.log(results[0].geometry.location);
//Outputs the LatLng of a Canada address
});
Edit 1: I forgot to mention, I did try setting the region on the geocode request. I set it to us, and got the same results.
Update - Answered: I marked #Jitimaro's answer as correct, because tacking the country code onto the end of the results does seem to work. I'm not sure if that will work with every sort of input (it's hard to predict shorthand address formats), but it seems to work fine for now.
However, I actually am handling this a different way. Instead of just tacking a country code onto the end of the address, I'm actually providing a LatLngBounds parameter on the end of my geocode request. That gives all addresses within my bounds preferential treatment as it is searching.

When you search with the website you should get a preview like this: http://maps.google.com/maps?q=Alsip+60801. When you search a reverse geocode you must parse the result to match your country http://maps.googleapis.com/maps/geo?q=Alsip+60801. You can check the different url with a network sniffer. Or you can add the country to your url http://maps.googleapis.com/maps/geo?q=Alsip+60801+USA.

Related

Google Maps - Show address instead geocodes

I have link with geo-coordinates (below), when I reach by this URL to the google maps page, in Input I see my geo-coordinates.
Is it possible to get their Human-readable Address instead of geocodes, without changing URL format (should be used geo-coordinates) ?
https://www.google.com/maps?&z=16&q=51.5362671,-0.11687110000002576&ll=51.5362671,-0.11687110000002576
If I understood question correctly, you need to use Reverse Geocoding. I found some links. It aren't solutions of your problem but (I hope) these examples help you to find solution.
Reverse Geocoding. This page has input where you can put coordinates and check the address. I put coordinates 51.5362671,-0.11687110000002576 and get result: Carnegie Street (Stop L), London N1, Великобританія. From code you can find how to get formatted address.
Geocoding Service. Read about Geocoding Responses: formatted_address field and Reverse Geocoding (Address Lookup). This doc also has examples how to get formatted address.
Perhaps, this link also can help you:
Reverse Geocoding: Get address from Latitude and Longitude using Google Maps Geocoding API
If you get formatted address you can use jQuery .val() method to set value for input:
var address = ... // code to get formatted address
$("selector_for_input").val(address);
Here you can find example of using .val() method.
I know that these links aren't 100% solutions of your problem but I hope you can find there answer for your question.

Google Geocode api returning incorrect result for a particular address

When I try to geocode one of the adress using Google geocode api, Google api returns an incorrect location.
search address : republic of estonia
result from google api: spain
I am using javascript Google api to geocode the address. For some other location the result is correct but for above address its incorrect.
If we enter below url in browser, we get the JSON back and it is showing result somewhere in Spain rather than showing result in Estonia.
http://maps.googleapis.com/maps/api/geocode/json?address=republic%20of%20estonia
I tried same addres in maps.google.com and it takes me to correct location.
Have a look at the Estonia country feature in Map Maker:
https://www.google.com/mapmaker?iwloc=0_0&fmi=0_0&gw=39&fid=5085290329182063613:4677726785527621059&dtab=overview&ll=58.620408,24.93212&spn=3.101226,8.668213&z=7&lyt=large_map_v3&htll=58.560236,25.449737&hyaw=278.7642140009582
If you check the list of names for this feature, you will see that there is no 'Republic of Estonia' in the list. The name is simply 'Estonia'.
The request for 'Estonia' works as expected:
https://maps.googleapis.com/maps/api/geocode/json?address=estonia&key=YOUR_API_KEY
If you believe that 'Republic of Estonia' must be in the list of the names please Send a feedback (bottom right corner) to Google from this page
https://www.google.com/maps/place/Estonia/#58.7223142,23.1414399,7z/data=!4m5!3m4!1s0x4692949c82a04bfd:0x40ea9fba4fb425c3!8m2!3d58.595272!4d25.013607?hl=en
Google assumed you were looking for "Calle Estonia" or "Estonia Street".
You should request that Google add "Republic of Estonia" to their list of spaces. Google, and most other mapping services, only have "Estonia" or "EST" in their list of places.
Also, you may want to use a different API if you need to get an accurate answer. Google is really good, but Google tries to "guess" what you mean and always gives you a result. But, a service like SmartyStreets matches searches to databases of addresses (so if the search is not a "real" place, it will tell you).
(Full disclosure: I have worked for SmartyStreets.)

how to get the complete address(es) using a business name?

I know that google maps's JSON return includes a "formatted_address" that gives you the full readable address but I've tested it on a couple of places with no success, the lat and lng coordinates aren't correct either. If I manually search it using maps.google.com it would be correct so I know the input is specific enough. How can I use the API to do this? What if the input is vague? Can I get a JSON return that includes all possible locations? (i.e. McDonalds in NYC will surely return many coordinates)
example:
http://maps.googleapis.com/maps/api/geocode/json?address=time+square+nyc&sensor=false
this returns the CORRECT coodrinates and full address that I can use.
http://maps.googleapis.com/maps/api/geocode/json?address=amc+loews+boston&sensor=false
this does not. but of course if i just search "amc lowes boston" manually it returns the correct address on google maps.
I think you're looking for Google Maps' Places Library.

Getting correct address from user typed street address using google maps

I've never used the google-maps api, but I just want to do one particular thing.
I've got around 1000 user typed street addresses. Some are missing states and postcodes. I've noticed if I type these addresses into google maps it generally gives me back the state and postcode (only very rarely it gives some options). Is there a way to do this programatically, so I don't have to manually copy/paste this in? Has someone already made an application/library to do this that I can just feed the user typed data to?
Edit:
I've noticed this does the job:
https://maps.google.com.au/maps?q=1%20George%20st%20sydney
It returns state and postcode. But it returns it on a webpage with a whole lot of other stuff. I just want the address only. I guess I could grep through the results, but some additional thing to add to the query string so it only returns the raw address (or some structure) would be useful.
You can use the Geocoding API - https://developers.google.com/maps/documentation/geocoding/
E.g. http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=1%20George%20st%20sydney ; you can replace "json" with "xml" if you prefer. Look for "formatted_address" in the result, or individual address components, depending on what you need.
However, I'm quite frustrated that it sometimes doesn't work, even for some cases where the regular google maps search is successful.

Find all parks for a given zipcode with google maps

I'm trying to use the Google maps geocode API to return all parks for a given zip code. It seems that if I have a park name I have no problem returning the data. Such as
http://maps.googleapis.com/maps/api/geocode/json?address=Kerry+Park+98122&sensor=false
However, if i search for parks for a zip code
http://maps.googleapis.com/maps/api/geocode/json?address=Parks+near+98122&sensor=false
I get any result for the zip code with parks in the address. I tried using lanlng but that has the same problem. It seems that Google doesn't allow types[] in query just the results which is unfortunate.
If I search for "parks near 98122" on maps.google.com I get all the results but it doesn't seem to be using the same API.
I requested a Google maps Places api key which I think is what I need.
I guess my questions are:
a) am I missing something here?
b) I'm not stuck with Google API are their others that will output JSON results for all parks by zip. I looked briefly into Bing and Yahoo to no avail.
Thanks.
Use Google Places API and the text search.
Change the [zipcode] and the key [apikey]:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=park+in+[zipcode]&key=[apikey]
The Google geocoder API doesn't have a provision for specifying a "type" such as "park" in the query. It will identify the feature type in the response, but you can't put it in the the request.
You might be able to do what you want in the Google Places API, now out for developer preview. I haven't tried it. You still can't specify a type in the request, but you specify a location and radius, and it returns all places, and each place has one or more associated type codes (which might include park). You can search through the returned results to see if parks appear. See http://code.google.com/apis/maps/documentation/places/
I just worked on this, I am not using a zip code, but you can take a zip code and get a lat/lng pair. The lat/lng pair already used here(map center set).
This is the query to get parks. I will say it is probably not absolute(only major parks) and there is that 20 limit.
This is from their docs btw, using Places api, also this code is from ReactJS hence ref eg. "current"
const plotParks = (parks) => {
// array of parks, use marker to plot on map and fit bounds to center
}
const service = new window.google.maps.places.PlacesService(mapTarget.current);
// radius in meters so I added mile to radius, seems correct, formula pulled from SO ha
const radiusInMeters = (Math.round(10000*(radiusMiles*5280 / 3.281))/10000);
service.nearbySearch(
{location: mapTarget.current.getCenter(), radius: radiusInMeters, type: ['park']},
(results, status, pagination) => {
if (status !== 'OK' || !results.length) {
alert('No parks found near you, try increasing your radius or try a new address');
} else {
plotParks(results);
}
});
do ctrl+f for 'store' to jump to the code
https://developers.google.com/maps/documentation/javascript/places