Different latitude and longitude using google maps and ggmap geocode function - r

I am trying to plot the nearest Walmart store in Canada to a certain post code using ggmap. The function used to get the location is:
goodPoint<-geocode("M1L2K1 walmart canada")
This returned the lat and lon:
> goodPoint
lon lat
1 -79.30113 43.72304
When I check Google map search using the same search term and get the latitude and longitude I get 43.728933,-79.29565.
The difference between the returned latitude and longitude is about 1.1 km, so quite significant.
Can anyone suggest why there is this difference when in the documentation it states that it uses Google Maps for the location? Any suggestions for ways around this?

Have you looked at the actual output from Google geocoder? The geocoder is not the same thing as the maps search. The geocoder resolves addresses, but it afaict does not perform searches such as "find a walmart near M1L2K1 canada".
https://maps.googleapis.com/maps/api/geocode/json?address=M1L2K1%20walmart%20canada&sensor=false
yields the following parsed address:
"formatted_address" : "Scarborough, Ontario M1L 2K1, Kanada"
...
"location_type" : "APPROXIMATE",
...
"types" : [ "postal_code" ]
So in fact, Google even tells you that this is a "postal code resolution" geocoding.
Again, geocoding does not equal search. "Walmart" is not a postal address, but a reasonable search term.

Related

Is there a way to use latitude and longitude in fusion map to locate cities

I am trying to integrate cities in fusion map for Florida.In the API I have latitude and longitude details. But when I use lat and long in the markers,the location of city is pointed outside of the map. Where can I find the correct coordinate of the fusion map and how to use lat and long to correctly point out the exact location of cities?

Can I restrict geocode results to a specific state in R?

I'm using the ggmap geocode with the Google maps API. I have vector of city names in Ohio (city), and I am trying to return coordinates for each of these cities. My code,
coordinates <- geocode(city)
Is mostly returning the correct coordinates, but many of the city names in Ohio are the same as those in other states, so I'm receiving a few incorrect coordinates. Is there an argument I can add to "geocode" so that it only returns coordinates from Ohio?
Thanks!

Google Distance Matrix between two locations does not return results when origins and destinations are reversed

Inbound https://maps.googleapis.com/maps/api/distancematrix/json?origins=Mount+Monodnak&destinations=Boston+MA
Outbound: https://maps.googleapis.com/maps/api/distancematrix/json?origins=Boston+MA&destinations=Mount+Monadnock
You can see that data is returned on the outbound leg to the mountain but not on the return back. Travel mode needs to be driving.
I Geocoded Mount Monadnock to get the latitude and longitude and tried the nearestRoad api call but that also returned {}
Here is the URL with my API key removed (but is required) roads.googleapis.com/v1/nearestRoads?points=42.8610804,-72.1083267&key=
A solution I am considering is to find the lat long bounds of the zip code area and find which point is closest to the destination to calculate distance and duration but that is obviously not ideal.
Any ideas or insight would be much appreciated, thanks.

Google Geocode address near a point?

I'm attempting to use Google Geocode to convert a textual address to a longitude/latitude position.
Sometimes, the geocoding operation fails (e.g. the city name is listed as "Rouyn Centre Rouyn-Noranda" instead of "Rouyn-Noranda"). Now, it just so happens that I've got a longitude/latitude point that is near the desired address (say within 1km of the actual address).
Is there a way to look up the coordinates of a street name, number (excluding city, postal code and country) that is "near" a second point?
You could test any subsequent points to see if they're within 1km of the original point using google.maps.geometry.spherical.computeDistanceBetween().

Convert lat/lon to zipcode / neighborhood name

I have a large collection of pictures with GPS locations, encoded as lat/lon coordinates, mostly in Los Angeles. I would like to convert these to (1) zipcodes, and (2) neighborhood names. Are there any free web services or databases to do so?
The best I can come up with so far is scrape the neighborhood polygons from the LA times page and try to find out in which polygon every coordinate is. However this might be quite a lot of work, and not all of my coordinates are in LA. As for the zipcodes, this 2004 database is the best I can find, however zipcodes are encoded as a single coordinates instead of a polygon. So the best I can do is find the minimum distance from a given coordinate to the given zipcode-coordinates, which is not optimal.
I was under the impression that google-maps or open-street-maps should be able to do this (as they seem to 'know' exactly where every neighboorhood and zipcode is), however I cannot find any API's to do the lookups / queries.
You can now do this directly within R itself thanks to the rather awesome ggmap package.
Like others mention, you'll be reverse geocoding using the google maps API (and therefore limited to 2,500 queries daily), but it's as simple as:
library("ggmap")
# generate a single example address
lonlat_sample <- as.numeric(geocode("the hollyood bowl"))
lonlat_sample # note the order is longitude, latitiude
res <- revgeocode(lonlat_sample, output="more")
# can then access zip and neighborhood where populated
res$postal_code
res$neighborhood
Use Reverse Geocoding to convert your lat/lon to addresses. It has some limit on the number of queries per day though.
Here is a nice blog post with examples how to geocode and reverse geocode using google-maps.
Try this one:
http://www.usnaviguide.com/zip.htm
There is some limit as to how many queries per day you can do on the site, but they also sell the complete database, which changes every few months.
Sorry that I don't know of any free resources.
As others suggested, geocode them into street address should work fine for zip code. i am not too sure about neighborhood, because you may have to look if street number is odd/even to see if it is located which side of a road that determines neighborhood.
An alternative way is to prepare GIS polygon feature (ESRI shape file for example), test each point against this set of polygons see which one it intersects.
zip code is very straighforward, you can download shape file from the census.
http://www.census.gov/cgi-bin/geo/shapefiles2010/main
neighborhood is harder, i'd guess. In another part of US i had to create my shape file on my own by combining definitions from municipal government, real-estate website, newspaper etc so that it looks like what people thinks neighborhood in the city are without having any overlap or gap. It can take some time to compose such set of polygons. you may crab census "block group", or even census "block" from the above page and merge them
Once you prepared polygon features, there are couple of GIS tools on different environment (stand-alone executable, GUI program, c/python/sql etc API, probably R as well, to do intersection of polygons and points.

Resources