WP7备注(10)(Accelerometer+GeoCoordinateWatcher+Map Service)
2011-04-27 10:31 血糯米Otomii 阅读(493) 评论(0) 编辑 收藏 举报Accelerometer基本设置:
Library:+Microsoft.Devices.Sensors
WMAppManifest.xml:+<Capability Name="ID_CAP_SENSORS" />
Accelerometer(Silverlight):
Accelerometer acc = new Accelerometer(); acc.ReadingChanged += OnAccelerometerReadingChanged; acc.Start(); void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs args) {}
Accelerometer(XNA):
Vector3 accelerometerVector; object accelerometerVectorLock = new object(); Accelerometer accelerometer = new Accelerometer(); accelerometer.ReadingChanged += OnAccelerometerReadingChanged; void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs args) { lock (accelerometerVectorLock) { accelerometerVector = new Vector3((float)args.X, (float)args.Y, (float)args.Z); }
GeoCoordinateWatcher基本设置:
Library:+System.Device.Location
WMAppManifest.xml:+<Capability Name="ID_CAP_LOCATION" />
GeoCoordinateWatcher:
GeoCoordinateWatcher geoWatcher = new GeoCoordinateWatcher(); geoWatcher.PositionChanged += OnGeoWatcherPositionChanged; geoWatcher.Start(); void OnGeoWatcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> args) { text = String.Format("Latitude: {0:F3}\r\n" + "Longitude: {1:F3}\r\n" + "Altitude: {2}\r\n\r\n" + "{3}", args.Position.Location.Latitude, args.Position.Location.Longitude, args.Position.Location.Altitude, args.Position.Timestamp); }
Map Service:
GeoCoordinateWatcher geoWatcher = new GeoCoordinateWatcher(); TerraServiceSoapClient proxy = new TerraServiceSoapClient(); proxy.GetAreaFromPtCompleted += OnProxyGetAreaFromPtCompleted; proxy.GetTileCompleted += OnProxyGetTileCompleted; statusText.Text = "Obtaining geographic location..."; geoWatcher.PositionChanged += OnGeoWatcherPositionChanged; geoWatcher.Start(); void OnGeoWatcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> args) { // Turn off GeoWatcher geoWatcher.PositionChanged -= OnGeoWatcherPositionChanged; geoWatcher.Stop(); // Set coordinates to title text GeoCoordinate coord = args.Position.Location; ApplicationTitle.Text += ": " + String.Format("{0:F2}°{1} {2:F2}°{3}", Math.Abs(coord.Latitude), coord.Latitude > 0 ? 'N' : 'S', Math.Abs(coord.Longitude), coord.Longitude > 0 ? 'E' : 'W'); // Query proxy for AreaBoundingBox LonLatPt center = new LonLatPt(); center.Lon = args.Position.Location.Longitude; center.Lat = args.Position.Location.Latitude; statusText.Text = "Accessing Microsoft Research Maps Service..."; proxy.GetAreaFromPtAsync(center, 1, Scale.Scale16m, (int)ContentPanel.ActualWidth, (int)ContentPanel.ActualHeight); } void OnProxyGetAreaFromPtCompleted(object sender, GetAreaFromPtCompletedEventArgs args) { if (args.Error != null) { statusText.Text = args.Error.Message; return; } statusText.Text = "Getting map tiles..."; AreaBoundingBox box = args.Result; int xBeg = box.NorthWest.TileMeta.Id.X; int yBeg = box.NorthWest.TileMeta.Id.Y; int xEnd = box.NorthEast.TileMeta.Id.X; int yEnd = box.SouthWest.TileMeta.Id.Y; // Loop through the tiles for (int x = xBeg; x <= xEnd; x++) for (int y = yBeg; y >= yEnd; y--) { // Create Image object to display tile Image img = new Image(); img.Stretch = Stretch.None; img.HorizontalAlignment = HorizontalAlignment.Left; img.VerticalAlignment = VerticalAlignment.Top; img.Margin = new Thickness((x - xBeg) * 200 - box.NorthWest.Offset.XOffset, (yBeg - y) * 200 - box.NorthWest.Offset.YOffset, 0, 0); // Insert after TextBlock but before Image with logo ContentPanel.Children.Insert(1, img); // Define the tile ID TileId tileId = box.NorthWest.TileMeta.Id; tileId.X = x; tileId.Y = y; // Call proxy to get the tile (Notice that Image is user object) proxy.GetTileAsync(tileId, img); } } void OnProxyGetTileCompleted(object sender, GetTileCompletedEventArgs args) { if (args.Error != null) { return; } Image img = args.UserState as Image; BitmapImage bmp = new BitmapImage(); bmp.SetSource(new MemoryStream(args.Result)); img.Source = bmp; }