Export (0) Print
Expand All

Listening for Civic Address Report Events

You can write JScript code that receives civic address reports through an event handler.

The following example code shows some simple HTML that displays the contents of the civic address report that is received when the NewCivicAddressReport event occurs.

This example calls RequestPermissions if no data provider is available.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Civic Address Events Example</title>
</head>
<body onload = "OnLoadPage()"; 
      onunload = "CivicFactory.StopListeningForReports()">

        <!-- Civic address Location report factory object -->
        <object id="CivicFactory" 
            classid="clsid:2A11F42C-3E81-4ad4-9CBE-45579D89671A"
            type="application/x-oleobject">
        </object> 
        
        <p>This example listens to civic address report events.</p>
        <br>
        
        Report status: <input type="TEXT" name="txtStatus" value=""> 
        <br><br>

        <!-- Text boxes for Civic address fields. -->
        Address line 1: <input type="TEXT" name="txtAd1" value=""> <br>
        Address line 2: <input type="TEXT" name="txtAd2" value=""> <br>
        City: <input type="TEXT" name="txtCit" value=""> <br>
        State or province: <input type="TEXT" name="txtStP" value=""> <br>
        Country or region: <input type="TEXT" name="txtCyR" value=""> <br>
        Postal code: <input type="TEXT" name="txtPst" value=""> <br>
        
        Timestamp: <input type="TEXT" name="txtTmC" value=""> <br>

        <br>
        
        <script language="javascript" for="CivicFactory">

        // JScript date object assumes local time.
        // If you display the timestamp directly, the
        // the string will show UTC plus the local time zone,
        // such as "PST". This function ensure that local time
        // is displayed in the string.
        function GetTimeString(timestamp)
        {
             var datetime = new Date(timestamp);
             var dateLocal = new Date();

             dateLocal.setTime(Date.UTC(datetime.getFullYear(),
                                      datetime.getMonth(),
                                      datetime.getDate(),
                                      datetime.getHours(),
                                      datetime.getMinutes(),
                                      datetime.getSeconds(),
                                      datetime.getMilliseconds()));

              return dateLocal.toString();
         }

        // Handles the new civic address report event.
        function CivicFactory::NewCivicAddressReport(report)
        {  
            // Display the properties.
            txtAd1.value = report.AddressLine1;
            txtAd2.value = report.AddressLine2;
            txtCit.value = report.City;
            txtStP.value = report.StateProvince;
            txtCyR.value = report.CountryRegion;
            txtPst.value = report.PostalCode;         
            txtTmC.value = GetTimeString(report.Timestamp);
        }

        // Handles the status changed event.
        function CivicFactory::StatusChanged(status)
        {
            DisplayStatus(status);            
        }
        </script>
        
        <script>
        <!--
        
        function OnLoadPage()
        {
            // Request a new report each second.
            CivicFactory.ListenForReports(1000);
            
            // Test for the current status.
            var reportstatus = CivicFactory.Status;
            
            DisplayStatus(reportstatus);
            
            if(reportstatus == 2)  // Access denied?
            { 
                // Prompt the user for permission.
                CivicFactory.RequestPermissions(0);
            } 
        }
        
        // Displays the current report status.
        function DisplayStatus(status)
        {
            switch(status)
            {
                case 0:
                    txtStatus.value = "Report not supported."
                    break;
                case 1:
                    txtStatus.value = "Error."
                    break;
                case 2:
                    txtStatus.value = "Access denied."
                    break;
                case 3:
                    txtStatus.value = "Initializing."
                    break;
                case 4:
                    txtStatus.value = "Running."
                    break;
                default:
                    break;
            }
        }
        
        -->
        </script>
</body>
</html>


 

 

Community Additions

ADD
Show:
© 2014 Microsoft