设为首页 收藏本站
查看: 2134|回复: 0

windows 8 metro 关于定位

[复制链接]

尚未签到

发表于 2015-5-23 10:33:18 | 显示全部楼层 |阅读模式
1、概述


  •   地理定位的方式有四种:wifi, ip地址, 手机基站, GPS。

  •   win8内置了wifi, ip地址两种方式,wifi定位的精度在350米左右,ip地址的精度在25千米。

  •   win8定位服务不提供朝向,高度,速度,地址等数据。

  •   当需要用定位服务时,需要显式地提醒用户,并在window8的隐私设置里打开定位服务。

  • 在应用能力里打开位置服务。

2、指导方针


  •   只调用一次定位的请求,用getGeopostionAsync()方法。

  •   设置轨迹数据变化阈值,通过设置MovementThreshold属性,当在此值的范围之外变化时,才触发PositionChanged事件,比如城市间的天气变化。

  •   设置位置数据报告频率,通过设置ReportInterval属性,为0时,则实时变化。注意:有些设备设置此属性可能无用。

  •   设置精度,通过设置DesiredAccuracy属性,当精度为高时,才会调用GPS。

  •   启动延迟,可能会有2秒,注意不要阻塞UI。

  •   后台运行,当应用挂起时,数据不会更新,所以考虑后台运行。

  • 捕获StatusChanged事件,了解位置服务可用状态。
  • 当用户禁止了位置服务时,调用getGeopostionAsync()会报异常,LocationStatus值是disable,这时需要提醒用户去开启位置服务。
  • 当状态不可用时,应该清除缓存数据。
  • 当用户重新开启位置功能时,不会有任何事件产生,只有依靠程序请求数据来得到这个事件。
  • 当应用从挂起到激活时,应当重新获取位置数据。
  • 提供一个刷新按钮让用户去重新获取位置数据。
  • 建议提示"Your location is currently turned off. Change your settings through the Settings charm to turn it back on.”
  • 如果位置不是重要信息,就用通知就行,如果位置是重要信息,比如地图,就用flyout.

3、C#示例
  获取位置数据



using System;
using System.Collection.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.DirectUI;
using Windows.UI.DirectUI.Controls;
using Windows.UI.DirectUI.Data;
using Windows.Devices.Geolocation;
namespace GeolocationSample
{
partial class MainPage
{
Geolocator geo = null;
public MainPage()
{
InitializeComponent();
}
private async void button1_Click(
object sender, RoutedEventArgs e)
{
if (geo == null)
{
geo = new Geolocator();
}
IGeoposition pos = await geo.GetGeopositionAsync();            
textblockLatitude.Text = "Latitude: " + pos.Coordinate.Latitude.ToString();
textblockLongitude.Text = "Longitude: " + pos.Coordinate.Longitude.ToString();
textblockAccuracy.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString();
}
}
}

  
  获取位置变化情况数据



using System;
using System.Collection.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.DirectUI;
using Windows.UI.DirectUI.Controls;
using Windows.UI.DirectUI.Data;
using Windows.Devices.Geolocation;
namespace GeolocationEventsSample
{
partial class MainPage
{
private Geolocator geo = null;
private CoreDispatcher _cd;
public MainPage()
{
InitializeComponent();
_cd = Window.Current.CoreWindow.Dispatcher;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (geo == null)
{
geo = new Geolocator();
}
if (geo != null)
{
geo.PositionChanged +=
new TypedEventHandler(geo_PositionChanged);
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (geo != null)
{
geo.PositionChanged -= new TypedEventHandler (geo_PositionChanged);
}
}
private void geo_PositionChanged(Geolocator sender, PositionChangedEventArgs e)
{
_cd.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
{
IGeoposition pos = (a.Context as IPositionChangedEventArgs).Position;
textLatitude.Text = "Latitude: " + pos.Coordinate.Latitude.ToString();
textLongitude.Text = "Longitude: " + pos.Coordinate.Longitude.ToString();
textAccuracy.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString();
}, this, e);
}
}
}

  
  设置位置数据变化阈值




using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Core;
using Windows.Devices.Geolocation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace GeolocationAdjustDistance
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class BlankPage : Page
{
private Geolocator geo = null;
private CoreDispatcher _cd;
private double prevLatitude = -1;
private double prevLongitude = -1;
private double totalDistance = 0;
public BlankPage()
{
this.InitializeComponent();
_cd = Window.Current.CoreWindow.Dispatcher;
}
///
/// Invoked when this page is about to be displayed in a Frame.
///
/// Event data that describes how this page was reached.  The Parameter
/// property is typically used to configure the page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (geo == null)
{
geo = new Geolocator();
}
if (geo != null)
{
geo.PositionChanged += new TypedEventHandler(geo_PositionChanged);
geo.StatusChanged += new TypedEventHandler(geo_StatusChanged);
geo.MovementThreshold = float.Parse(tbThreshold.Text);
tbThreshold.IsEnabled = false;
TextBox1.Text = "Tracking Started " +
"(Time, Latitude, Longitude, Distance)\n";
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (geo != null)
{
geo.PositionChanged -= new TypedEventHandler(geo_PositionChanged);
geo.StatusChanged -= new TypedEventHandler(geo_StatusChanged);
TextBox1.Text += "\nTracking Stopped.\n" +
"Total Distance recorded: " +
totalDistance.ToString("F2") + " m\n";
tbThreshold.IsEnabled = true;
}
}
private double CalculateDistance(double prevLat, double prevLong, double currLat, double currLong)
{
const double degreesToRadians = (Math.PI / 180.0);
const double earthRadius = 6371; // kilometers
// convert latitude and longitude values to radians
var prevRadLat = prevLat * degreesToRadians;
var prevRadLong = prevLong * degreesToRadians;
var currRadLat = currLat * degreesToRadians;
var currRadLong = currLong * degreesToRadians;
// calculate radian delta between each position.
var radDeltaLat = currRadLat - prevRadLat;
var radDeltaLong = currRadLong - prevRadLong;
// calculate distance
var expr1 = (Math.Sin(radDeltaLat / 2.0) *
Math.Sin(radDeltaLat / 2.0)) +
(Math.Cos(prevRadLat) *
Math.Cos(currRadLat) *
Math.Sin(radDeltaLong / 2.0) *
Math.Sin(radDeltaLong / 2.0));
var expr2 = 2.0 * Math.Atan2(Math.Sqrt(expr1),
Math.Sqrt(1 - expr1));
var distance = (earthRadius * expr2);
return distance * 1000;  // return results as meters
}
void geo_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
_cd.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
{
Geoposition pos = (a.Context as IPositionChangedEventArgs).Position;
double updateDistance = 0;
// Calculate distance;
if ( ( prevLatitude == -1 ) || ( prevLongitude == -1 ) )
{
updateDistance = 0;
} else {
updateDistance = CalculateDistance( prevLatitude, prevLongitude,
pos.Coordinate.Latitude, pos.Coordinate.Longitude );
}
// Update tracking
prevLatitude = pos.Coordinate.Latitude;
prevLongitude = pos.Coordinate.Longitude;
totalDistance += updateDistance;
// display the results.
TextBox1.Text += "Position Update: " +
pos.Coordinate.Timestamp.ToString("T") + ", " +
pos.Coordinate.Latitude.ToString("F3") + ", " +
pos.Coordinate.Longitude.ToString("F3") + ", " +
updateDistance.ToString("F2") + " m\n";
}, this, args);
}
void geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
var newStatus = args.Status;
var strStatus = "";
switch (newStatus)
{
case PositionStatus.Ready:
strStatus = "Location is available.";
break;
case PositionStatus.Initializing:
strStatus = "Geolocation service is initializing.";
break;
case PositionStatus.NoData:
strStatus = "Location service data is not available.";
break;
case PositionStatus.Disabled:
strStatus = "Location services are disabled. Use the " +
"Settings charm to enable them.";
break;
case PositionStatus.NotInitialized:
strStatus = "Location status is not initialized because " +
"the app has not yet requested location data.";
break;
case PositionStatus.NotAvailable:
strStatus = "Location services are not supported on your system.";
break;
default:
strStatus = "Unknown PositionStatus value (" +
newStatus.ToString() + ").";
break;
}
_cd.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
{
TextBox1.Text += strStatus + "\n";
}, this, args);
}
}
}

  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69762-1-1.html 上篇帖子: 《Windows 8应用开发权威指南》图书今日发售 迷你书下载 下篇帖子: Windows 8 动手实验系列教程 实验5:进程生命周期管理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表