在UWP应用

时间:2018-02-14 11:32:47

标签: c# uwp

我正在获得这样的位置

BasicGeoposition myLocation = new BasicGeoposition();
        if(cachedLocation != null)
        {
            ApplicationDataCompositeValue cachedPostion = (ApplicationDataCompositeValue)cachedLocation;
            myLocation.Latitude = Convert.ToDouble(cachedPostion[latKey]);
            myLocation.Longitude = Convert.ToDouble(cachedPostion[longKey]);
        }
        else
        {
            Geoposition position = await GetGeoPosition();
             //rest of the code
        }

}

此处,当设备位置关闭时,myLocation.Latitude和myLocation.Longitude将变为0。我想用myLocation和其他一些用户数据创建一个数组。但如果位置为0,我不应该将它添加到数组中。如果位置关闭,有没有任何一种方法可以将其指定为null,而不是检查每个位置是否为0。只是如何检查位置是否关闭?
请帮帮我。

更新

在另一个班级

BasicGeoposition currentLocation = await MetrixLocationAssistant.GetCurrentLocation();

string currentLatitude = MetrixFloatHelper.ConvertNumericFromNumberToDB(Convert.ToDecimal(currentLocation.Latitude));
string currentLongitude = MetrixFloatHelper.ConvertNumericFromNumberToDB(Convert.ToDecimal(currentLocation.Longitude));
personData.DataFields.Add(new DataField("geocode_lat", currentLatitude));
personData.DataFields.Add(new DataField("geocode_long", currentLongitude));

1 个答案:

答案 0 :(得分:2)

根据documentation for the Geolocator class,您可以检索地理位置服务的访问状态,如下所示:

var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
    case GeolocationAccessStatus.Allowed:
        break;

    case GeolocationAccessStatus.Denied:
        break;

    case GeolocationAccessStatus.Unspecified:
        break;
}

但是将几个坐标检查为零并不是很昂贵。除非你的应用程序是关于在大西洋的非洲南部航行,否则特别是(0,0)坐标不太可能造成麻烦。