Maptitude GISDK Help |
This example opens a connection to the GISDK environment, calculates the fastest route between two addresses, and closes the connection to the GISDK environment:
using System;
using System.Collections.Generic;
using System.Text;
using CaliperForm;
///
/// Calculate the fastest route between 2 addresses
/// </summary>
static void Calculate_Route()
{
CaliperForm.Connection Conn = new CaliperForm.Connection
{ MappingServer = mapping_product };
Boolean opened = false;
try
{
opened = Conn.Open(log_file);
if (opened)
{
// You must declare dk as "dynamic" or the compiler will throw an error
dynamic dk = Conn.Gisdk;
OptionsArray region_prefs = new OptionsArray(dk.Macro("Get Geocoding Preferences"));
if (!region_prefs.ContainsKey("region_file")) {
Console.Out.WriteLine("Region File missing from 'Get Geocoding Preferences'");
Conn.Close();
return;
}
string region_file = region_prefs["region_file"] as string;
string data_directory =
System.IO.Path.GetDirectoryName(region_file);
string map_wizard_map_file = data_directory + "\\Map Wizard Maps\\MapWizardUSMap.map";
// Open the map wizard map witht the layers for this country package
var map_options = new OptionsArray();
map_options["Auto Project"] = "true";
// path used to look for layers used in the map, before other locations
// usually searched for
map_options["Force Directory"] = data_directory;
string map_name = dk.OpenMap(map_wizard_map_file, map_options);
if (map_name == null) {
Console.Out.WriteLine("Cannot open map wizard map file " + map_wizard_map_file +
". Perhaps some layers are missing?");
Conn.Close();
return;
}
// geocode an address
dynamic finder = dk.CreateObject("Data.Finder");
finder.SetRegion();
var args = new OptionsArray();
args["silent"] = true;
args["address"] = "200 Beacon Street";
args["city"] = "Boston MA";
var found_origin = new OptionsArray(finder.Find("ADDRESS", args));
args["address"] = "100 Seaport Blvd";
var found_destination = new OptionsArray(finder.Find("ADDRESS", args));
if (found_origin["Coord"] == null || found_destination["Coord"] == null) {
Console.Out.WriteLine("Unable to locate origin or destination address.");
Conn.Close();
return;
}
//Calculate path using the Routing API
var router = dk.CreateObject("Routing.Router");
router.Minimize = "Time";
var options = new OptionsArray();
string xml_directions_file = dk.Macro("G30 Tutorial Folder") + "Example Directions.xml";
options["Directions"] = xml_directions_file;
options["DirectionsField"] = "NAME";
options["StopDescriptions"] = new string[] { "200 Beacon St" , "100 Seaport Blvd" };
dynamic path = router.CalculatePath(found_origin["Coord"],
found_destination["Coord"],options);
if (path == null) {
Console.Out.WriteLine("Router error: {0}", router.ErrorMessage);
Conn.Close();
return;
}
Console.Out.WriteLine("Shortest Travel Time Path between two addresses: {0:F2} minutes.",
path.Value);
//add annotation
var annotationOpts = new OptionsArray();
annotationOpts["Points"] = path.Coords;
annotationOpts["Line Color"] = dk.ColorRGB(65535, 32768, 0);
object[] lineStyles = dk.Macro("G30 setup line styles");
annotationOpts["Line Style"] = lineStyles[1];
annotationOpts["Line Width"] = 8;
dk.AddAnnotation("Map|" + map_name, "Polyline", annotationOpts);
//calculate scope from path points
dynamic scope = dk.GetArrayScope(path.Coords);
//back up a little
scope.Width = scope.Width * 1.05;
scope.Height = scope.Height * 1.05;
//zoom map to scope of path
dk.SetMapScope(map_name, scope);
dk.RedrawMap(map_name);
// Dump the directions to the standard output
if (System.IO.File.Exists(xml_directions_file)) {
Console.Out.WriteLine("XML Turn by Turn Directions:");
Console.Out.WriteLine(System.IO.File.ReadAllText(xml_directions_file));
}
Conn.Close();
}
}
catch (System.Exception error)
{
Console.Out.WriteLine(error.Message);
}
}
For more information, see:
Getting Started with CaliperForm
Using the CaliperForm.Connection Class
Using the GISDK Extension Engine In-Process API
See also these specific C# examples for:
©2025 Caliper Corporation | www.caliper.com |