日期:2014-05-16 浏览次数:21002 次
通过ArcGIS Runtime for WindowsPhone SDK开发手机地图应用,能够定位所在位置,并将多个定位位置信息导出为GPX文件,供其他软件,如ArcMap、Google Earth等调用。本文将介绍如何实现地图定位,以及导出GPX文件的功能。
Windows Phone SDK提供了GeoCoordinateWatcher类实现定位相关功能,需要引入System.Device.dll库,添加System.Device.Location命名空间。
使用GeoCoordinateWatcher,需要创建一个实例,并设置定位精度、移动间距等基本属性,另外监听状态变化、结果变化等事件。如下代码所示:
if (_watcher == null)
{
_watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // 使用高定位精度
_watcher.MovementThreshold = 50; // 最小移动间距,单位米,大于该间距的点才会触发PositionChanged事件
_watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
_watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
启动定位功能,即调用GeoCoordinateWatcher的Start方法,如下:
_watcher.Start();
监听定位状态变化事件,如果出错可提示用户,如下:
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
if (_watcher.Permission == GeoPositionPermission.Denied)
{
txtStatus.Text = "应用程序无权访问定位服务";
}
else
{
txtStatus.Text = "设备不支持定位服务";
}
break;
case GeoPositionStatus.Initializing:
break;
case GeoPositionStatus.NoData:
txtStatus.Text = "获取定位数据失败";
break;
case GeoPositionStatus.Ready:
txtStatus.Text = "GPS已经就绪";
break;
}
}
对定位结果的处理,需要在定位位置变化事件中进行,如下所示:
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
try
{
txtStatus.Text = "";
MapPoint point = new MapPoint(e.Position.Location.Longitude, e.Position.Location.Latitude);
ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
MapPoint ptCurrent = mercator.FromGeographic(point) as MapPoint;
//更新GPS定位点位置
_gLocation.Geometry = ptCurrent;
//更新轨迹线
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 记录GPS定位信息
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.Longitude = e.Position.Location.Longitude;
gpsInfo.Speed = e.Position.Location.Speed;
gpsInfo.VerticalAccuracy = e.Position.Location.VerticalAccuracy;
gpsInfo.Time = e.Position.Timestamp.DateTime;
_gpxWriter.AddGpsInfo(gpsInfo);
#endregion
_gpsLayer.Re