Geolocator geolocator = new Geolocator(); //geolocator.DesiredAccuracy = Windows.Devices.Geolocation.PositionAccuracy.High; geolocator.DesiredAccuracyInMeters = 50; try { Geoposition geoposition = await geolocator.GetGeopositionAsync(TimeSpan.FromMilliseconds(500),TimeSpan.FromSeconds(1)); textLatitude.Text = "Latitude: " + geoposition.Coordinate.Latitude.ToString("0.0000000000"); textLongitude.Text = "Longitude: " + geoposition.Coordinate.Longitude.ToString("0.0000000000"); textAccuracy.Text = "Accuracy: " + geoposition.Coordinate.Accuracy.ToString("0.0000000000"); }
使用以下方式获取坐标,我试图测试设备是否可以使用以下代码正确定位我的位置:
if( Math.Abs(geoposition.Coordinate.Latitude - 45.3285) < 0.001 ){ if (Math.Abs(geoposition.Coordinate.Longitude - 14.4474) < 0.001) { txt.Text = "KONT"; } }
问题是位置的精确度非常小,如果我尝试使用更精确的坐标,它将永远不会再得到相同的坐标,并且使用这个代码的准确性是非常糟糕的(甚至可以失败甚至300米).
解决方法
Geoposition geoposition = await geolocator.GetGeopositionAsync(TimeSpan.FromMilliseconds(500),TimeSpan.FromSeconds(1));
你给它只有1秒,而更准确的位置需要时间.
我的例子:
Geolocator geolocator; Geoposition geoposition; public MainPage() { this.InitializeComponent(); geolocator = new Geolocator(); geolocator.DesiredAccuracyInMeters = 10; geolocator.ReportInterval = 0; myButton.Click += async (sender,e) => { geoposition = await geolocator.GetGeopositionAsync(); string latitude = geoposition.Coordinate.Latitude.ToString("0.0000000000"); string Longitude = geoposition.Coordinate.Longitude.ToString("0.0000000000"); string Accuracy = geoposition.Coordinate.Accuracy.ToString("0.0000000000"); }; }
上面的代码重新定位了一个位置(在我的情况下),精度为〜35米,但等待了大约20-30秒.还要注意,精度取决于可用的卫星的数量.
>将Geolocator.ReportInterval设置为0:
Apps that do require real-time data should set ReportInterval to 0,to indicate that no minimum interval is specified. On Windows,when the report interval is 0,the app receives events at the frequency that the most accurate location source sends them. On Windows Phone,the app will receive updates at a rate dependent on the accuracy requested by the app.
>设置Geolocator.DesiredAccuracyInMeters到10米:
◾If the user is trying to share his position,the app should request an accuracy of about 10 meters.
>尝试在启动Geolocator并重新启动它之间dealy:
Consider start-up delay. The first time an app requests location data,there might be a short delay (1-2 seconds) while the location provider starts up. Consider this in the design of your app’s UI. For instance,you may want to avoid blocking other tasks pending the completion of the call to GetGeopositionAsync.