91

When searching Google Maps for a city, the city's administrative boundary is displayed.

enter image description here

Is there a way to get the polygon boundaries of City in a JSON format using google maps api?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Luis Tiago
  • 1,011
  • 1
  • 8
  • 4

5 Answers5

88

You can get polygon coordenates in json for using with googlemaps using openstreetmap. Go to http://nominatim.openstreetmap.org/ search a place like "Partido de Ituzaingó"

click on "details"

Look for OSM ID and copy it (control+c), example: 2018776

paste the ID in http://polygons.openstreetmap.fr/index.py and download the polygon

german_martin
  • 981
  • 6
  • 3
  • http://polygons.openstreetmap.fr/index.py not work anymore. you can use http://global.mapit.mysociety.org – Shai M. Dec 26 '17 at 12:15
  • 2
    global.mapit.mysociety.org didn't work for Baghdad (but the original post did): first I got it here: https://nominatim.openstreetmap.org/details.php?place_id=179724619, then found the relation id to be 5638803, when I run http://global.mapit.mysociety.org/code/osm_rel/5638803 I get {"code": 404, "error": "No areas were found that matched code osm_rel = 5638803."} – abbood Jun 04 '18 at 08:02
  • Too complicated. And didn't work for me. its easier to grab the geojson directly from the nominatim's source code. – ymakux Jan 27 '20 at 03:32
37

To get polygon boundaries just add format=json to your request!

So instead of https://nominatim.openstreetmap.org/search.php?q=Warsaw+Poland&polygon_geojson=1

use

https://nominatim.openstreetmap.org/search.php?q=Warsaw+Poland&polygon_geojson=1&format=json

By adding &format=geojson you can get a valid geojson response

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • Thanks, I would like to download the boundaries of all neighbourhood within a city? Is this also doable and what is the command for that? – NeStack May 08 '22 at 12:07
10

I've found a work around solution for this problem.

  1. Go to www.gadm.org/country
  2. Choose your country and select Google Earth .kmz file format
  3. Choose the level you need (level 3 is the deepest with all small towns/cities)
  4. Download the file (can be large)
  5. Unzip the .kmz file (You'll find a .kml which is XML)
  6. Open it with Sublime or notepad++ (the file will probably be too large for other text editor)
  7. Search by city name and copy data below (Search can take 1 to 4 seconds with large file)

You can use lat,long data and parse it into an array. This solution is working well, but the quality of the path is sometimes rough.

jbchurchill
  • 4,489
  • 21
  • 41
Charles St-Yves
  • 101
  • 1
  • 2
  • Excellent tip thanks. I've made a basic repo with PHP and javascript to automate much of this herE: https://github.com/andyg2/parse-gadm-kml/ – Andy Gee Sep 02 '21 at 12:41
8

No, there is no way to steal Google's data. May be you could look at OpenStreetMap which is designed for people to download.

Ian Turton
  • 81,417
  • 6
  • 84
  • 185
1

As an alternative to Google Maps API, OpenStreetMap may help you to search for a city by name in this page http://nominatim.openstreetmap.org/ . Then review search results, and you can get region coordinate in GeoJSON format e.g (http://nominatim.openstreetmap.org/details.php?place_id=145126778) to get mashhad geojson coordinates in Administrative tab

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
HamidReza
  • 111
  • 2