Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Generally draw the Line to marker location but i want to how to draw route between

multiple markers in MVC using kendo

Line to marker location source code:

Map.cshtml:

HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Kendo UI Snippet</title>

    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.common.min.css"/>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.rtl.min.css"/>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.silver.min.css"/>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.mobile.all.min.css"/>

    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2018.2.620/js/kendo.all.min.js"></script>
</head>
<body>
  
    <div id="map"></div>
    <script>
      $("#map").kendoMap({
        center: [18.89, -72.19],
        zoom: 5,
        layers: [{
          type: "shape",
          dataSource: {
            type: "geojson",
            transport: {
              read: "http://output.jsbin.com/zuguhajiye.js"
            }
          }
        }, {
          type: "marker",
          dataSource: {
            data: [{
              location: [20.69, -70.96],
              title: "Foo",
              pointTo: [18.89, -72.19]
            }],
            locationField: "location",
            titleField: "title"
          }
        }],
        reset: function(e) {
          var map = e.sender;
          var markers = map.layers[1].items;

          for (var i = 0; i < markers.length; i++) {
            linkMarker(map, markers[i]);
          }
        }
      });

      function linkMarker(map, marker) {
        var data = marker.dataItem;
        if (data.pointTo) {
          // Convert lat/long locations to coordinates on the screen
          // See: http://docs.telerik.com/kendo-ui/api/dataviz/map#methods-eventToView
          var from = map.locationToView(marker.location());
          var to = map.locationToView(data.pointTo);

          // Draw a path on the shape layer
          // See: http://docs.telerik.com/kendo-ui/api/dataviz/drawing/path
          //      http://docs.telerik.com/kendo-ui/getting-started/dataviz/drawing/basic-shapes
          var shapeLayer = map.layers[0];
          var line = new kendo.dataviz.drawing.Path({
            stroke: {
              color: "#aaa",
              width: 4,
              lineCap: "round"
            }
          });
          line.moveTo(from).lineTo(to);

          shapeLayer.surface.draw(line);
        }
      }
    </script>
</body>
</html>


Finally i need draw route between multiple markers in MVC using kendo.

please help me.

thank u.

What I have tried:

Generally draw the Line to marker location but i want to how to draw route between

multiple markers in MVC using kendo
Posted
Updated 2-Jul-18 23:51pm

1 solution

First of all you need more than one point to work with - otherwise there is no route...
JavaScript
data: [{
  location: [20.69, -70.96],
  title: "Foo"
},
{
  location: [22.69, -77.96],
  title: "Next Foo"
},
{
  location: [12.69, -67.96],
  title: "Next Next Foo"
}],


If you want to draw a line between two point you have to pass two points (markers) to the drawing method... So run your loop up to one-before-the-last elements...

JavaScript
for (var i = 0; i < markers.length - 1; i++) {
	linkMarker(map, markers[i], markers[i + 1]);
}


And the drawing function:
JavaScript
function linkMarker(map, marker, nextMarker) {
	var data = marker.dataItem;
	var nextData = nextMarker.dataItem;

	var dataFrom = map.locationToView(marker.location());
	var nextDataFrom = map.locationToView(nextMarker.location());

	var shapeLayer = map.layers[0];
	var line = new kendo.dataviz.drawing.Path({
		stroke: {
		  color: "#aaa",
		  width: 4,
		  lineCap: "round"
		}
	});
	  
	line.moveTo(dataFrom).lineTo(nextDataFrom);

	shapeLayer.surface.draw(line);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900