Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map api drawn polyline with encoded points

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 docs to solve my problem. There are few topics how to decode it, but I don't need to do that thing. I just need to draw polyline using encoded points. Could somebody give me example?

like image 345
Roman Avatar asked Oct 29 '12 12:10

Roman


1 Answers

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&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
like image 138
geocodezip Avatar answered Sep 24 '22 01:09

geocodezip