




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Windows Phone GPS EmulatorA Windows Phone Recipe IntroductionAll Windows Phone devices have a built-in Assisted GPS (aGPS), which is used by various phone applications including maps, camera, and search (to provide location-based search results). Developers can access location information on Windows
2、 Phone by using the System.Device.Location namespace, which is supported in .NET 4 and later. The GeoCoordinateWatcher class supplies location data based on latitude and longitude coordinates.Working with the GeoCoordinateWatcher is relatively simple. Later in this piece, well explain in more detail
3、 how to work with that class and how to test your application on a Windows Phone 7. However, sometimes your application requires more than just a single location to track movement, and you may need to test how your application behaves in different locations. At these times, it may look odd to be dri
4、ving around the block with your Windows Phone attached to a laptop while you try to debug your application. Dont worryyoure in good hands. The Windows Phone GPS Emulator (a small WPF application) and one WP7 DLL enable you to debug your application on the Windows Phone emulator or a real device with
5、out leaving the comfort of your home or office. Once youve completed your testing and debugging, you only need to change a single line of code to switch to the device back to real GPS. With the GPS Emulator, you can set a location anywhere on the globe by using the map display. Furthermore, you can
6、plan routes with multiple intermediate waypoints, or use Bing services to calculate driving directions between locations. Once youve planned a route, you can simulate driving through the pre-defined waypoints along the path. Image 1 GPS Emulator FunctionalityAs you can see in Image 1, most of the wi
7、ndow area of the GPS Emulator is dedicated to the Map control. You can fully interact with it by panning, zooming, and setting locations. A single left mouse click is used to set the current location (shown in a purple circle); double clicking sets a waypoint on a route. 1. Waypoint editor enables y
8、ou to add a new point by setting the latitude (Lat) and longitude (Long). You can also search for well-known locations (such as Pike Place Market in Seattle) or for a street address. Each point you find can be added to a route. You can also edit existing points on the route.2. The Route displays the
9、 list of waypoints on a given route. You can edit each point, change its location, or change the time it takes to simulate a drive from the current point to another point. 3. Simulation enables you to control the speed at which you play back the route. You can start and stop the route simulation if
10、you want to simulate a traffic light. 4. The Connection indicator turns green when your Windows Phone application is connected to the GPS Emulator. The connection is made over HTTP by using WCF.5. Map Type enables you to choose a Road, Aerial, or Hybrid view of the main map control.Using GPS Emulato
11、r with your Windows Phone ApplicationSo far we have a nice looking WPF application that interacts with Bing services to display a map, and you can use this application to simulate driving from one location to another. How does that help me debug my Windows Phone client application?When you download
12、the Windows Phone GPS Emulator recipe, youll notice there is an assembly called GPSEmulatorClient. This DLL has a class called GeoCoordinateWatcher, which is the same name as the System.Device.Location.GeoCoordinateWatcher class, just in a different name space. This homebrew, “fake” GeoCoordinateWat
13、cher class implements the IGeoPositionWatcher interface, which is the same interface that the “real” System.Device.Location.GeoCoordinateWatcher class implements. Therefore, we can say that the GPSEmulatorClient.GeoCoorinateWatcher implements the same API as the real System.Device.Location.GeoCoordi
14、nateWatcher. This means that both classes are transparent to the developer in terms using the location APIs. We added all the functions that are not defined by the IGeoPositionWatcher interface but are public in the System.Device.Location.GeoCoordinateWatcher class. As a result, you can write your a
15、pplication to use the GeoCoordinateWatcher and only change the initiation process based on your environment. Youll need to add a reference to the GpsEmulatorClient as well as the using GpsEmulatorClient; statement. In the GpsEmulatorPhoneTestClient application, we define the symbol by using the #def
16、ine keyword. Next, in the MainPage constructor, we use the #if GPS_EMULATOR statement to distinguish between our testing environment and a real device, as shown in the next code snippet: / This line has to be the first line in the file#define GPS_EMULATOR / defining a compiler GPS symbol. IGeoPositi
17、onWatcher _Watcher;#if GPS_EMULATOR _Watcher = new GpsEmulatorClient.GeoCoordinateWatcher();#else _Watcher = new System.Device.Location.GeoCoordinateWatcher();#endifSince _Watcher is of type IGeoPositionWatcher both implementationsthe real and the fakecan be casted to that variable. From this point
18、on, it is simply a matter of using the GeoCoordinateWatcher method and properties to listen to location and status changes.Working with the GeoCoordinateWatcherAs we said, the GeoCoordinateWatcher class is part of the System.Device.Location namespace. The GeoCoordinateWatcher class has the following
19、 properties: DesiredAccurecy is of type GpsPositionAccurecy, which can be Default or High. High means the GPS readings are of high resolution, providing more accurate location (down to a few meters where possible). Note that High location resolution can shorten battery life.Note: One of the GeoCoord
20、inateWatcher constructors has an input of GpsPositionAccuracy. By default it is set to Default, which means lower location resolution. Permission is of type GeoPositionPermission, which defines the applications level of access to the device. With Windows Phone you need to declare in your application
21、 manifest that you want to use the GPS device. Position is of type GeoCoordinate and holds the current location, if the GPS status is valid. Status is of type GpsPositionStatus, which indicates if the GPS readings are valid. There is one more property, MovementThreshold, which defines the minimum mo
22、vement threshold, which by default is set to zero. You need to know that the GPS fires a lot of events, about one per second. That may not seem like a lot, but if you dont move that fast or if your application doesnt need to keep track at such a high resolution, make sure you set MovementThreshold t
23、o something other than zero. You can read more about this in a post by Jaime Rodriguez.Next, youll want to register to the following events: PositionChanged occurs when the location service detects a change in position, taking into account limitations like MovementThreshold StatusChanged occurs when
24、 the status of the location service changes from Initializing to Ready, for example.Theres not a lot more to it besides handling the events and updating your application accordingly. In our test sample, we simply read the values and print them on a screen, as shown in the next code snippet and image
25、. void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs e) tbTimeAcquired.Text = e.Position.Timestamp.ToString(); tbLatitude.Text = e.Position.Location.Latitude.ToString(); tbLongtitude.Text = e.Position.Location.Longitude.ToString(); As you can see, the GeoPositionChangedEventArgs
26、 holds the position relevant to the location reading that triggered the PositionChanged event. The Position, which is of type GeoCoordinate class, includes, among other properties, Lat, Long, and time. It can include additional information, including VerticalAccuracy, altitude, speed, and more. The
27、output of our client application looks something like this: Image 2 Running the Windows Phone Test client with the GPS EmulatorThe purple circle represents the current location transmitted by the Windows Phone GPS Emulator. It turns out that Microsoft building 24, where my office is located, is at L
28、at 47.6416 and Long -122.1306.Using Bing Maps with the GPSTheres something wrong with image #2. The GPS Emulator shows Bing maps, while the Windows Phone test client is simply less attractive. But no worries, weve got a complete solution for you that explains how to work with Bing maps and the GPS.
29、You can download it as part of the recipe. If you look into the code, youll find something that looks like the following image:Image 3 Windows Phone GPS Emulator as part of a Windows Phone Lab, which can be found in the Windows Phone Training Kit. Follow a Few RulesBefore we dive into the technical
30、details of the GPS Emulator, there are few rules you need to follow: Run the GPS Emulator in Admin mode; it opens a WCF channel through which the Windows Phone application can read the location emulation. Make sure the GPS Emulator runs before calling the GPSEmulatorClient.GeoCoordinateWatcher.Start
31、 method, as none of the demos really handles this scenario. Youll need to create a Bing Maps Account to fully access the power of the GPS Emulator. You can create an account here: . Remember to remove the #define GPS_EMULATOR symbol when testing on a real device.How the GPS Emulator WorksThere are t
32、wo parts to the GPS Emulator recipe.The first is the Windows WPF application, which lets you set coordinates and route, but also behind the scenes maintains a WCF service. This WCF, defined by IGpesEmulatorService, exposes a single method, GetCurrentLocation, which returns a string. In this string,
33、the GPS Emulator application transmits the current emulated location. If you follow the code trail, youll find the implementation of the GetCurrentLocation method simply returns the transmittedLocation, which is a local variable in the GPS Emulator application that holds a string that represents the
34、 current state of the GPS emulation. For example, when you use the left mouse button to click on the map to set a new location, we set the transmittedLocation to the following:transmittedLocation = String.Format(0, 1, tbPositionLat.Text, tbPositionLng.Text);The same applies for the route simulation:
35、 Each time the location of the route simulation object is updated, we set the new location. The second part of the recipe is the GpsEmulatorClient. This is a Windows Phone DLL that mimics the function of the GeoCoordinateWatcher by implementing the same IGeoPositionWatcher interface. Looking into th
36、e code of the GpsEmulatorClient. GeoCoordinateWatcher, youll find that it contains a Service Reference to the GpsEmulator WCF service that the Windows GPS Emulator application exposes. Through this service, the GpsEmulatorClient. GeoCoordinateWatcher polls for the current location. The GpsEmulatorCl
37、ient. GeoCoordinateWatcher polls as rapidly as possible, polling each time the last call was processed. If you look at the Start method, found in the GeoCoordinateWatcher.cs file, youll see that we initialize a new instance of a GpsEmulatorClientService, and bind a handler to the GetCurrentPositionC
38、ompleted event, as shown in the following code snippet:status = GeoPositionStatus.Initializing;client = new GpsEmulatorServiceClient( new BasicHttpBinding(BasicHttpSecurityMode.None), new EndpointAddress(http:/localhost:8192/GpsEmulator); / change end point to real IP when testing on a real devicecl
39、ient.OpenCompleted += new EventHandler(client_OpenCompleted);client.GetCurrentPositionCompleted +=new EventHandler(client_GetCurrentPositionCompleted);ICommunicationObject commObject = client as ICommunicationObject;The client_GetCurrentPositionCompleted method is where we parse the string and extra
40、ct the coordinates. These are the coordinates that the GPS Emulator set to the transmittedLocation variable that we described above. Assuming that the parsing is successful, we raise the OnPositionChanged event. If you wish to test on a real device, youll need to change the EndPointAddress from http:/localhost:8192/GpsEmulator to http:/:8192/GpsEmulator.SummaryThe Windows P
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東滕州市八年級(jí)政治上冊(cè) 第1單元 讓愛駐我家 第2課 我們共有一個(gè)家教學(xué)實(shí)錄與檢測(cè) 魯教版
- 達(dá)州市通川區(qū)楊家溝煤業(yè)有限公司楊家溝煤礦礦山地質(zhì)環(huán)境保護(hù)與土地復(fù)墾方案情況
- 四川化工職業(yè)技術(shù)學(xué)院
- 肝膿腫護(hù)理相關(guān)知識(shí)
- 【人教PEP版英語(yǔ)四年級(jí)下冊(cè)】期中測(cè)試卷6
- 人教版小學(xué)四年級(jí)語(yǔ)文下冊(cè)2024-2025學(xué)年度第二學(xué)期第一單元質(zhì)量檢測(cè)試卷含參考答案
- 人教版小學(xué)四年級(jí)語(yǔ)文下冊(cè)2024-2025學(xué)年度第二學(xué)期第八單元質(zhì)量檢測(cè)試卷
- 第5單元 第14課 新年賀卡-綜合制作-教學(xué)設(shè)計(jì)2023-2024學(xué)年清華大學(xué)版(2012)初中信息技術(shù)八年級(jí)上冊(cè)001
- 網(wǎng)絡(luò)安全運(yùn)維專家簡(jiǎn)歷
- 安徽省部分地市2024-2025學(xué)年高三下學(xué)期2月聯(lián)合考試物理試題(解析版)
- 迪士尼樂園主題PPT模板
- C形根管的形態(tài)識(shí)別和治療實(shí)用教案
- 部編版《道德與法治》四年級(jí)下冊(cè)第5課《合理消費(fèi)》優(yōu)質(zhì)課件
- 京東入駐流程(課堂PPT)
- 鍋爐巡檢制度
- 切紙機(jī)說(shuō)明書-原稿
- 中國(guó)國(guó)際航空公司VI形象識(shí)別規(guī)劃提案
- 三菱PLC模擬量模塊fx2n4da中文手冊(cè)
- 金屬材料工程課程設(shè)計(jì)
- 學(xué)校突發(fā)公共衛(wèi)生事件應(yīng)急處置.ppt
- 學(xué)生課堂表現(xiàn)評(píng)價(jià)量表(20211208204532)
評(píng)論
0/150
提交評(píng)論