Table of Contents

Source Code WpfDesktopEditionSample KMLFile.zip

App.xaml.cs

 using System.Windows;  
 
 /// <summary>  
 /// Interaction logic for App.xaml  
 /// </summary>  
 namespace DisplayASimpleMap  
 {  
     public partial class App : Application  
     {  
     }  
 }  
 
 
 

Loading.xaml.cs

 
 using System;  
 using System.Windows;  
 using System.Windows.Controls;  
 using System.Windows.Shapes;  
 using System.Windows.Threading;  
 
 namespace DisplayASimpleMap  
 {  
     /// <summary>  
     /// Interaction logic for Loading.xaml  
     /// </summary>  
     public partial class Loading : UserControl  
     {  
         #region Data  
 
         private readonly DispatcherTimer animationTimer;  
 
         #endregion Data  
 
         #region Constructor  
 
         public Loading()  
         {  
             InitializeComponent();  
             animationTimer = new DispatcherTimer(  
                 DispatcherPriority.ContextIdle, Dispatcher);  
             animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 90);  
         }  
 
         #endregion Constructor  
 
         #region Private Methods  
 
         private void HandleAnimationTick(object sender, EventArgs e)  
         {  
             SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;  
         }  
 
         private void HandleLoaded(object sender, RoutedEventArgs e)  
         {  
             const double offset = Math.PI;  
             const double step = Math.PI * 2 / 10.0;  
 
             SetPosition(C0, offset, 0.0, step);  
             SetPosition(C1, offset, 1.0, step);  
             SetPosition(C2, offset, 2.0, step);  
             SetPosition(C3, offset, 3.0, step);  
             SetPosition(C4, offset, 4.0, step);  
             SetPosition(C5, offset, 5.0, step);  
             SetPosition(C6, offset, 6.0, step);  
             SetPosition(C7, offset, 7.0, step);  
             SetPosition(C8, offset, 8.0, step);  
         }  
 
         private void HandleUnloaded(object sender, RoutedEventArgs e)  
         {  
             Stop();  
         }  
 
         private void HandleVisibleChanged(object sender,  
             DependencyPropertyChangedEventArgs e)  
         {  
             bool isVisible = (bool)e.NewValue;  
 
             if (isVisible)  
                 Start();  
             else  
                 Stop();  
         }  
 
         private void SetPosition(Ellipse ellipse, double offset,  
             double posOffSet, double step)  
         {  
             ellipse.SetValue(Canvas.LeftProperty, 50.0  
                 + Math.Sin(offset + posOffSet * step) * 50.0);  
 
             ellipse.SetValue(Canvas.TopProperty, 50  
                 + Math.Cos(offset + posOffSet * step) * 50.0);  
         }  
 
         private void Start()  
         {  
             animationTimer.Tick += HandleAnimationTick;  
             animationTimer.Start();  
         }  
 
         private void Stop()  
         {  
             animationTimer.Stop();  
             animationTimer.Tick -= HandleAnimationTick;  
         }  
 
         #endregion Private Methods  
     }  
 }  
 
 

Sample.xaml.cs

 
 using System;  
 using System.Diagnostics;  
 using System.IO;  
 using System.Windows;  
 using System.Windows.Controls;  
 using Microsoft.Win32;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.WpfDesktopEdition;  
 
 namespace DisplayASimpleMap  
 {  
     public partial class Sample : Window  
     {  
         private readonly string googleRegistryPathX86 = @"SOFTWARE\Google\GoogleEarthPlugin";  
         private readonly string googleRegistryPathX64 = @"SOFTWARE\Wow6432Node\Google\GoogleEarthPlugin";  
 
         public Sample()  
         {  
             InitializeComponent();  
         }  
 
         private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)  
         {  
             //Initialization wpfmap  
             Map1.MapUnit = GeographyUnit.DecimalDegree;  
 
             //add backgound overlay  
             Map1.Overlays.Add(new WorldMapKitWmsWpfOverlay());  
 
             //add layeroverlay to add kmllayer  
             LayerOverlay layerOverlay = new LayerOverlay();  
             Map1.Overlays.Add("OverlayForKml", layerOverlay);  
             layerOverlay.TileType = TileType.SingleTile;  
 
             Map1.CurrentExtent = new RectangleShape(-139.2, 92.4, 120.9, -93.2);  
 
             //add kml files to combobox  
             foreach (string filePath in Directory.GetFiles(@"..\..\data", "*.kml"))  
             {  
                 cmbKmlFiles.Items.Add(Path.GetFileName(filePath));  
             }  
             cmbKmlFiles.SelectedIndex = cmbKmlFiles.Items.Count - 1;  
         }  
 
         private void btnConvertToKmlAndDisplay_Click(object sender, RoutedEventArgs e)  
         {  
             if (File.Exists(txtFilePath.Text))  
             {  
                 //create shapefilefeaturelayer from txtfilepath  
                 loadingControl.Visibility = Visibility.Visible;  
                 ShapeFileFeatureLayer shapeFileLayer = new ShapeFileFeatureLayer(txtFilePath.Text);  
                 shapeFileLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
                 shapeFileLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.PointType = PointType.Bitmap;  
                 shapeFileLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image = new GeoImage(@"..\..\data\markergreen.png");  
                 shapeFileLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = new LineStyle(new GeoPen(GeoColor.SimpleColors.Red));  
                 shapeFileLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.State1;  
 
                 string currentKmlFilePathName = string.Format(@"..\..\data\convertedkmlfile\{0}.kml", Path.GetFileNameWithoutExtension(txtFilePath.Text));  
 
                 //create kmllayer from shapefilefeaturelayer  
                 KmlLayer.CreateKmlFile(currentKmlFilePathName, shapeFileLayer);  
 
                 //get layeroverlay from wpfmap  
                 LayerOverlay layerOverlay = (LayerOverlay)Map1.Overlays["OverlayForKml"];  
                 layerOverlay.Layers.Clear();  
                 layerOverlay.Layers.Add(shapeFileLayer);  
 
                 shapeFileLayer.Open();  
                 Map1.CurrentExtent = shapeFileLayer.GetBoundingBox();  
                 Map1.Refresh();  
 
                 //check if display the result in google earth.  
                 if (chkBoxDisplayInGE.IsChecked.Value)  
                 {  
                     AskToOpenGE(currentKmlFilePathName);  
                 }  
                 loadingControl.Visibility = Visibility.Hidden;  
                 MessageBox.Show("Conversion is completed");  
             }  
             else  
             {  
                 MessageBox.Show("File is not found");  
             }  
         }  
 
         private void cmbKmlFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)  
         {  
             //create a kmllayer from kml file  
             string selectedItem = cmbKmlFiles.SelectedItem.ToString();  
             KmlLayer kmlLayer = new KmlLayer(string.Format(@"..\..\data\{0}", selectedItem));  
 
             //get layeroverlay from wpfMap  
             LayerOverlay layerOverlay = (LayerOverlay)Map1.Overlays["OverlayForKml"];  
             layerOverlay.Layers.Clear();  
             layerOverlay.Layers.Add(kmlLayer);  
 
             //change the currentextent to kml layer  
             if (selectedItem.Contains("Vector.kml"))  
             {  
                 Map1.CurrentExtent = new RectangleShape("POLYGON((-140.082672375 66.698917625,-140.082672375 14.9313395,-53.598297375 14.9313395,-53.598297375 66.698917625,-140.082672375 66.698917625))");  
             }  
             else if (selectedItem.Contains("Region.kml"))  
             {  
                 System.Windows.Forms.MessageBox.Show("please keep zoom in twice to see the difference");  
                 Map1.CurrentExtent = new RectangleShape(-180, 90, 180, -90);  
             }  
             else  
             {  
                 Map1.CurrentExtent = new RectangleShape(-180, 90, 180, -90);  
             }  
             Map1.Refresh();  
         }  
 
         private void btnBrowse_Click(object sender, RoutedEventArgs e)  
         {  
             OpenFileDialog dialog = new OpenFileDialog();  
             dialog.Multiselect = false;  
             dialog.InitialDirectory = Path.GetFullPath(txtFilePath.Text);  
             dialog.Filter = "Text Files (*.shp)|*.shp";  
             if (dialog.ShowDialog().Value)  
             {  
                 txtFilePath.Text = dialog.FileName;  
             }  
         }  
 
         private void AskToOpenGE(string FilePath)  
         {  
             string mentionedString = "The KML file has been saved successfully, do you want to open it in Google Earth?";  
             if (MessageBox.Show(mentionedString, "Open in Google Earth", MessageBoxButton.YesNo) == MessageBoxResult.Yes)  
             {  
                 string googleRegistryPath = @"SOFTWARE\Google\GoogleEarthPlugin";  
                 string googleRegistryKey = "";  
                 if (Environment.Is64BitOperatingSystem)  
                 {  
                     googleRegistryPath = @"SOFTWARE\Wow6432Node\Google\GoogleEarthPlugin";  
                 }  
 
                 if (RegistryKeyExist(googleRegistryPath, googleRegistryKey))  
                 {  
                     RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(googleRegistryPath);  
                     string keyValue = registryKey.GetValue(googleRegistryKey, string.Empty).ToString();  
                     string path = Directory.GetParent(keyValue).FullName + "
client";  
                     if (File.Exists(path + "
googleearth.exe"))  
                     {  
                         RunProcess(path + "
googleearth.exe", "\"" + Path.GetFullPath(FilePath) + "\"");  
                     }  
                     else  
                     {  
                         MessageBox.Show("Please install Google Earth!");  
                     }  
                 }  
                 else  
                 {  
                     MessageBox.Show("\"Google Earth\" is not insatlled on local machine, please download at \"http://www.google.com/earth/download/ge/agree.html\"");  
                 }  
             }  
         }  
 
         private bool RegistryKeyExist(string registryPath, string keyName)  
         {  
             bool exist = false;  
             RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(registryPath);  
 
             if (registryKey != null)  
             {  
                 string keyValue = registryKey.GetValue(keyName, string.Empty).ToString();  
 
                 if (keyValue != string.Empty)  
                 {  
                     exist = true;  
                 }  
             }  
 
             return exist;  
         }  
 
         private void RunProcess(string processFileName, string fileName)  
         {  
             Process.Start(processFileName, fileName);  
         }  
 
         private void DisplayInGoogleEarth(string kmlFilePath)  
         {  
             //get googlemap registry path.  
             string googleRegistryPath = string.Empty;  
             if (Environment.Is64BitOperatingSystem)  
             {  
                 googleRegistryPath = googleRegistryPathX64;  
             }  
             else  
             {  
                 googleRegistryPath = googleRegistryPathX86;  
             }  
 
             if (Registry.LocalMachine.OpenSubKey(googleRegistryPath) == null)  
             {  
                 MessageBox.Show("google Earth is not installed");  
                 return;  
             }  
 
             string keyValue = Registry.LocalMachine.OpenSubKey(googleRegistryPath).GetValue("", string.Empty).ToString();  
             string filePath = string.Format("{0}
client
googleearth.exe", Directory.GetParent(keyValue).FullName);  
             if (File.Exists(filePath))  
             {  
                 Process.Start(filePath, Path.GetFullPath(kmlFilePath));  
             }  
             else  
             {  
                 MessageBox.Show("google Earth is not installed");  
             }  
         }  
     }  
 }