Google Maps Geocode Issues: Certain Encoded URLs Failing - php

I am trying to get Lat/Lon for a list of locations from an address. It works for most addresses, but certain ones fail and return Zero Results.
Take this address as an example: 1045 Mono Way, Sonora, CA 95370
As you can see, that is a valid address on Google Maps - no issues there.
When pinging the Geocoder Api, I am encoding the address using urlencode(). This is the encoded address: 1045%20Mono%20Way%2C%20Sonora%2C%20CA%2095370
This is the link that I am accessing:
https://maps.googleapis.com/maps/api/geocode/xml?address=1045%20Mono%20Way%2C%20Sonora%2C%20CA%2095370&sensor=false
As you can see, zero results.
Now, if you change the first part of the address (1045) to any other number (e.g. 1044), you will get valid results.
I am lost.
Need help.
Please ;)
BTW, I am using this reply to get the lat/lon from the API: Google Maps - converting address to latitude & longitude - PHP backend?

I would bet that address just doesn't exist. The addresses are provided by local government, typically in a range of numbers that corresponds to a stretch of street. The fact that trying odd numbers doesn't work (1045, 1047, 1049) but even numbers do (1044, 1046, 1048) suggests that the addresses on one side of the road are not there. Or at least not in the dataset provided by the city or county.
The ideal solution would be a "fuzziness" argument to the API call, which would return a location similar to what their Maps site is. But since we can't rewrite third party APIs, I'd suggest checking for a "ZERO_RESULTS" response, and then resubmitting the request with just the street name if needed.
Also, you can do a geocode search without your API key, so you may want to keep that out of your question!

Related

Querying a MySQL database to show properties within a certain radius of a UK postcode using PHP

I have a MySQL table containing a list of properties in the UK. The table contains the full address of each property including the post code. I need to be able to query the database to show all properties within a certain radius (e.g. 3 miles) of a given post code.
I've searched SO and couldn't find a similar question.
Research on Google seems to suggest I need to use Google Maps but I can't find any material to get me going. I don't need to display the results on a map at this stage, a simple list of the properties will suffice.
I'm not asking for someone to code this for me (although that would be nice!), I'm just after a point in the right direction.
Thanks.
If your properties are already accounted on the Google Server the Google Places API would be able to retrieve them. You just need to set the radius of 3 miles as parameter.
https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters
More details related to the parameter passed can be found in this official documentation.
If these places are not accounted in Google than they wont be displayed after requesting this REST API. Then you have to follow the standard procedure of:
Creating a query of fetching the requesting the address in within 3 miles with given postal address.
Select address from table where radius=3 & postalCode=xxxxx;
When you have the address convert them into Lat/Lng using Geocoder API.
Finally display them on Google Maps.
Hope this helps!!

How to mark locations on google map by having array of ip addresses, not lat & lng coordinates

I know how to mark locations on google map using Google Maps API v3.
How to do the same if I have a php/javascript array of ip addresses, not lat and lng coordinates?
You'll need some way to convert them from IP addresses to co-ordinates - to which there is a post on StackOverflow asking this very question.
My favourite is IPInfoDB which offers the service for free and can return you the information you require using a very simple GET request:
http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>&ip=74.125.45.100
If you replace ip-country with ip-city then you get city level precision.
You need a ip-geolocation service that can convert the IP address into approximate geographical coordinates. See something like http://ip2location.com/ which I believe offers a paid API service. I don't know if there is any unlimited free service to do the same.
I like http://www.iptolatlng.com/ because it gives me staight JSON and it gives me the results I want in the easiest fashion.

How to extract addresses in PHP, from a source with no standard format of writing addresses?

I have a bunch of messages (from twitter) that include addresses. They are in various kinds (as many as you could imagine a random sampling of people enter an address. The city location is always known so they normally just put a road name and number/area)
Is there any library out there to extract these? I've tried looking but found nothing.
If no, any suggestions as to how I do this? At the moment I am just extracting things like [previous word + [rd/ave/street/lane/blvd]] but it isn't that accurate.
Any ideas?
Thanks
I know of no library that does this.. but a crazy idea came to mind while reading your question.
Use the google maps geocoding api to find long and lat for your address..
then use the reverse geocoding api to find the address from your lat and long since it will be neatly formatted in a json object.
Quite messy but it is the best i can come up with. (Has the upside that you then already have the coordinates of your address :)

Location searching

For the site I am building I want the user to be able to search for a location, however because I don't have a list of locations for the UK, I have developed a script in PHP which calls: http://ajax.googleapis.com/ajax/services/search/local in order to get the lat and lon coordinates of this location to store in the database for future use.
This works well apart from if I search for a town, for example Heaton, it brings back the one in Staffordshire rather than the one in Newcastle for example.
The whole reason I'm using the google ajax api is to cut down the google requests per day because I don't want to exceed the daily request limit.
As you can see though, I've hit a brick wall because the search isn't refined enough to bring back the correct location.
Can anyone suggest another way around this? I'm quite stuck. Thanks
I don't know if you have seen this already, but there is a parameter in the Google Search API that allows you to specify a boundary for the search:
GeocoderRequest object specification
The specification for a geocoding
request to be sent to the Geocoder.
Properties Type Description
address string Address. Optional.
bounds LatLngBounds LatLngBounds within which to search. Optional.
language string Preferred language for results. Optional.
location LatLng LatLng about which to search. Optional.
region string Country code top-level domain within which to search.
In your case I would specify a bounds around northumberland, so something like:
http://ajax.googleapis.com/ajax/services/search/local?q=heaton&v=3.1&region=GB&sensor=false&bounds=(55.395,%20-2.510%20,2054.342,%20-0.417)
might work...? I don't know if this would be relevant to your app though. Good luck!

Determine if a string is a valid geographic location

I've got a bunch of "locations" - some are accurate (gaborone, botswana), some are geocodes (40.75,-73.997) and some are completely useless (#siliconcape). I need to find a way to run through the list and determine the City and Country of each string and geocode, and return nulls for the invalid locations.
Is there some sort of library/service/api/method that can be used to determine whether or not a given string represents a valid geographical location? While accounting for typos, ordering errors, etc?
Probably the easiest method would be to use something like the Google Geocoding API. It'll take a string and attempt to parse it to a location. You can get output as XML, JSON, CSV.
Here's some example CSV output:
input : gaborone, botswana
output: 200,4,-24.6541100,25.9087390
input : #siliconcape
output: 602,0,0,0
input : 40.75,-73.997
output: 200,8,"324 W 30th St, New York, NY 10001, USA"
I think I've found a solution - just sharing it here in case anyone else needs it:
Use Google Maps API geocoding - feed it addresses and get co-ordinates back. Details for doing it via PHP: http://www.phpriot.com/articles/google-maps-geocoding/

Categories