$(document).ready(function () {
window.onload = resizeElementHeight();
$(window).resize(resizeElementHeight);
$("#toggle").bind("click", function () {
if ($("#leftContainer").is(':visible')) {
$("#collapse").attr("src", "Images/expand.gif");
$("#map-content").css("width", "100%");
$("#toggle").css("left", "0px");
$("#leftContainer").hide();
}
else {
$("#leftContainer").show();
$("#collapse").attr("src", "Images/collapse.gif");
$("#map-content").css("width", "80%");
$("#toggle").css("left", "20%");
}
resizeElementHeight();
});
$("#warningDialog").dialog({
autoOpen: false,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
$("#outofBoundaryWarning").dialog({
autoOpen: false,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
var btnTracks = $("#divTrackMode").find("input");
for (var i = 0; i < btnTracks.length; i++) {
$(btnTracks[i]).bind("click", function () {
var btnTracks = $("#divTrackMode").find("input");
for (var i = 0; i < btnTracks.length; i++) {
$(btnTracks[i]).css("border", "0px solid #D3D3D3");
$(btnTracks[i]).css("background-color", "transparent");
}
$(this).css("border", "1px solid #D3D3D3");
$(this).css("background-color", "#eeeeee");
});
};
$("#divTrackMode input[type=image]").bind("click", function () {
var mode = $(this).attr("command");
switch (mode) {
case "ClearAll":
$("#btnPanMap").trigger("click");
break;
case "Undo":
$("#btnPanMap").trigger("click");
break;
case "Polygon":
$(this).trigger("click");
break;
case "PanMap":
$("#btnPanMap").trigger("click");
break;
default: break;
}
});
});
var OnMapCreated = function (map) {
map.events.register("mousemove", map, function (e) {
var mouseCoordinate = this.getLonLatFromPixel(new OpenLayers.Pixel(e.clientX, e.clientY));
if (mouseCoordinate) {
$("#spanMouseCoordinate").text("X:" + mouseCoordinate.lon.toFixed(6) + " Y: " + mouseCoordinate.lat.toFixed(6));
}
});
};
// Add the loading image.
var pageManager = Sys.WebForms.PageRequestManager.getInstance();
pageManager.add_beginRequest(function () {
$("#loading").show();
});
pageManager.add_endRequest(function () {
$("#loading").hide();
});
function resizeElementHeight() {
var documentheight = $(window).height();
var contentDivH = documentheight - $("#footer").height() - $("#header").height() - 1;
$("#content-container").height(contentDivH + "px");
$("#leftContainer").height(contentDivH + "px");
$("#map-content").height(contentDivH + "px");
$("#toggle").css("line-height", contentDivH + "px");
if (contentDivH > 500) {
$("#queryResultUpdatePanel").height(contentDivH - 365 + "px");
} else {
$("#queryResultUpdatePanel").visible = false;
}
$("#mapContainer").height(contentDivH + "px");
// refresh the map.
webMap.GetOpenLayersMap().updateSize();
}
function pageLoad() {
resizeElementHeight();
}
====ContextMenuItemType.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ThinkGeo.MapSuite.RoutingExamples
{
public enum ContextMenuItemType
{
NoSure = 0,
StartPoint = 1,
EndPoint = 2,
StopPoint = 3
}
}
====RouteDirection.cs====
using System;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.Routing;
namespace ThinkGeo.MapSuite.RoutingExamples
{
public class RouteDirection
{
private static string directionImageUriTemplate = "../Images/Directions/{0}";
private string directionImageUriString;
private GeographyUnit geographyUnit;
private DistanceUnit lengthUnit;
private Feature roadFeature;
private string roadNameColumnName;
private RouteSegment roadSegment;
private Feature roadSegmentFeature;
public RouteDirection(RouteSegment roadSegment, Feature roadFeature, string roadNameColumnName, GeographyUnit geographyUnit, DistanceUnit lengthUnit)
{
this.roadSegment = roadSegment;
this.roadFeature = roadFeature;
this.roadNameColumnName = roadNameColumnName;
this.geographyUnit = geographyUnit;
this.lengthUnit = lengthUnit;
}
public DrivingDirection Direction
{
get
{
if (roadSegment != null)
{
return roadSegment.DrivingDirection;
}
else
{
return DrivingDirection.Back;
}
}
}
public Uri DirectionImageUri
{
get
{
switch (Direction)
{
case DrivingDirection.Back:
directionImageUriString = "back.png";
break;
case DrivingDirection.Forward:
directionImageUriString = "Forward.png";
break;
case DrivingDirection.Invalid:
directionImageUriString = "Invalid.png";
break;
case DrivingDirection.LeftForward:
directionImageUriString = "LeftForward.png";
break;
case DrivingDirection.Right:
directionImageUriString = "Right.png";
break;
case DrivingDirection.RightBack:
directionImageUriString = "RightBack.png";
break;
case DrivingDirection.RightForward:
directionImageUriString = "RightForward.png";
break;
case DrivingDirection.LeftBack:
directionImageUriString = "LeftBack.png";
break;
case DrivingDirection.Left:
directionImageUriString = "Left.png";
break;
}
return new Uri(string.Format(directionImageUriTemplate, directionImageUriString), UriKind.RelativeOrAbsolute);
}
}
public GeographyUnit GeographyUnit
{
get { return geographyUnit; }
set { geographyUnit = value; }
}
public string Length
{
get
{
string length = string.Empty;
double value = Math.Round(((LineBaseShape)RoadFeature.GetShape()).GetLength(GeographyUnit, lengthUnit), 2);
if (lengthUnit == DistanceUnit.Mile)
{
length = string.Format("{0} mi", value);
}
else
{
length = string.Format("{0} km", value);
}
return length;
}
}
public DistanceUnit LengthUnit
{
get { return lengthUnit; }
set { lengthUnit = value; }
}
public Feature RoadFeature
{
get { return roadFeature; }
set { roadFeature = value; }
}
public string RoadName
{
get
{
if (!string.IsNullOrEmpty(RoadNameColumnName))
{
return roadFeature.ColumnValues[RoadNameColumnName];
}
else
{
return string.Empty;
}
}
}
public string RoadNameColumnName
{
get { return roadNameColumnName; }
set { roadNameColumnName = value; }
}
}
}
====RoutePathAndDirection.cs====
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.Routing;
namespace ThinkGeo.MapSuite.RoutingExamples
{
public class RoutePathAndDirection
{
private readonly List stopPoints;
private readonly List avoidableFeatureIds;
private PointShape endPoint;
private PointShape startPoint;
private RoutingEngine routingEngine;
private FeatureSource barrierFeatureSource;
public RoutePathAndDirection()
{ }
public RoutePathAndDirection(RoutingEngine routingEngine, PointShape startPoint, PointShape endPoint)
: this(routingEngine, startPoint, endPoint, null)
{
}
public RoutePathAndDirection(RoutingEngine routingEngine, PointShape startPoint, PointShape endPoint, FeatureSource barrierFeatureSource)
: this(routingEngine, startPoint, endPoint, barrierFeatureSource, new string[] { })
{
}
public RoutePathAndDirection(RoutingEngine routingEngine, PointShape startPoint, PointShape endPoint, FeatureSource barrierFeatureSource, IEnumerable avoidableFeatureIds)
: this(routingEngine, startPoint, endPoint, barrierFeatureSource, avoidableFeatureIds, new PointShape[] { })
{
}
public RoutePathAndDirection(RoutingEngine routingEngine, PointShape startPoint, PointShape endPoint, FeatureSource barrierFeatureSource, IEnumerable stopPoints)
: this(routingEngine, startPoint, endPoint, barrierFeatureSource, new string[] { }, stopPoints)
{
}
public RoutePathAndDirection(RoutingEngine routingEngine, PointShape startPoint, PointShape endPoint, FeatureSource barrierFeatureSource, IEnumerable avoidableFeatureIds, IEnumerable stopPoints)
{
this.endPoint = endPoint;
this.startPoint = startPoint;
this.routingEngine = routingEngine;
this.barrierFeatureSource = barrierFeatureSource;
this.stopPoints = new List(stopPoints);
this.avoidableFeatureIds = new List(avoidableFeatureIds);
routingEngine.RoutingAlgorithm.FindingRoute += RoutingAlgorithm_FindingRoute;
}
public PointShape StartPoint
{
get { return startPoint; }
set { startPoint = value; }
}
public PointShape EndPoint
{
get { return endPoint; }
set { endPoint = value; }
}
public List StopPoints
{
get { return stopPoints; }
}
public RoutingEngine RoutingEngine
{
get { return routingEngine; }
set { routingEngine = value; }
}
public FeatureSource BarrierFeatureSource
{
get { return barrierFeatureSource; }
set { barrierFeatureSource = value; }
}
public List AvoidableFeatureIds
{
get { return avoidableFeatureIds; }
}
public RoutingResult GetRoute()
{
if (stopPoints != null && !stopPoints.Any())
{
RoutingResult result = routingEngine.GetRoute(startPoint, endPoint);
return result;
}
else
{
Collection points = new Collection();
points.Add(startPoint);
foreach (PointShape stopPoint in stopPoints)
{
points.Add(stopPoint);
}
points.Add(endPoint);
// Filter the invalid point.
Collection availablePoint = FilterInvalidPoints(points);
RoutingResult result = null;
for (int i = 0; i < availablePoint.Count - 1; i++)
{
RoutingResult tempResult = routingEngine.GetRoute(availablePoint[i], availablePoint[i|+ 1]);
result = CombineRoutingResult(result, tempResult);
}
return result;
}
}
private Collection FilterInvalidPoints(IEnumerable points)
{
Collection result = new Collection();
barrierFeatureSource.Open();
Collection barrierFeatures = barrierFeatureSource.GetAllFeatures(ReturningColumnsType.NoColumns);
foreach (PointShape point in points)
{
bool inBarrier = barrierFeatures.Any(a => a.Contains(new Feature(point)));
if (!inBarrier) result.Add(point);
}
return result;
}
private RoutingResult CombineRoutingResult(RoutingResult baseResult, RoutingResult additionalResult)
{
if (baseResult == null)
{
baseResult = new RoutingResult();
baseResult.Distance = additionalResult.Distance;
baseResult.Weight = additionalResult.Weight;
baseResult.Route = additionalResult.Route;
additionalResult.Features.ForEach(a => baseResult.Features.Add(a));
additionalResult.OrderedStops.ForEach(a => baseResult.OrderedStops.Add(a));
additionalResult.RouteSegments.ForEach(a => baseResult.RouteSegments.Add(a));
}
else
{
baseResult.Distance += additionalResult.Distance;
baseResult.Weight += additionalResult.Weight;
Collection vertexes = new Collection();
baseResult.Route.Vertices.ForEach(vertexes.Add);
additionalResult.Route.Vertices.ForEach(vertexes.Add);
baseResult.Route = new LineShape(vertexes);
additionalResult.Features.ForEach(a => baseResult.Features.Add(a));
additionalResult.OrderedStops.ForEach(a => baseResult.OrderedStops.Add(a));
additionalResult.RouteSegments.ForEach(a => baseResult.RouteSegments.Add(a));
}
return baseResult;
}
private void RoutingAlgorithm_FindingRoute(object sender, FindingRouteRoutingAlgorithmEventArgs e)
{
Collection beContainedFeatureIds = new Collection();
barrierFeatureSource.Open();
Collection startPointAdjacentIds = e.RouteSegment.StartPointAdjacentIds;
Collection endPointAdjacentIds = e.RouteSegment.EndPointAdjacentIds;
foreach (string id in startPointAdjacentIds.Where(id => avoidableFeatureIds.Contains(id)))
{
beContainedFeatureIds.Add(id);
}
foreach (string id in endPointAdjacentIds.Where(id => avoidableFeatureIds.Contains(id)))
{
beContainedFeatureIds.Add(id);
}
// Remove the ones that be contained in the avoidable area
foreach (string id in beContainedFeatureIds)
{
if (e.RouteSegment.StartPointAdjacentIds.Contains(id))
{
e.RouteSegment.StartPointAdjacentIds.Remove(id);
}
if (e.RouteSegment.EndPointAdjacentIds.Contains(id))
{
e.RouteSegment.EndPointAdjacentIds.Remove(id);
}
}
}
}
}
====StopItem.cs====
using ThinkGeo.MapSuite.Core;
using System;
namespace ThinkGeo.MapSuite.RoutingExamples
{
public class StopItem
{
private PointShape locationPointShape;
private string locationPointShapeText;
private int number;
public StopItem(PointShape locationPointShape)
{
string pointText = string.Format("{0} , {1}", Math.Round(locationPointShape.X, 8), Math.Round(locationPointShape.Y, 8));
LocationPointShapeText = pointText;
this.locationPointShape = locationPointShape;
}
public int Number
{
get { return number + 1; }
set { number = value; }
}
public PointShape LocationPointShape
{
get
{
return locationPointShape;
}
}
public string LocationPointShapeText
{
get { return locationPointShapeText; }
set { locationPointShapeText = value; }
}
public StopItem Self
{
get { return this; }
}
}
}
====Default.cs====
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ThinkGeo.MapSuite.RoutingExamples.Default" %>
<%@ Register Assembly="WebEdition" Namespace="ThinkGeo.MapSuite.WebEdition" TagPrefix="cc1" %>
City Routing and Directions