今天做个了WP7的地图的小练习,其中包括一些关机GPS根据经纬度定位,GPS距离计算,定位当前城市。翻阅了不少资料,得到了这些知识, 下面列出这些代码,供大家参考,以免在以后学习中少走弯路。。。
1:根据GPS获取经纬度
private void button2_Click(object sender, RoutedEventArgs e)
{
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.Start();
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Initializing:
break;
case GeoPositionStatus.Ready:
break;
case GeoPositionStatus.NoData:
break;
case GeoPositionStatus.Disabled:
break;
}
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
string[] ss = e.Position.Location.ToString().Split(',');
//map1..Center = new GeoCoordinate(double.Parse(ss[0]), double.Parse(ss[1]));
System.Device.Location.GeoCoordinate location = new System.Device.Location.GeoCoordinate(double.Parse(ss[0]), double.Parse(ss[1]));
//System.Device.Location.GeoCoordinate location = new System.Device.Location.GeoCoordinate(36.40, 117.00);
//36.40 ,东经117.00
Pushpin pin = new Pushpin();
pin.Location = e.Position.Location;
this.map1.Children.Add(pin);
this.map1.SetView(location, 10);
}
2:根据城市名称获取经纬度 //需引用webSeriver服务 http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/mex
private void button1_Click(object sender, RoutedEventArgs e)
{
GeocodeReference1.GeocodeServiceClient geocodeClient = new GeocodeReference1.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geocodeClient.GeocodeCompleted += new EventHandler<GeocodeReference1.GeocodeCompletedEventArgs>(geocodeClient_GeocodeCompleted);
GeocodeReference1.GeocodeRequest request = new GeocodeReference1.GeocodeRequest();
request.Credentials = new Credentials();
//request.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)this.map1.CredentialsProvider).ApplicationId;
request.Credentials.ApplicationId = "AjjGZGtCVs7lEuRn860kGigumg5hhJ8LqKXOIxpk1zwuxxQUgcrFDRWmrYfYCtFg";
request.Query = "Jinan";
geocodeClient.GeocodeAsync(request);
}
void geocodeClient_GeocodeCompleted(object sender, GeocodeReference1.GeocodeCompletedEventArgs e)
{
GeocodeReference1.GeocodeResult resule = e.Result.Results[0];
double dLatitude = resule.Locations[0].Latitude;
double dLongitude = resule.Locations[0].Longitude;
System.Device.Location.GeoCoordinate location = new System.Device.Location.GeoCoordinate(dLatitude, dLongitude);
Pushpin pin = new Pushpin();
pin.Location = location;
this.map1.Children.Add(pin);
this.map1.SetView(location,10);
}
3:得到两地区的在地图上的距离
private const double EAETH_RADIUS = 6378.137;
public static double rad(double d)
{
return d * Math.PI / 180.0;
}
public static double getDistance(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
s = s * EAETH_RADIUS;
s = Math.Round(s * 10000) / 10000;
return s;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
double dd = getDistance(36.40, 117.00, 39.57, 116.17);
}
4:nokia ovi地图服务接口