Kelderic
Kelderic

Reputation: 6717

Import Polygon Into Google My Maps

Background

I have a Google Map, creating with the JavaScript API.. I have a set of polygons which are overlays on the map.

These polygons started out as KML files, but have been converted to encoded polygons. Encoding Algorithm information: https://developers.google.com/maps/documentation/utilities/polylinealgorithm

Example:

oosrFrdygOh_sJpchBoog@y{lPyylHtcA_t\praM

This is a 4 point polygon in Ohio.

Question

My question is, is there a way to import these into Google My Maps? I'd like to import these so that I can edit them.

Upvotes: 1

Views: 8094

Answers (2)

Kelderic
Kelderic

Reputation: 6717

The answer is no, an encoded polygon cannot be imported. For a shape to be imported to My Maps, it must first be converted to a KML file.

BlueCollar laid out the first step, which is to use Google's encoding API to decode the encoded shapes into Lat/Lng pairs.

var encodedPath = 'oosrFrdygOh_sJpchBoog@y{lPyylHtcA_t\praM';
var path = google.maps.geometry.encoding.decodePath(encodedPath);

For the example encoded value, which would return:

(39.91816, -85.33082),(38.013470000000005, -85.86915),(38.22091, -83.01270000000001),(39.76632, -83.02369),(39.91816, -85.33082)

Those pairs then need to be inserted into a KML file:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>Test Shape</name>
    <Polygon>
      <extrude>1</extrude>
      <altitudeMode>relativeToGround</altitudeMode>
      <outerBoundaryIs>
        <LinearRing>
          <coordinates>
            -85.33082,39.91816
            -85.86915,38.01347
            -83.01270000000001,38.22091
            -83.02369,39.76632
            -85.33082,39.91816
          </coordinates>
        </LinearRing>
      </outerBoundaryIs>
    </Polygon>
  </Placemark>
</kml>

The KML file can then be imported.

Upvotes: 4

BlueCollar
BlueCollar

Reputation: 151

Try calling decodePath() on the encoded string, i.e.:

let encodedPath = 'oosrFrdygOh_sJpchBoog@y{lPyylHtcA_t\praM'; let path = google.maps.geometry.encoding.decodePath(encodedPath);

Upvotes: 0

Related Questions