5

Using Google Maps' API v3, is it possible to draw a polygon on a Google maps and get a list of all the street names that fall within that polygon?

enter image description here

2
  • 2
    Have you tried anything? I can't see an explicit method for that, but I can think of some terrible hacks. Compute a fine grid (2m x 2m) of points over the polygon, reverse-geocode, get the street name from the response. Or use OpenStreetMap instead, and the Overpass API which (I think) can do this.
    – Spacedman
    Commented Jul 28, 2018 at 16:25
  • you can send the lat,lng of your polygon as directions like maps.googleapis.com/maps/api/directions/…
    – Mapperz
    Commented Jul 28, 2018 at 21:06

2 Answers 2

5

So I used the solution (OpenStreetMap) from @Spacedman:

  1. Download the .pbf for your country https://download.geofabrik.de

  2. Download the polygon file for the desired municipality from https://github.com/JamesChevalier/cities or create your polygon file (.poly) here : http://share.mapbbcode.org/

  3. Download lasted version from Osmosis https://wiki.openstreetmap.org/wiki/Osmosis#Latest_stable_version

  4. Do this:

    osmosis --read-pbf-fast you_file_country-latest.osm.pbf file="your_file_country-latest.osm.pbf" --bounding-polygon file="your_municipality_polygon.poly" --write-xml file="your_file_out.osm"

enter image description here 5. In the .osm file, you obtain all the street names, to extract the names use the option (regex) of notepad++

enter image description here

It's complex but it works, but I still have to find a way to do it directly from Google API

2
  • 2
    I think you should take a look at Overpass API, the following query will return all highway=* with a name in your bounding box: overpass-turbo.eu/s/AD5 ... Polygon selection would also be possible.
    – mmd
    Commented Jul 28, 2018 at 21:22
  • Please check my answer if it helps you. Commented Oct 12, 2021 at 19:48
1

Sandra's solution put me into the right direction and helped me a lot. However, I am convinced the way how she "parses" the output file is not correct and the regular expression doesn't cover all streets - at least in Spain as a street can start with Calle and this kind of streets don't have the addr:street key.

This is a Linux solution which bases on Sandra's answer:

bin/osmosis -q ... --bp file="My.poly" \
            --tf accept-nodes way=* --wk keyList=highway --wx file=- \
    | sed -e 's/[ ]*<nd [^>]*>//g' \
    | sed -r '/^\s*$/d' \
    | perl -lne 'print $1 if /k="name" v="(.+)"/' \
    | sort \
    | uniq

What happens:

  1. Get only the way nodes which contain the highway key.
  2. Remove the <nd..> elements. Here I refer to this question regarding removing it.
  3. Remove empty lines.
  4. Run perl in order to get the street name from the element.
  5. Sort the result.
  6. Remove duplicates which can be, when for example the street is a one-way and has multiple entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.