Finally I found the solution! The problem was that encodePath
expects a google.maps.LatLng
object and not just the GeoPoints
.
Here is a function which turns an array like the one descripted above into an encoded string:
function encodeLatLngPolygon(array) {
var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3
}
poly = new google.maps.Polyline(polyOptions);
var path = poly.getPath();
for(var i=0;i<array.length;i++) {
var xyz = new google.maps.LatLng(parseFloat(array[i][0]).toFixed(2), parseFloat(array[i][1]).toFixed(2));
path.push(xyz);
}
var code = google.maps.geometry.encoding.encodePath(path)
return code;
}
The toFixed
reduces the numbers after the decimal point for saving bytes. You can delete or adjust this parameter.