Search code examples
javascriptgoogle-mapsgoogle-maps-api-3turfjs

Google Maps & Turf.js Polyline Integration


I am working on an example google maps and turf js integration. I added the line with GeoJson data.Working snippet is attached. But I want to create a polyline out of turf line positions.

for (var j = 0; j < line.geometry.coordinates.length; j++) {
    var wp = new google.maps.LatLng(line.geometry.coordinates[j][1], line.geometry.coordinates[j][0])
    path.push(wp);
}

I tried to create a path then create my polyline based on this path but couldn't manage to create a path out of turf line arrays.

Any ideas how can I do this?

Thanks in advance!

var path = [];
		var map = new google.maps.Map(document.getElementById('map'), {
		  zoom: 2,
		  maxZoom: 8,
		  minZoom: 1,
		  center: {
		    lat: 0,
		    lng: -180
		  },
		  streetViewControl: false,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		});

var line = {
    "type": "Feature",
    "properties": { "line": "on" },
    "geometry": {
      "type": "LineString",
      "coordinates": [
        [-122.214, 37.772],
    [-157.821, 21.291],
    [178.431, -18.142],
    [153.027, -27.467]
      ]
    }
  };
     map.data.addGeoJson(line);
     
     
     
    var point = turf.point([185.431, -4.542]);

		var marker = new google.maps.Marker({
		  position: new google.maps.LatLng(point.geometry.coordinates[1], point.geometry.coordinates[0]),
		  map: map,
		});
html,
body,
#map {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://npmcdn.com/@turf/[email protected]/turf.min.js"></script>
<div id="map"></div>


Solution

  • I solved it, I just didn't see it...

    $.each(line.geometry.coordinates, function (key) {
        var wp = { lat: line.geometry.coordinates[key][1], lng: line.geometry.coordinates[key][0] }
        path.push(wp);
    });