Hands-On Lab: Writing a Location-Aware Gadget



Yüklə 34,31 Kb.
tarix07.11.2018
ölçüsü34,31 Kb.
tag:

classid="clsid:9DCC3CC8-8609-4863-BAD4-03601F4C65E8"

type="application/x-oleobject">

This object enables you to manage location reports that provide information about the current latitude and longitude.


Task 3: Change ReloadMap to Use the Location API


Whenever the map reloads, the gadget must use the Location API synchronously to retrieve the current location and then update the map accordingly.

  1. The current call to UpdateMap will still be useful when no other information is available. Create a try/catch block that uses the current code to handle the error condition. Modify the code in ReloadMap to match the following code:

try

{

}



catch(err)

{

UpdateMap(34.0540, -118.2370);



}

  1. Declare variables to contain the location report and its status. Add the following code before the try keyword at the beginning of ReloadMap:

var reportStatus = 0;

var report = null;



  1. Add the following code as the first line of the catch block to retrieve the location report status:

reportStatus = LatLongFactory.Status;

  1. Next, add the following code to test the report status that you retrieved. Status number four means the location report object is running. If the report is not in this state, the else block will use the default Los Angeles, California coordinates.

if (reportStatus == 4)

{


}

else


{

UpdateMap(34.0540, -118.2370);

}


  1. Now, add the code to retrieve the location report from the factory object and then update the map. Type the following code in the empty if block you created in step 4:

report = LatLongFactory.LatLongReport;

UpdateMap(report.Latitude, report.Longitude);

AddPin(report.Latitude, report.Longitude);

Task 4: Handle the New Report Event


The Location API enables you to receive location report updates asynchronously by requesting events for a particular report type. When you request events, you must handle two types of events: new report events and status changed events. For this task, only the new report event is important.

  1. Add the code to request event notifications. In ReloadMap, add the following line of code as the first line in the try block you created in the previous task:

LatLongFactory.ListenForReports(0);

The parameter provides a minimum report timing interval of zero. This will work fine for this lab, because the location provider you will use will set the report interval.



  1. Add the code to stop listening to events when the Web page unloads. Add the onunload attribute to the tag, as follows:

Be sure to keep the entire body tag on the same line.



  1. Add the event handler code. First, you must create a separate script block to contain the functions that handle the events raised by the LatLongFactory object. Add the following code immediately following the object block you created in Task 2:



  1. Add the function to handle the NewLatLongReport event. Replace the comment in the previous step with the following code:

function LatLongFactory::NewLatLongReport( report )

{

RemovePin();



MoveMap(report.Latitude, report.Longitude);

AddPin(report.Latitude, report.Longitude);

}

Task 5: Package the Desktop Gadget


Before you run the Desktop gadget, you must package two files into a zip archive.

  1. Open the Location Gadget folder.

  2. Press CTRL + A to select all files in the right pane.

  3. Right-click one of the selected files, point to Send To, and then click Compressed (zipped) Folder. Windows creates a new compressed folder.

  4. Press F2 to rename the compressed folder.

  5. Replace the .zip file name extension with the .gadget file name extension.

  6. Click Yes, when you are prompted.

You will see that Windows changes the icon for the compressed folder to a gadget icon.

Exercise 2: Modify the Weather Gadget


The Weather gadget can also use the Location API. In this exercise, you will add a bit of code to make the Weather gadget respond to changes in the Location API status, as well as new location reports.

Task 1: Open the Weather Gadget


The code that you will need to modify is in the Weather gadget HTML file.

  1. On the Desktop, open the folder named Weather Gadget.

  2. Open the subfolder named en-US.

  3. Right-click weather.html, point to Open With, and then click Microsoft Visual Studio 2008.

Task 2: Add the Location API Object


To add Location API code that makes the gadget respond to changes in location, you will first need to create a LatLongReportFactory object. For this example, you will use the HTML OBJECT element. Add the following code to weather.html after the tag:

height="0" border="0" type="application/x-oleobject">



This is the same object that you added in the previous exercise.


Task 3: Handle the Status Changed Event


For this gadget, you must handle the StatusChanged event. The Weather gadget responds to this event by displaying a small, square graphic that changes color when the status changes.

Immediately following the

tag that you added in the previous task, add the following script section:



Each time the location report status changes, the event handler will forward the new status to the MicrosoftGadget object.


Task 4: Handle the New Report Event


This gadget handles location report events. When a new location report becomes available through the event mechanism, the gadget calls a function in the MicrosoftGadget object to retrieve the current latitude and longitude from the report. The script code that automatically updates the weather gadget's location has already been written for you. That code resembles the code you wrote in the previous exercise. To handle the new report event, add the following code after the closing curly bracket (}) of the StatusChanged function that you added in the previous task:

function factory::NewLatLongReport(report)



{

MicrosoftGadget.getWeatherUpdate();



}

Task 5: Package the Weather Gadget


You must package all the weather gadget files into a zip archive.

  1. Open the Weather Gadget folder.

  2. Press CTRL + A to select all files in the right pane.

  3. Right-click one of the selected files, point to Send To, and then click Compressed (zipped) Folder. Windows creates a new compressed folder.

  4. Press F2 to rename the compressed folder.

  5. Rename the folder "Weather.gadget." Click Yes, when you are prompted.

You will see that Windows changes the icon for the compressed folder to a gadget icon.

Exercise 3: Run the Gadgets


Now that you have written the code, you can run the gadgets to see the results.

Task 1: Install and Run the Location Gadget


You can now install and run the Location gadget.

  1. In the Location Gadget Explorer window, double-click LocationSample.gadget to install the gadget.

  2. Click Install, when you are prompted.

  3. Right-click anywhere on the Desktop.

  4. Click Gadgets.

  5. Double-click the Location Sample gadget.

The Location Sample gadget opens. You will notice that the gadget displays a map with a push pin that shows the current location as Los Angeles. This location was coded into the gadget as the default location.

Task 2: Install and Run the Weather Gadget


  1. In the Weather Gadget Explorer window, double-click Weather.gadget to install the gadget.

  2. Click Install, when you are prompted.

  3. Right-click anywhere on the Desktop.

  4. Click Gadgets.

  5. Double-click the Weather gadget.

The Weather gadget opens. You will notice that the gadget displays the current weather conditions for Redmond, Washington. The red square indicates that the Location API has not provided location data.

Task 3: Enable the Location Provider


Typically, hardware devices, such as GPS devices, provide location data through a device driver. For this lab, we have installed a device driver that imitates a hardware device. The lab's device driver reads location coordinates from a short text script. The script provides latitude and longitude information, along with timing interval information. This technique enables you to see the effects of location changes as if you were moving across the United States.

  1. Double-click the Location and Other Sensors shortcut on the Desktop.

  2. For the Microsoft Sensor Skeleton Device Sample sensor, select the Enabled check box.

  3. Click Apply.

  4. Close the Control Panel.

The two gadgets you created will now receive events from the Location API. About once every second, you will see the location change on the map in the Location Sample gadget.

The Weather gadget will change its weather report based on the latest location data. The green square indicates that the Location API is actively providing data. A yellow square indicates that the Location API provided the latest data, but no weather report is available for the current location.


Lab Summary


In this lab you performed the following exercises.

  • Created a Desktop gadget that shows the current location.

  • Created a Desktop gadget that displays the weather conditions for the current location.

  • Learned how to enable a location sensor and run the gadgets.

In this hands-on lab you were introduced to using the Location API in Windows 7.


Dostları ilə paylaş:


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2017
rəhbərliyinə müraciət

    Ana səhifə