Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an intersection with Google Places/Geocoding API

I'm trying to find the two closest streets to a point with the Google Places API (basically to indicate the closest intersection). However, any result will only ever return one "route", if at all.

I figure I could do 5 queries in a + or X pattern, but that's hacky, unreliable, and of course will hit the query limit a lot sooner.

I am tempted to say that this is a deliberate move because the API is not meant to be used for something like navigation systems (which I'm not trying to do), but I still hope there's a TOS-compliant way to get the intersection.

Side note - it seems like specifying "types=route" never returns any results, which further nolsters my suspicion that Places is only really meant to be used for actual POIs, not for navigation, although the terms of service don't mention any explicit restrictions in that regard.

EDIT: As Chris pointed out, there's the Geocoding API that is specifically designed for geocoding, but I still don't see a way to get the closest cross street other than through multiple queries.

like image 218
EboMike Avatar asked May 20 '12 20:05

EboMike


4 Answers

GeoNames provides a Geocoding API for nearest intersection from latitude/longitude coordinates.

http://www.geonames.org/maps/us-reverse-geocoder.html#findNearestIntersection

The JSON response looks like this:

{"intersection":
 {"adminName2":"San Mateo",
  "street2":"Curtis St",
  "postalcode":"94025",
  "street1":"Roble Ave",
  "adminCode1":"CA",
  "distance":"0.08",
  "countryCode":"US",
  "lng":"-122.180842",
  "placename":"Menlo Park",
  "lat":"37.450649",
  "adminName1":"California"
 },
 "credits":"1.0"
}

street1 and street2 are the cross streets.

like image 28
dmzza Avatar answered Oct 18 '22 03:10

dmzza


This is an old question, but I was searching for the same exact feature and Google brought me here.

Apparently is now possible to search for crossroads by simply putting an ampersand between the two street names, like

street name 1 & street name 2

this technique works for me both with the Geocoding API and the Google Maps website.

like image 91
user1527576 Avatar answered Sep 20 '22 15:09

user1527576


The consensus is that Google's reverse geocoder is not sophisticated enough to report intersections in one query. That is, there isn't a parameter that you can use to tell them you just want the results with "types" : [ "intersection" ]. Google is eating its own dogfood here; if you enter a lat/lon in a maps.google search box, you always get a street (in Google parlance, a 'route') back.

As you point out, there are plenty of heuristics you could apply to get the coveted ["intersection"] type.

e.g. Several 'major' intersections in 1st-world cities have transit stops that describe the intersection. If any results have a "types" : [ "bus_station", "transit_station" ] element, you can try the "long_name" element as a search term and hope you get an intersection. smirkingman also mentioned a viable solution. Both of these required 2+ queries, alas.

In conclusion, there is no latelngToIntersection(lat,lng) kind of function in the Google Geocoding API. Evidence of this exists in Google's own maps.google page. I would attack your problem differently. If you want to get the nearest major intersection given a GPS location, it may help to get the user to help. Geocoding is notoriously littered with icky humanity.

like image 7
Rooke Avatar answered Oct 18 '22 03:10

Rooke


You can use data from OpenStreetMap. Their .osm file provides the lat/long of intersecting streets.

  1. How to get the data.

    1. Go to OpenStreetMap and find your place where you're interested in, zoom into like the 4th tick mark.

    2. Click Export on the top nav bar, on the drop down, press "OpenStreetMap XML Data" and click 'Export'.

    3. For a lots of data like a state or country, Planet.osm and find a your desired "extract".

  2. To get the intersections, you want to look for data that matches this format, probably search for highway.

Example:

<way id="6419265" user="eric22" uid="160949" visible="true" version="2" changeset="10632563" timestamp="2012-02-09T10:49:21Z">
  <nd ref="53167978"/>
  <nd ref="53163978"/>
  <nd ref="53163979"/>
  <nd ref="53173508"/>
  <nd ref="53164158"/>
  <nd ref="53173510"/>
  <tag <b>k="highway"</b> v="residential"/>
  <tag k="name" v="Alberta St"/>
  <tag k="tiger:cfcc" v="A41"/>
  <tag k="tiger:county" v="St. Louis-City, MO"/>
  ...
</way>

The way.id is the street id, an the nd refs are streets ids can you should find in the same file. They look something like this

‹node id="53167978" lat="38.5852785" lon="-90.2450074" user="eric22" uid="160949" visible="true" version="3" changeset="10632563" timestamp="2012-02-09T10:48:49Z"/>
like image 4
NJordan Avatar answered Oct 18 '22 03:10

NJordan