Location and export of GPX files on Windows Phone maps

Source: Internet
Author: User
Tags polyline

1. using ArcGIS Runtime for WindowsPhone SDK to develop a mobile map application, you can locate the location and export multiple location information as a GPX file for other software, such as ArcMap and Google Earth. This article describes how to locate a map and export a GPX file. 2 GeoCoordinateWatcherWindows Phone SDK provides the GeoCoordinateWatcher class for locating functions. You need to introduce the System. Device. dll library and add the System. Device. Location namespace. 2.1 When GeoCoordinateWatcher is used for parameter settings, you need to create an instance and set basic attributes such as positioning accuracy and moving spacing. In addition, you can monitor events such as status changes and result changes. The following code is used: [csharp] if (_ watcher = null) {_ watcher = new GeoCoordinateWatcher (GeoPositionAccuracy. high); // use High positioning precision _ watcher. movementThreshold = 50; // the smallest moving distance, in meters. A point greater than the distance triggers the PositionChanged Event _ watcher. statusChanged + = new EventHandler <GeoPositionStatusChangedEventArgs> (watcher_StatusChanged); _ watcher. positionChanged + = new EventHandler <GeoPositionChangedEventArgs <GeoCoordinate> (watcher_PositionChan Ged);} 2.2 Start positioning function, that is, call the Start method of GeoCoordinateWatcher, as follows: [csharp] _ watcher. start (); 2.3 locate the status listening to locate the status change event. If an error occurs, you can be prompted, as follows: [csharp] void watcher_StatusChanged (object sender, GeoPositionStatusChangedEventArgs e) {switch (e. status) {case GeoPositionStatus. disabled: if (_ watcher. permission = GeoPositionPermission. denied) {txtStatus. text = "the application does not have access to the location service";} else {txtStatus. text = "the device does not support the location service";} break; Case GeoPositionStatus. initializing: break; case GeoPositionStatus. noData: txtStatus. text = "failed to get positioning data"; break; case GeoPositionStatus. ready: txtStatus. text = "GPS ready"; break;} 2.4 process the positioning result in the location change event, as shown below: [csharp] void watcher_PositionChanged (object sender, GeoPositionChangedEventArgs <GeoCoordinate> e) {try {txtStatus. text = ""; MapPoint point = new MapPoint (e. position. location. longi Tude, e. position. location. latitude); ESRI. arcGIS. client. projection. webMercator mercator = new ESRI. arcGIS. client. projection. webMercator (); MapPoint ptCurrent = mercator. fromGeographic (point) as MapPoint; // update GPS Positioning point location _ gLocation. geometry = ptCurrent; // update trajectory line if (_ gpsPoints = null) {_ gpsPoints = new PointCollection (); _ gpsPoints. add (ptCurrent);} else {_ gpsPoints. add (ptCurrent); Polyline line = n Ew Polyline (); line. paths. add (_ gpsPoints); _ gTrackLine. geometry = line;} # region record GPS location information GpsInfo gpsInfo = new GpsInfo (); gpsInfo. altitude = e. position. location. altitude; gpsInfo. course = e. position. location. course; gpsInfo. horizontalAccuracy = e. position. location. horizontalAccuracy; gpsInfo. latitude = e. position. location. latitude; gpsInfo. longpolling = e. position. location. longpolling; gpsInfo. spee D = e. position. location. speed; gpsInfo. verticalAccuracy = e. position. location. verticalAccuracy; gpsInfo. time = e. position. timestamp. dateTime; _ gpxWriter. addGpsInfo (gpsInfo); # endregion _ gpsLayer. refresh (); map. zoomToResolution (2.38865713397468, ptCurrent); // locate level 16 scale} catch (Exception ex) {MessageBox. show ("positioning error") ;}} ESRI. arcGIS. client. projection. webMercator is used to coordinate the WGS 84 longitude and latitude coordinates obtained by GPS to the Web Mer Cator projection coordinate, which needs to be selected based on the actual situation of the coordinate system. If the map coordinate system itself is WGS84, no conversion is required. If the map is Web Mercator, conversion is required, if the map is in another coordinate system, such as Xian1980 or CGCS2000, you need to call the Geometry Service of ArcGIS Server for online conversion. 2.5 Stop positioning When You Need To Stop positioning, you can directly call the Stop method as follows: [csharp] if (_ watcher! = Null) {_ watcher. stop (); txtStatus. text = ""; _ gpsLayer. graphics. clear ();} 3 export the GPX file. In the above Positioning Result processing, the location records are stored as GpsInfo objects for unified export. 3.1 The GpsInfoGpsInfo object is a set of simple attribute fields used to record the data obtained by the GPS device. Its Attributes are as follows: [csharp] public class GpsInfo {private double _ longpolling = 0; public double longpolling {get {return _ longpolling;} set {_ longpolling = value ;}} private double _ latitude = 0; public double Latitude {get {return _ latitude;} set {_ latitude = value ;}} private double _ altitude = 0; public double Altitude {get {return _ altitude;} set {_ altitude = value;} private double _ speed = 0; pu Blic double Speed {get {return _ speed;} set {_ speed = value ;}} private double _ course = 0; public double Course {get {return _ course ;} set {_ course = value ;}} private double _ horizontalAccuracy = 0; public double HorizontalAccuracy {get {return _ horizontalAccuracy;} set {_ horizontalAccuracy = value ;}} private double _ verticalAccuracy = 0; public double VerticalAccuracy {get {re Turn _ verticalAccuracy;} set {_ verticalAccuracy = value ;}} private DateTime _ time = new DateTime (); public DateTime Time {get {return _ time ;} set {_ time = value ;}}3.2 GpxWriterGpxWriter provides the function of writing GpsInfo to the gpx file. The Code is as follows: [csharp] public class GpxWriter {private string _ gpxFile = ""; private IList <GpsInfo> _ lstGpsInfo = null; private XmlWriterSettings _ settings = null; public GpxWriter (string GpxFile) {_ settings = new XmlWriterSettings (); _ settings. indent = true; _ settings. encoding = new UTF8Encoding (false); _ settings. newLineChars = Environment. newLine; _ lstGpsInfo = new List <GpsInfo> (); _ gpxFile = gpxFile;} public void AddGpsInfo (GpsInfo gpsInfo) {_ lstGpsInfo. add (gpsInfo);} public void WriteToGpx () {try {if (_ lstGpsInfo = null | _ lstGpsInfo. count = 0) return; IsolatedStorage File myIsolatedStorage = IsolatedStorageFile. getUserStoreForApplication (); if (myIsolatedStorage. fileExists (_ gpxFile) = true) {myIsolatedStorage. deleteFile (_ gpxFile);} using (IsolatedStorageFileStream stream = myIsolatedStorage. openFile (_ gpxFile, FileMode. createNew) {using (XmlWriter xmlWriter = XmlWriter. create (stream, _ settings) {// start when writing an xml file <? Xml version = "1.0" encoding = "UTF-8"?> XmlWriter. writeStartDocument (); // write the root node xmlWriter. writeStartElement ("gpx", "http://www.topografix.com/GPX/1/0"); // Add attributes to the node xmlWriter. writeAttributeString ("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); xmlWriter. writeAttributeString ("xmlns", "mbx", null, "http://www.motionbased.net/mbx"); xmlWriter. writeAttributeString ("xsi", "schemaLocation", null, "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.motionbased.net/mbx http://www.motionbased.net/site/schemas/mbx/0.0.1/mbx.xsd"); xmlWriter. writeAttributeString ("creator", "yellow east http://weibo.com/yelloweast"); xmlWriter. writeAttributeString ("version", "1.0"); // write the content to the root node xmlWriter. writeStartElement ("trk"); // write the content to the root node xmlWriter. writeStartElement ("trkseg"); // write the GPS information node foreach (GpsInfo gpsInfo in _ lstGpsInfo) {xmlWriter. writeStartElement ("trkpt"); // Add the xmlWriter attribute to the node. writeAttributeString ("lat", gpsInfo. latitude. toString (); xmlWriter. writeAttributeString ("lon", gpsInfo. longpolling. toString (); // Add the sub-node xmlWriter. writeElementString ("ele", gpsInfo. altitude. toString (); xmlWriter. writeElementString ("time", gpsInfo. time. toShortDateString () + "" + gpsInfo. time. toLongTimeString (); xmlWriter. writeElementString ("course", gpsInfo. course. toString (); xmlWriter. writeElementString ("speed", gpsInfo. speed. toString (); xmlWriter. writeEndElement (); // trkpt} xmlWriter. writeEndElement (); // trkseg xmlWriter. writeEndElement (); // trk xmlWriter. writeEndElement (); // gpx xmlWriter. writeEndDocument () ;}}_ lstGpsInfo. clear (); // Clear history} catch (Exception ex) {System. windows. messageBox. show ("error in writing GPX file:" + ex. message) ;}}} when calling, you only need to specify the gpx file name and then call related methods, such as variable definition: [csharp] private GpxWriter _ gpxWriter = null; Initialization: [csharp] DateTime now = DateTime. now; string gpxName = String. format ("{0}-{1}-{2 }_{ 3}-{4}-{5 }. gpx ", now. year, now. month, now. day, now. hour, now. minute, now. second); _ gpxWriter = new GpxWriter (gpxName); export the GPX file: [csharp] _ gpxWriter. writeToGpx ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.