Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps - Get Polygon border of zones/neighborhood

Tags:

google-maps

I want to make a simple search on Google Maps API v3 and get as a result the map with a colored polygon like the picture below:

Search: pinheiros, sao paulo

enter image description here

like image 728
Diego Dias Avatar asked May 09 '12 18:05

Diego Dias


People also ask

How do I show borders on Google Maps?

Google Maps has added a feature where it will highlight in a pink color the borders of a city, postal code or other borders based on your search. To see it yourself, go to Google Maps and search for a city name or even a zip code. You will see a pinkish highlight around the border.

How do I extract a polygon from Google Maps?

In Google Earth, create the polygon that you wish to bring into RockWorks. Or, locate the polygon that currently exists in your Saved Places listing. Right-click on the item, and choose Copy from the pop-up menu. Or, right-click on the item, and choose Save Place As to save the locations in a KMZ file.

Can you add polygons to Google Maps?

Draw a path or polygonTo make a path or polygon into a 3D object, click Altitude. A "New Path" or "New Polygon" dialog will pop up. You may need to move it out of the way before moving on to the next step. To draw the line or shape you want, click a start point on the map and drag.


1 Answers

I got it by using WikiMapia data.

The steps are:

  • Open WikiMapia.
  • Press F12 or Ctrl+Shift+I.

enter image description here

  • Search the location that you want.
  • Click with the right button on neighborhood and choose Edit Poly. enter image description here
  • On the developer tools click in Network and filter by getPolygon. enter image description here
  • Copy server response that looks like: enter image description here

  • And go to Console tab to execute the function: Wikimapia.Parser.Itiles.prototype.decodePolygon("Copyed text here");

  • Press enter to execute after this the Console will return the object with the points (The path of Poly).

I use JSON.stringify to transform the object in JSON.

Example:

JSON.stringify(Wikimapia.Parser.Itiles.prototype.decodePolygon("-619402099;-109032887;02fe8953fffe5a000ae5fe379a000fedffd250000e40ffd6050007f7ffeff2001925ffd59b001f6fffdc310012dcffed10003b82ffd9b9005514ffc4520053d2ffc807000c92fff82dfffde7000e670005070012ef0009390021bf000572001358fffc3a001a57fffa210013c1ffff940036530008610022fc000284004732fff5f0001fb1fff2960013c1fff80800594efffde7002c72000f16004b5000204600416f0013b2002292"))

The code above will return this JSON (indentation in JsonFormatter):

{
   "points":[
      {
         "lat":-10.9032887,
         "lng":-61.9402099
      },
      {
         "lat":-10.9032466,
         "lng":-61.9306183
      },
      {
         "lat":-10.8915629,
         "lng":-61.9308972
      },
      {
         "lat":-10.8903934,
         "lng":-61.9313049
      },
      {
         "lat":-10.8893188,
         "lng":-61.9316697
      },
      {
         "lat":-10.8889079,
         "lng":-61.9318736
      },
      {
         "lat":-10.8878227,
         "lng":-61.9325173
      },
      {
         "lat":-10.8869061,
         "lng":-61.933322
      },
      {
         "lat":-10.8864214,
         "lng":-61.9338048
      },
      {
         "lat":-10.8854416,
         "lng":-61.9353282
      },
      {
         "lat":-10.8839139,
         "lng":-61.9375062
      },
      {
         "lat":-10.8824811,
         "lng":-61.939652
      },
      {
         "lat":-10.8822809,
         "lng":-61.9399738
      },
      {
         "lat":-10.8826496,
         "lng":-61.9399202
      },
      {
         "lat":-10.8831343,
         "lng":-61.9400489
      },
      {
         "lat":-10.8839982,
         "lng":-61.940285
      },
      {
         "lat":-10.8844934,
         "lng":-61.9404244
      },
      {
         "lat":-10.8851677,
         "lng":-61.9403279
      },
      {
         "lat":-10.8856734,
         "lng":-61.9401777
      },
      {
         "lat":-10.8870641,
         "lng":-61.940167
      },
      {
         "lat":-10.8879597,
         "lng":-61.9403815
      },
      {
         "lat":-10.8897823,
         "lng":-61.9404459
      },
      {
         "lat":-10.8905936,
         "lng":-61.9401884
      },
      {
         "lat":-10.8910993,
         "lng":-61.9398451
      },
      {
         "lat":-10.8933855,
         "lng":-61.9396412
      },
      {
         "lat":-10.8945233,
         "lng":-61.9395876
      },
      {
         "lat":-10.8964513,
         "lng":-61.9399738
      },
      {
         "lat":-10.8981264,
         "lng":-61.9408
      },
      {
         "lat":-10.8990114,
         "lng":-61.9413042
      }
   ],
   "bounds":{
      "left":-61.9413042,
      "bottom":-10.9032887,
      "right":-61.9306183,
      "top":-10.8822809,
      "centerLatLng":null
   }
}

Finally I use regex like this regexr.com/3c5m2 to turn JSON into WKT. I don't copy entire JSON only the objects in "points" array in Regex. On RegExr I copy the replaced text and paste in POLYGON((pastehere)).

Important! After apply the regex you need to repeat the first point in last point.

-61.956523060798645 -10.877613428213532,-61.95640504360199 -10.877718788854143,-61.956791281700134 -10.878393096072424,-61.95735991001129 -10.87805594265392,-61.95682346820831 -10.877339490373695,-61.956523060798645 -10.877613428213532

Then you get something like this:

POLYGON((-61.956523060798645 -10.877613428213532,-61.95640504360199 -10.877718788854143,-61.956791281700134 -10.878393096072424,-61.95735991001129 -10.87805594265392,-61.95682346820831 -10.877339490373695,-61.956523060798645 -10.877613428213532))

That can be inserted in database that supports WKT (like MySQL).

WikiMapia has an API then all of this process can be automatic, but this works fine for me.

like image 200
Jessé Catrinck Avatar answered Sep 19 '22 19:09

Jessé Catrinck