Google map api drawn polyline with encoded points

后端 未结 1 1110
温柔的废话
温柔的废话 2021-01-02 12:45

I am new in javascript and Google map api, so i have encoded points like this: \"yzocFzynhVq}@n}@o}@nzD\" and trying to draw polyline with it, I haven\'t found topics or doc

相关标签:
1条回答
  • 2021-01-02 13:26

    See the geometry library documentation for decodePath

    That will convert your encoded string into an array of google.maps.LatLng objects that can be used to create a Polyline

    Working example

    working code snippet:

    function initialize() {
      var myLatLng = new google.maps.LatLng(24.886436490787712, -70.2685546875);
      var mapOptions = {
        zoom: 13,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };
    
      var bermudaTriangle;
    
      var map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);
    
    
      // Construct the polygon
      bermudaTriangle = new google.maps.Polygon({
        paths: google.maps.geometry.encoding.decodePath("yzocFzynhVq}@n}@o}@nzD"),
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35
      });
    
      bermudaTriangle.setMap(map);
      map.setCenter(bermudaTriangle.getPath().getAt(Math.round(bermudaTriangle.getPath().getLength() / 2)));
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    #map_canvas {
      height: 100%;
    }
    @media print {
      html,
      body {
        height: auto;
      }
      #map_canvas {
        height: 650px;
      }
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
    <div id="map_canvas"></div>

    0 讨论(0)
提交回复
热议问题