-->

Google Maps - Get Polygon border of zones/neighbor

2020-08-17 07:37发布

问题:

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

回答1:

I got it by using WikiMapia data.

The steps are:

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

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

  • 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.



回答2:

The short answer is: no, the Google Maps API does not provide that functionality.

One way to implement this is to find the spatial data (polygons) that you're inerested in, load it to a database (which ideally supports spatial queries), and then to query the database via AJAX to add the feature geometry as a KMLLayer to the map, along with the corresponding push pin.



回答3:

You may check out this working on a website at funda.nl. When you select an individual property, it also shows a Google Map in which a polygon is drawn around the neighborhood of the property selected.



标签: google-maps