Getting an intersection with Google Places/Geocoding API
Asked Answered
C

7

13

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.

Cleocleobulus answered 20/5, 2012 at 20:7 Comment(6)
The reverse-geocoder always returns exactly one result: the nearest thing it found, so you can't do what you describe. What exactly are you trying to achieve?Liturgist
I'm trying to find the closest intersection to the point where I am. At the very least I'd like two street names when I am ON an intersection, but ideally I want the closest cross street as well. My hope was that providing a certain radius size would give me multiple 'route' results, and I could use the second-best 'route' as the cross-street.Cleocleobulus
On testing, I can confirm that a reverse geocode never returns more than one result: the nearest thing in GoogleMap's opinion. I also tried google.maps.places.PlaceSearchRequest, but even if you ask for "street_address" it returns nothing (unless there happens to be a place of interest that someone has marked nearby).Liturgist
Yep, that's the same conclusion I came to when I was testing it, which prompted me to post this question. I wonder if there's a way to get the intersection other than submitting multiple queries in close proximity, or IF there's no other way to do it other than through multiple queries, what the most efficient with the least number of queries is.Cleocleobulus
Interesting problem, but I don't see a solution, even with multiple queries. Suppose you make 4 queries for the nearest address N/S/E/W by, say, 200 metres. It'll work when you're within 200 metres of an intersection, but it'll fail when the nearest intersection is a kilometre away. Which brings me back to my original question: What use is the 'nearest intersection', when it could be miles away? IOW, what is the underlying use case? If you could explain what you're really trying to achieve, we might be able to help.Liturgist
In my app, users get to pick "waypoints", i.e. points along the route they intend to travel. I want to automatically pick a descriptive name for each one of those. It's very likely that there are multiple waypoints on the same street, so I'd like the intersection as a discriminator between multiple waypoints. It's likely that users will pick a point near an intersection, and if there is no intersection nearby, I won't print one. The whole thing is mostly to automatically assign user-friendly names to points, it doesn't really matter if the name is not entirely correct.Cleocleobulus
T
8

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.

Troopship answered 1/6, 2012 at 15:29 Comment(1)
Hey Rooke, that's actually an interesting approach. I could even try using a landmark as a reference (preferably a bus stop, like "Schmuck Street near Dork Station", but possibly even "Food Street near Applebee's" if nothing else comes up. That'll require fewer queries than an X pattern. I'd still prefer the intersection name, but let's be practical. And here's the bounty!Cleocleobulus
P
14

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.

Pander answered 6/6, 2013 at 17:37 Comment(0)
T
8

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.

Troopship answered 1/6, 2012 at 15:29 Comment(1)
Hey Rooke, that's actually an interesting approach. I could even try using a landmark as a reference (preferably a bus stop, like "Schmuck Street near Dork Station", but possibly even "Food Street near Applebee's" if nothing else comes up. That'll require fewer queries than an X pattern. I'd still prefer the intersection name, but let's be practical. And here's the bounty!Cleocleobulus
I
4

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"/>
Importunacy answered 30/11, 2012 at 1:42 Comment(2)
Nice database. Is there an API where I can access the database using simple queries? Obviously, I don't want to download planet.osm, and this is an automated background operation, I can't use the web interface. I checked the website, but didn't immediately find anything.Cleocleobulus
read the wiki, you can connect to the db. The united states planet.osm is only 25 gigs. I'd save what ever part of the map you need, instead of hitting their db or website every time since it's a free service.Importunacy
P
2

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/

Powder answered 21/5, 2012 at 1:28 Comment(1)
Thanks Chris! I'm a complete schmuck - how did I not notice the Geocoding API? However, I still have the same problem with the Geocoding API - I can only do a reverse geocoding of a specific point, which returns the closest street, but never an intersection. How can I find the closest cross street?Cleocleobulus
L
2

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.

Liturgist answered 31/5, 2012 at 8:36 Comment(1)
Sigh, I'm not sure who to give the bounty too - I'll probably try both yours and Rooke's approach. Rooke inspired me though to try something else, i.e. identify a street by its name and a major landmark. Would be nice to be able to split up a bounty.Cleocleobulus
B
1

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.

Betseybetsy answered 30/5, 2012 at 4:15 Comment(0)
A
1

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.

Aerate answered 26/8, 2020 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.