我制作了一个应用程序来获取纬度和经度并将其转换为我在模拟器上尝试过的地址,它显示lat-47.669和long= -122.124。我在设备上测试了它在日本的显示位置。但是,我在印度海得拉巴。这是怎么回事,请检查我的代码是否错误或设备问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using PhoneApp3.GeocodeService;
using Microsoft.Phone.Maps.Services;
using Windows.Devices.Geolocation;
namespace PhoneApp3
{
public partial class map : PhoneApplicationPage
{
/// <summary>
/// This variable is used to get the location
/// from the windows phone location service
/// </summary>
GeoCoordinateWatcher myLocationWatcher;
private GeoCoordinate MyCoordinate = null;
/// <summary>
/// Constructor for the PhoneApplicationPage object
/// </summary>
public map()
{
InitializeComponent();
}
private ProgressIndicator ProgressIndicator = null;
// My current location
// Reverse geocode query
private ReverseGeocodeQuery MyReverseGeocodeQuery = null;
/// <summary>
/// Accuracy of my current location in meters;
/// </summary>
private double accuracy = 0.0;
Double latitude, longitude;
/// <summary>
/// Click event handler for the high accuracy button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GetLocation(object sender, EventArgs e)
{
// Start data acquisition from the Location Service, high accuracy
//accuracy can be one default and another high
StartLocationService(GeoPositionAccuracy.High);
}
/// <summary>
/// Helper method to start up the location data acquisition
/// </summary>
/// <param name="accuracy">The accuracy level </param>
private void StartLocationService(GeoPositionAccuracy accuracy)
{
// Reinitialize the GeoCoordinateWatcher
myLocationWatcher = new GeoCoordinateWatcher(accuracy);
myLocationWatcher.MovementThreshold = 20;
// Add event handlers for StatusChanged and PositionChanged events
myLocationWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
myLocationWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
// Start data acquisition
myLocationWatcher.Start();
}
/// <summary>
/// Handler for the StatusChanged event. This invokes MyStatusChanged on the UI thread and
/// passes the GeoPositionStatusChangedEventArgs
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
//the dispatcher dispatches to the specified method when a status change occurs
Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));
}
/// <summary>
/// Custom method called from the StatusChanged event handler
/// </summary>
/// <param name="e"></param>
void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The location service is disabled or unsupported.
// Alert the user
MessageBox.Show("location is unsupported on this device");
break;
}
}
/// <summary>
/// Handler for the PositionChanged event. This invokes MyStatusChanged on the UI thread and
/// passes the GeoPositionStatusChangedEventArgs
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//Dispatcher invokes the event for position change.
Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
}
/// <summary>
/// Custom method called from the PositionChanged event handler
/// </summary>
/// <param name="e"></param>
void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// Update the TextBlocks to show the current location
LatitudeTextBlock.Text = e.Position.Location.Latitude.ToString("0.000");
LongitudeTextBlock.Text += e.Position.Location.Longitude.ToString("0.000");
}
// for converting latitude longitude to address
public void Button_Click(object sender, RoutedEventArgs e)
{
GetCurrentCoordinate();
}
private async void GetCurrentCoordinate()
{
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
try
{
Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));
accuracy = currentPosition.Coordinate.Accuracy;
latitude = Convert.ToDouble(LatitudeTextBlock.Text);
longitude = Convert.ToDouble(LongitudeTextBlock.Text);
MyCoordinate = new GeoCoordinate(latitude, longitude);
if (MyReverseGeocodeQuery == null || !MyReverseGeocodeQuery.IsBusy)
{
MyReverseGeocodeQuery = new ReverseGeocodeQuery();
MyReverseGeocodeQuery.GeoCoordinate = new GeoCoordinate(MyCoordinate.Latitude, MyCoordinate.Latitude);
// LongitudeTextBlock.Text = MyCoordinate.Longitude.ToString();
// LatitudeTextBlock.Text = MyCoordinate.Latitude.ToString();
MyReverseGeocodeQuery.QueryCompleted += ReverseGeocodeQuery_QueryCompleted;
MyReverseGeocodeQuery.QueryAsync();
}
}
catch (Exception ex)
{
// ...
LatitudeTextBlock.Text = "hiiii";
}
}
public void ReverseGeocodeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
{
if (e.Error == null)
{
if (e.Result.Count > 0)
{
MapAddress address = e.Result[0].Information.Address;
labelResults.Text = "Current Location: " + address.City + ", " + address.State;
}
}
}
}
}