Enterprise Integration Zone is brought to you in partnership with:

Brian Hitney is a Developer Evangelist with Microsoft Corporation focused on cloud computing. He frequently delivers presentations and works with local community groups and customers on emerging technologies, .NET, and developer tools. Prior to his Developer Evangelist role, Brian worked as a software engineer on a Windows team in Redmond, and before he joined Microsoft he helped build large scale e-commerce applications for various companies across the United States. Brian is based out of Greensboro, NC. Brian is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

Creating a Great Mashup: Pt. 3

07.09.2013
| 2209 views |
  • submit to reddit

This post details features used by Earthquake Explorer, a Windows 8 app that displays earthquake information on Bing maps.  Earthquake Explorer was based off of the Earthquakes mashup starter kit on github.  Read part 1 hereRead part 2 here

We left off Part 2 talking about creating a compelling live tile in Windows 8.  This really is a must-do feature in Windows 8 – primarily because a live tile creates a more compelling app experience.  It encourages the user to keep the live tile ‘pinned-left’ (that is, more likely on their primary screen), and encourages the user to open the app more frequently.  That equals higher app usage, higher ad impressions, etc. 

Because the earthquake app is consuming earthquake data on our backend using Windows Azure Mobile Services, it’s trivial to send notifications when new earthquakes are detected.  We can send a nice live tile that looks like so:

image

Clearly, this is better than displaying _nothing_, and the map adds a professional look to it.  We can also combine this with badges – for example, in Dark Skies, I do something similar when users add new locations to the map, adding a badge notification that displays how many new locations are nearby:

image

The first step in doing this is to determine which tile template to use.  Because the map is the main image, this is a pretty simple as we’d want the map to take up the main tile.  The place to look is the tile template catalog on MSDN.  In this case, TileWideImageAndText02 seems to fit the bill for wide tiles, and TileSquareImage is the bet bet for smaller, square-sized tiles.  From the template page:

image image

The key here is the tile XML – it’s the same if we’re doing a push notification, if the app is updating its own tile, or if Windows is updating the tile using the TileUpdater using periodic updates.  To get the image, we’ll use the Bing Maps REST API, which requires a valid Bing Maps key from the portal site

For a wide tile, we can get a static map using a URL like so, specifying the zoom level (2), centered location (36 latitude, –79 longitude), pushpin location (also at 36 latitude, –79 longitude), and a size of 310x100:

http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/36,-79/2?mapSize=310,100&pushpin=36,-79;48&key={your_api_key}

Which returns an image like so:

From the Windows Azure Mobile Service, it’s fairly simple to send a notification with some code like the below.  I’m removing some of the dynamic text to shorten it up a little, but realistically we’d be setting the location of the pin(s) and text of the tile itself dynamically:

function sendNotification(channelUri) {

   

      var baseImageUrl = "http://dev.virtualearth.net/" + 

          "REST/v1/Imagery/Map/Road/36,-79/2?[mapsize]" +

           "&pushpin=36,-79;48&key={your_api_key}";

     

      var smallImage = baseImageUrl.replace("[mapsize]", "mapSize=150,150");

      var bigImage = baseImageUrl.replace("[mapsize]", "mapSize=310,100");

   

      var payload = "<tile><visual><binding template='TileWideImageAndText02'>" +

          "<image id='1' src='" + xmlEscape(bigImage) + "' alt='map'/>" +

          "<text id='1'>Some top line text here</text>" +

          "<text id='2'>Some bottom line text here</text>" +

          "</binding>" +

          "<binding template='TileSquareImage' branding='none'>" +

          "<image id='1' src='" + xmlEscape(smallImage) +

          "' alt='map'/></binding></visual></tile>";

   

      var theTag = 'some_identifier';

      push.wns.send(channelUri, payload, 'wns/tile',

          {

              client_id: 'your ms-app:// id',

              client_secret: 'your client secret',

              headers: { 'X-WNS-Tag': theTag }

          },

          function (error, result) {

              if (error) {

                  console.log("(" + error.statusCode + ") Error sending notification");             

              }

          }

      );

  }

(Note: the xmlEscape method is omitted, but is a simple method that escapes special characters for the XML document.)  What we’re doing here is using the push.wns (Windows Notification Service) object to send the tile XML directly.  If you review the script reference, it is certainly simpler to use the push.wns.sendTileWideImageAndText02 method, but I generally prefer to build the XML directly as done above.  The reason is that it allows you to bundle multiple notifications together – such as including both the wide and narrow tile versions.  (The other side benefit is that this allows you to enter the application package ID and secret, which should allow a single WAMS to send notifications to multiple applications.)

We’ll look at other mashup examples in future posts!





Published at DZone with permission of Brian Hitney, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)