Location and export of GPX files on Windows Phone maps

Source: Internet
Author: User
Tags polyline
1 Overview

Using ArcGIS runtime for windowsphone SDK to develop a mobile map application, you can locate the location and export multiple location information to 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 geocoordinatewatcher

The Windows Phone SDK provides the geocoordinatewatcher class for locating. You need to introduce the system. device. dll library and add the system. device. Location namespace.

2.1 parameter settings

To use geocoordinatewatcher, you need to create an instance, set basic attributes such as positioning precision and moving distance, and listen for events such as status changes and result changes. The following code is used:

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_positionchanged );}
2.2 start positioning

Start the positioning function, that is, call the start method of geocoordinatewatcher, as follows:

_watcher.Start();

2.3 positioning status

Monitors status change events. If an error occurs, the system prompts the user as follows:

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 Positioning Result

To process the positioning result, you must locate the location change event as follows:

Void watcher_positionchanged (Object sender, geopositionchangedeventargs <geocoordinate> E) {try {txtstatus. TEXT = ""; mappoint point = new mappoint (E. position. location. longpolling, 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 = new polyline (); line. paths. add (_ gpspoints); _ gtrackline. geometry = line;} # region record GPS location information 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. speed = 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 ("display location information error ");}}

ESRI. arcGIS. client. projection. webmercator is used to convert the longitude and latitude coordinates of WGS 84 obtained by GPS to the Web Mercator projection coordinates, which must be selected based on the specific conditions of the coordinate system. If the map coordinate system itself is WGS84, no conversion is required, if the map is a Web Mercator, You need to convert it. If the map is in another coordinate system, such as xian1980 or cgcs2000, you need to call ArcGIS
Server geometry service for online conversion.

2.5 stop Positioning

When you need to stop positioning, you can directly call the stop method as follows:

if (_watcher != null){    _watcher.Stop();   txtStatus.Text = "";   _gpsLayer.Graphics.Clear();}

3. Export the GPX file

In the above Positioning Result processing, each location record has been stored as a gpsinfo object for unified export processing.

3.1 gpsinfo

The gpsinfo object is a simple set of attribute fields used to record the data obtained by GPS devices. Its Attributes are as follows:

public class GpsInfo    {        private double _longitude = 0;        public double Longitude        {            get { return _longitude; }            set { _longitude = 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;        public 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 { return _verticalAccuracy; }            set { _verticalAccuracy = value; }        }        private DateTime _time = new DateTime();        public DateTime Time        {            get { return _time; }            set { _time = value; }        }    }

3.2 gpxwriter

Gpxwriter provides the ability to write gpsinfo to a GPX file. The Code is as follows:

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 Ddgpsinfo (gpsinfo) {_ lstgpsinfo. add (gpsinfo);} public void writetogpx () {try {If (_ lstgpsinfo = NULL | _ lstgpsinfo. count = 0) return; isolatedstoragefile myisolatedstorage = isolatedstoragefile. getuserstoreforapplication (); If (myisolatedstorage. fileexists (_ gpxfile) = true) {myisolatedstorage. deletefile (_ gpxfile);} using (isolatedstoragefilestream stream = myisolatedstorage. openf Ile (_ gpxfile, filemode. createnew) {using (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 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:

private GpxWriter _gpxWriter = null;

Initialization:

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:

_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.