GeoRoute.xaml.cs :  » GIS » DeepEarth » ExampleControl » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GIS » DeepEarth 
DeepEarth » ExampleControl » Controls » GeoRoute.xaml.cs
/// Deep Earth is a community project available under the Microsoft Public License (Ms-PL)
/// Code is provided as is and with no warrenty  Use at your own risk
/// View the project and the latest code at http://codeplex.com/deepearth/

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using DeepEarth.Client.MapControl;
using DeepEarth.Client.MapControl.Geometry;
using DeepEarth.Client.MapControl.Layers;
using DeepEarth.Client.Services.Bing;
using DeepEarth.Client.Services.Bing.VEGeocodeService;
using DeepEarth.Client.Services.Bing.VERouteService;

namespace ExampleControl.Controls{
    public partial class GeoRoute : ILayer
    {
        private Map _Map;
        private GeometryLayer routeLayer;

        public string ApplicationID { get; set; }

        public GeoRoute()
        {
            InitializeComponent();
            Loaded += GeoRoute_Loaded;
        }

        void GeoRoute_Loaded(object sender, RoutedEventArgs e)
        {
            // Test for DesignTime for display in Blend
            if (HtmlPage.IsEnabled)
            {
                _Map = MapInstance;

                GeocodeService = new Geocode(ApplicationID);
                RouteService = new Route(ApplicationID);

                Find.Click += Find_Click;
                Clear.Click += Clear_Click;
                AddRoute.Click += AddRoute_Click;
                ToggleRoute.Click += ToggleRoute_Click;
            }
        }

        public Geocode GeocodeService { get; set; }
        public Route RouteService { get; set; }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            ClearSearchFields();
        }

        private void ClearSearchFields()
        {
            StreetAddressText.Text = string.Empty;
            TownCityText.Text = string.Empty;
            CountryRegionText.Text = string.Empty;
        }

        private void CloseGeoFindListBoxStoryboard_Completed(object sender, EventArgs e)
        {
            GeocodeFindResults.ItemsSource = null;
            GeocodeFindResults.SelectedIndex = -1;
        }

        private void CloseGeoRouteListBoxStoryboard_Completed(object sender, EventArgs e)
        {
            GeocodeRoutePoints.ItemsSource = null;
            GeocodeRoutePoints.SelectedIndex = -1;
        }

        private void Find_Click(object sender, RoutedEventArgs e)
        {
            GeocodeFind();
        }

        private void GeocodeFind()
        {
            VisualStateManager.GoToState(this, "CloseGeoFindListBox", true);

            string query = string.Empty;

            if (StreetAddressText.Text.Length > 0) query += StreetAddressText.Text + ", ";
            if (TownCityText.Text.Length > 0) query += TownCityText.Text + ", ";
            if (CountryRegionText.Text.Length > 0) query += CountryRegionText.Text;

            query = query.Trim();

            if (query.Length > 0)
            {
                GeocodeService.Find(query, (o, ev) =>
                                               {
                                                   List<GeocodeResult> results = ((GeocodeResultArgs) ev).Results;

                                                   if (results.Count > 0)
                                                   {
                                                       ListLocations(results);
                                                       VisualStateManager.GoToState(this, "OpenGeoFindListBox", true);
                                                   }
                                               });
            }
        }

        private void ListLocations(List<GeocodeResult> results)
        {
            GeocodeFindResults.ItemsSource = results;
        }

        private void GeocodeFindResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var lb = sender as ListBox;
            if (lb != null)
            {
                object result = lb.SelectedItem;
                if (result != null)
                {
                    GeocodeRoutePoints.Items.Add(result);
                    VisualStateManager.GoToState(this, "OpenGeoRouteListBox", true);
                    ClearSearchFields();
                }
            }
        }

        /// <summary>
        /// Key event handler to automatically start the geocode search
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FindText_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                GeocodeFind();
            }
        }

        private void AddRoute_Click(object sender, RoutedEventArgs e)
        {
            var locations = new List<Point>();

            foreach (GeocodeResult p in GeocodeRoutePoints.Items)
            {
                locations.Add(new Point {X = p.Locations[0].Longitude, Y = p.Locations[0].Latitude});
            }

            RouteService.GetDirections(locations, (o, ev) =>
                                                      {
                                                          RouteResult result = ((RouteResultArgs) ev).Result;
                                                          var points = new ObservableCollection <Point>();
                                                          foreach (var loc in result.RoutePath.Points)
                                                          {
                                                              points.Add(new Point(loc.Longitude, loc.Latitude));
                                                          }
                                                          if (routeLayer == null)
                                                          {
                                                              routeLayer = new GeometryLayer(MapInstance) {ID = "ROUTELAYER"};
                                                              routeLayer.UpdateMode = GeometryLayer.UpdateModes.TransformUpdate;
                                                              MapInstance.Layers.Add(routeLayer);
                                                          }
                                                          else
                                                          {
                                                              routeLayer.Clear();
                                                          }
                                                          routeLayer.Add(new LineString {Points = points, LineThickness = 2, LineColor = Colors.Blue});
                                                          //new LineString(routeLayer) { Points = points, LineThickness = 2, LineColor = Colors.Blue };
                                                          //add pins for the locations specified
                                                          foreach (Point point in locations)
                                                          {
                                                              routeLayer.Add(new Pushpin{Point = point});
                                                              //new Pushpin(routeLayer) { Point = point };
                                                          }
                                                      });

            ClearSearchFields();

            VisualStateManager.GoToState(this, "CloseGeoRouteListBox", true);
            VisualStateManager.GoToState(this, "CloseGeoFindListBox", true);
        }

        private void ToggleRoute_Click(object sender, RoutedEventArgs e)
        {
            if (routeLayer != null)
            {
                routeLayer.IsVisible = !routeLayer.IsVisible;
                routeLayer.Visibility = Visibility.Collapsed;
            }
        }

        #region ILayer APIs

        public string ID { get; set; }
        public bool IsVisible { get; set; }

        public Map MapInstance
        {
            get
            {
                if (_Map == null) _Map = Map.GetMapInstance(this);
                return _Map;
            }
            set
            {
                if (ReferenceEquals(_Map, value))
                {
                    return;
                }

                _Map = value;
            }
        }

        #endregion
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.