Skip to main content
A newer version of this page is available. .

How to: Draw Lines on a Map using Google Maps API

  • 3 minutes to read

This example demonstrates how to draw lines that connect the map markers using an ASP.NET application created according to the Use Raster Maps tutorial.

using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Maps.Web;
using System.Web.Script.Serialization;
using DevExpress.ExpressApp.Maps.Web.Helpers;
// ...
public class WebPolylineController : ViewController<ListView> {
    public WebPolylineController() {
        TargetObjectType = typeof(IMapsMarker);
    }
    protected override void OnViewControlsCreated() {
        base.OnViewControlsCreated();

        WebMapsListEditor webMapsListEditor = ((ListView)View).Editor as WebMapsListEditor;
        if (webMapsListEditor != null)
            webMapsListEditor.MapViewer.ClientSideEvents.Customize = GetCustomizeScript();
    }
    private IEnumerable<MapPoint> GetPolylinePoints() {
        List<MapPoint> polylinePoints = new List<MapPoint>();
        foreach (IMapsMarker marker in View.CollectionSource.List) {
            polylinePoints.Add(new MapPoint(marker.Latitude, marker.Longitude));
        }
        return polylinePoints;
    }
    private string GetCustomizeScript() {
        var jsSerializer = new JavaScriptSerializer();
        string polylines = jsSerializer.Serialize(GetPolylinePoints());
        return string.Format(
@"function(sender, map) {{
    map.on('ready', function(e) {{
        var googleMap = e.originalMap;
        var flightPlanCoordinates = {0};
        var flightPath = new google.maps.Polyline({{
        path: flightPlanCoordinates,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
        }});

        flightPath.setMap(googleMap);
    }});
}}", polylines);
    }
}

Run the application and create several markers. The result is demonstrated in the image below.

Maps_Polyline

See Also