如何从Windows Phone 8.1获取当前国家/地区位置以及可能的CountryCode?我做到了这一点,但FindLocationsAsync引发了我的异常"价值不在预期的范围内"
private async void LocateMe()
{
Geolocator GeoLoc = new Geolocator();
GeoLoc.DesiredAccuracyInMeters = 50;
try
{
Geoposition GeoPosition = await GeoLoc.GetGeopositionAsync();
MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync("", GeoPosition.Coordinate.Point, 5);
if (result.Status == MapLocationFinderStatus.Success)
{
foreach (MapLocation mapLocation in result.Locations)
{
string strCountryCode = mapLocation.Address.CountryCode;
}
}
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
答案 0 :(得分:1)
它被称为反向地理定位试试这个
var geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 100;
Geoposition position = await geolocator.GetGeopositionAsync();
//reverse geocoding
BasicGeoposition myLocation = new BasicGeoposition
{
Longitude = position.Coordinate.Longitude,
Latitude = position.Coordinate.Latitude
};
Geopoint pointToReverseGeocode = new Geopoint(myLocation);
MapLocationFinderResult result = await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
//here also it should be checked if there result isn't null and what to do in such a case
string country = result.Locations[0].Address.Region;
答案 1 :(得分:1)
你应该试试这个..我宣布了Map_Tapped事件 private async void
private async void mapNearByUser_MapTapped(Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args)
{
//Your Map Tapped
Geopoint pointToReverseGeocode = new Geopoint(args.Location.Position);
// Reverse geocode the specified geographic location.
MapLocationFinderResult result =
await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
var resultText = new StringBuilder();
try
{
if (result.Status == MapLocationFinderStatus.Success)
{
resultText.AppendLine(result.Locations[0].Address.District + ", " + result.Locations[0].Address.Town + ", " + result.Locations[0].Address.Country);
}
MessageBox(resultText.ToString());
}
catch
{
//MessageBox(resultText.ToString());
MessageBox("Please wait..");
}
}
mapNearByUser_MapTapped(Windows.UI.Xaml.Controls.Maps.MapControl sender,Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args) { //你的地图点击 Geopoint pointToReverseGeocode = new Geopoint(args.Location.Position);
// Reverse geocode the specified geographic location.
MapLocationFinderResult result =
await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
var resultText = new StringBuilder();
try
{
if (result.Status == MapLocationFinderStatus.Success)
{
resultText.AppendLine(result.Locations[0].Address.District + ", " + result.Locations[0].Address.Town + ", " + result.Locations[0].Address.Country);
}
MessageBox(resultText.ToString());
}
catch
{
//MessageBox(resultText.ToString());
MessageBox("Please wait..");
}
}