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?
-
2Have 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.– SpacedmanCommented 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
So I used the solution (OpenStreetMap) from @Spacedman:
Download the .pbf for your country https://download.geofabrik.de
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/
Download lasted version from Osmosis https://wiki.openstreetmap.org/wiki/Osmosis#Latest_stable_version
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"
5. In the .osm file, you obtain all the street names, to extract the names use the option (regex) of notepad++
It's complex but it works, but I still have to find a way to do it directly from Google API
-
2I 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.– mmdCommented Jul 28, 2018 at 21:22
-
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:
- Get only the
way
nodes which contain thehighway
key. - Remove the
<nd..>
elements. Here I refer to this question regarding removing it. - Remove empty lines.
- Run
perl
in order to get the street name from the element. - Sort the result.
- Remove duplicates which can be, when for example the street is a one-way and has multiple entries.