Accelerometer
Accelerometer measures acceleration.
It measures acceleration in three axis X,Y,Z as follows :
This coordinate system remains fixed relative to the phone regardless of orientation change.
In this position the accelerometer reading is (0,-1,0) . -1 means g in -Y direction
If you rotate above emulator 90 degrees counterclockwise you will get (-1,0,0)
simple accelerometer sample code :
AccelerometerSample.zip
Adding acc_CurrentValueChanged handler and starting the accelerometer obect - acc
Defining the handler
Writing to text block from non UI thread :
The problem is that access to a UI object such as TextBlock can be done only from the main thread (thread safety). However, we can not be sure that the handler will be executed on the main thread context, so we first check this by calling CheckAccess if it is true then access to this UI object can be done. If CheckAccess is false then you must use the Dispatcher.BeginInvoke to queue executing the handler on the main thread.
Note that BeginInvoke is used with Action as argument type. Action encapsulate a method which return nothing and has no arguments.
Remarks
- Note that Accelerometer reside in Microsoft.Devices.Sensors and you might need to add a reference.
- Altough this is the default note that WMAppManifest must have the following :
otherwise the accelometer will not function!
Run the application , invoke the accelerometer via the emulator and the same values X,Y,Z which appears on the emulator will appear also on the application TextBlock
Note that as the emulator start the acceleration is 0,-1,0 , why ?
Geographic Location
Geographic location is supported in Windows Phone 7 using A-GPS.
Core class for Location - GeoCoordinateWatcher
sample code :
The sample register a handler for event name PositionChanged for GeoCoordinatorWatcher object. The handler write the longitude and latitude via GeoCoordinate argument into a textblock after the watcher is started.
Latitude
- Angular distance from the equator
- An angle between 0 - 90 degrees.
- North pole latitude is 90 while south pole is -90
Longitude
- An angle between 180 to -180
A graphical representation of longitude and latitude
A perspective view of the Earth showing how latitude (φ) and longitude (λ) are defined on a spherical model. The graticule spacing is 10 degrees. |
Run the application and verify that latitude and longitude are the same in the emulator and application
Map Service
Location is possible as was shown in terms of latitude\longitude ,but most people prefer location in terms of map.
Microsoft supports Map Control.
Before using map service you will need to do the following once :
Sample code using map control
MapService.zip
The sample contains map control and four buttons : Zoom In, Zoom Out, Road Mode , Aerial Mode
Add namespace Microsoft.Phone.Controls.Maps to MainPage.xaml.cs
Drag and drop map control from the toolbox
MainPage.xaml
Note that you should replace "Your Key" with your Bing Map Key.
MainPage.xaml.cs
Running the application without providing Bing Map Key and clicking the "Zoom In" will produce the following
Running the application providing Bing Map Key and clicking the "Zoom In" will produce the following (notice that the invalid error does not appers)
Nathan
No comments:
Post a Comment