商信互联
本示例演示如何在根据“使用栅格地图”教程创建的Mobile或ASP.NET应用程序中使用Google Maps API绘制连接地图标记的线。
面向ASP.NET的控制器
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);
}
}
Imports Microsoft.VisualBasic
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Maps.Web
Imports System.Web.Script.Serialization
Imports DevExpress.ExpressApp.Maps.Web.Helpers
' ...
Public Class WebPolylineController
Inherits ViewController(Of ListView)
Public Sub New()
TargetObjectType = GetType(IMapsMarker)
End Sub
Protected Overrides Sub OnViewControlsCreated()
MyBase.OnViewControlsCreated()
Dim webMapsListEditor As WebMapsListEditor = TryCast(CType(View, ListView).Editor, WebMapsListEditor)
If webMapsListEditor IsNot Nothing Then
webMapsListEditor.MapViewer.ClientSideEvents.Customize = GetCustomizeScript()
End If
End Sub
Private Function GetPolylinePoints() As IEnumerable(Of MapPoint)
Dim polylinePoints As New List(Of MapPoint)()
For Each marker As IMapsMarker In View.CollectionSource.List
polylinePoints.Add(New MapPoint(marker.Latitude, marker.Longitude))
Next marker
Return polylinePoints
End Function
Private Function GetCustomizeScript() As String
Dim jsSerializer = New JavaScriptSerializer()
Dim polylines As String = jsSerializer.Serialize(GetPolylinePoints())
Return String.Format("function(sender, map) {{" & ControlChars.CrLf & _
" map.on('ready', function(e) {{" & ControlChars.CrLf & _
" var googleMap = e.originalMap;" & ControlChars.CrLf & _
" var flightPlanCoordinates = {0};" & ControlChars.CrLf & _
" var flightPath = new google.maps.Polyline({{" & ControlChars.CrLf & _
" path: flightPlanCoordinates," & ControlChars.CrLf & _
" strokeColor: '#FF0000'," & ControlChars.CrLf & _
" strokeOpacity: 1.0," & ControlChars.CrLf & _
" strokeWeight: 2" & ControlChars.CrLf & _
" }});" & ControlChars.CrLf & _
" flightPath.setMap(googleMap);" & ControlChars.CrLf & _
" }});" & ControlChars.CrLf & _
"}}", polylines)
End Function
End Class
运行该应用程序并创建几个标记。结果显示在下图中。
面向移动的控制器
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Maps.Mobile;
using DevExpress.ExpressApp.Maps.Mobile.Editors;
using DevExpress.ExpressApp.Maps.Mobile.Helpers;
using Newtonsoft.Json;
using System.Collections.Generic;
//...
public class MobilePolylineController : ViewController<ListView> {
public MobilePolylineController() {
TargetViewId = "MapsMarker_ListView";
}
protected override void OnViewControlsCreated() {
base.OnViewControlsCreated();
MobileMapsListEditor mobileMapsListEditor = View.Editor as MobileMapsListEditor;
if (mobileMapsListEditor != null) {
Map map = mobileMapsListEditor.Control as Map;
map.OnCustomize = GetCustomizeScript();
}
}
private IEnumerable<MapPoint> GetPolylinePoints() {
List<MapPoint> polylinePoints = new List<MapPoint>();
polylinePoints.Add(new MapPoint(40.7, -74));
polylinePoints.Add(new MapPoint(50.3, 0.7));
return polylinePoints;
}
private string GetCustomizeScript() {
var polylines = JsonConvert.SerializeObject(GetPolylinePoints());
return string.Format(
@"(function(mapInstance) {{
mapInstance.on('ready', function(e) {{
var googleMap = e.originalMap;
var flightPlanCoordinates = {0};
var flightPath = new google.maps.Polyline({{
path: flightPlanCoordinates,
strokeColor: '#0000FF',
strokeOpacity: 1.0,
strokeWeight: 2
}});
window.flightPath = flightPath;
flightPath.setMap(googleMap);
}});
}})", polylines);
}
}
Imports Microsoft.VisualBasic
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Maps.Mobile
Imports DevExpress.ExpressApp.Maps.Mobile.Editors
Imports DevExpress.ExpressApp.Maps.Mobile.Helpers
Imports Newtonsoft.Json
Imports System.Collections.Generic
'...
Public Class MobilePolylineController
Inherits ViewController(Of ListView)
Public Sub New()
TargetViewId = "MapsMarker_ListView"
End Sub
Protected Overrides Sub OnViewControlsCreated()
MyBase.OnViewControlsCreated()
Dim mobileMapsListEditor As MobileMapsListEditor = TryCast(View.Editor, MobileMapsListEditor)
If mobileMapsListEditor IsNot Nothing Then
Dim map As Map = TryCast(mobileMapsListEditor.Control, Map)
map.OnCustomize = GetCustomizeScript()
End If
End Sub
Private Function GetPolylinePoints() As IEnumerable(Of MapPoint)
Dim polylinePoints As New List(Of MapPoint)()
polylinePoints.Add(New MapPoint(40.7, -74))
polylinePoints.Add(New MapPoint(50.3, 0.7))
Return polylinePoints
End Function
Private Function GetCustomizeScript() As String
Dim polylines = JsonConvert.SerializeObject(GetPolylinePoints())
Return String.Format("(function(mapInstance) {{" & ControlChars.CrLf & _
" mapInstance.on('ready', function(e) {{" & ControlChars.CrLf & _
" var googleMap = e.originalMap;" & ControlChars.CrLf & _
" var flightPlanCoordinates = {0};" & ControlChars.CrLf & _
" var flightPath = new google.maps.Polyline({{" & ControlChars.CrLf & _
" path: flightPlanCoordinates," & ControlChars.CrLf & _
" strokeColor: '#0000FF'," & ControlChars.CrLf & _
" strokeOpacity: 1.0," & ControlChars.CrLf & _
" strokeWeight: 2" & ControlChars.CrLf & _
" }});" & ControlChars.CrLf & _
" window.flightPath = flightPath;" & ControlChars.CrLf & _
" flightPath.setMap(googleMap);" & ControlChars.CrLf & _
" }});" & ControlChars.CrLf & _
" }})", polylines)
End Function
End Class
运行应用程序。结果显示在下图中。