Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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"/>

You are correct, the Places API is not meant to be use for Geocoding. Political results such as streets and towns are limited to only 2 results as they are only there to provide area identity for the returned Places.

If you would like to search for streets etc. try using the Geocoding API: https://developers.google.com/maps/documentation/geocoding/

As discussed, there is no GoogleMap facility to reverse geocode and get a set of close points.

The only alternative I see is

  • Calculate 200 metres N/S/E/W of the point with Haversine. You'll need to choose '200' empirically
  • Reverse-geocode those 4 points
  • From the returned addresses, take the 2 closest streets that are different (may not succeed)

If you do this client-side, your users will rarely run into the daily 2'500 limit.

Google Maps API doesn't support this. It is an address geocoder only. The best you can do with Google Maps API is user their directions service to find where the first turn is, but that's a total hack. You might want to try looking into other maps services that can offer this ability.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top