Posts Tagged AJAX

Creating Hover-Style Info Boxes on the Bing Maps AJAX v7.0 Control

When v7.0 of the Bings Maps AJAX control was released last fall, I began testing out how easy it would be to port some of my existing code that was originally developed against the v6.3 AJAX control. I was pleasantly surprised to find the newer version performed considerably faster, and had a more natural API. However, many of the features that v6.3 gives you OTB were not available in v7.0; info boxes being one of those features. Thus I began testing how easy it would be to develop my own info boxes. I got about 90% of the way there before running into some snags with the way mouse events and pushpins interacted that made building display-on-hover style info boxes problematic. You can read more about this experience on Windows Live Developer Forums – Creating Infoboxes in Bing Maps AJAX v7.

Microsoft recently released an update for the Bing Maps AJAX v7.0 control which includes the ability to create info boxes. The initial release did not offer this feature so this is a welcome improvement. It also seems to have addressed the mouse event issues with push-pins. Using information published in the MSDN documentation and the experiences gathered from other developer’s forum postings, I have put together an example of how to use the new info box features and create hover-style info boxes that allow clickable content. Unlike the examples provided in the API documentation, my solution doesn’t require that the user click the pin to display the info box, or click the close button to hide it . Instead, they will show by hovering the mouse over the pushpin, will stay visible as long as the mouse remains on the pushpin or info box, and will then automatically hide as the mouse moves off the info box or pushpin.

Sample code showing how this can be done is provided below.

Code Snippet
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";>
<html xmlns=";>
    <script type="text/javascript" src=";></script>
    <script type="text/javascript">
        var map = null;
        var pinInfobox = null;

        // create a map object and place two test pins on it, with infobox on pin hover.
        function GetMap() {
            // Initialize the map
            var mapSettings = {
                // MapOptions
                credentials: "BING MAP CODE GOES HERE",
                // ViewOptions
                // default to roughly center of CO.
                center: new Microsoft.Maps.Location(39.1000, -105.6500),
                // this gives a combo arial and birdseye in v7
                mapTypeId: Microsoft.Maps.MapTypeId.birdseye,
                padding: 1,
                zoom: 7 // shows the whole state of CO
            map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapSettings);
            // Hide the info box when the map is moved.
            Microsoft.Maps.Events.addHandler(map, 'viewchange', mapViewChange);

            // Retrieve the location of the map center
            var pinLocation = map.getCenter();
            // Add a pin to the center of the map
            var pin = new Microsoft.Maps.Pushpin(pinLocation, { text: '1' });
            //Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox);
            Microsoft.Maps.Events.addHandler(pin, 'mouseover', pinMouseOver);
            Microsoft.Maps.Events.addHandler(pin, 'mouseout', pinMouseOut);
            // Add the pushpin to the map

            // create a second pin
            pinLocation = new Microsoft.Maps.Location(39.0000, -105.6000)
            pin = new Microsoft.Maps.Pushpin(pinLocation, { text: '2' });
            Microsoft.Maps.Events.addHandler(pin, 'mouseover', pinMouseOver);
            Microsoft.Maps.Events.addHandler(pin, 'mouseout', pinMouseOut);


        // This function will create an infobox
        // and then display it for the pin that triggered the hover-event.
        function displayInfobox(e) {
            // make sure we clear any infoBox timer that may still be active

            // build or display the infoBox
            var pin =;
            if (pin != null) {

                // Create the info box for the pushpin
                var location = pin.getLocation();
                var options = {
                    id: 'infoBox1',
                    title: 'My Pushpin Title',
                    description: 'This is the plain text description.',
                    //htmlContent: '',
                    height: 100,
                    width: 150,
                    visible: true,
                    showPointer: true,
                    showCloseButton: true,
                    // offset the infobox enough to keep it from overlapping the pin.
                    offset: new Microsoft.Maps.Point(0, pin.getHeight()),  
                    zIndex: 999
                // destroy the existing infobox, if any
                // In testing, I discovered not doing this results in the mouseleave
                // and mouseenter events not working after hiding and then reshowing the infobox.
                if (pinInfobox != null) {
                    if (Microsoft.Maps.Events.hasHandler(pinInfobox, 'mouseleave'))
                    if (Microsoft.Maps.Events.hasHandler(pinInfobox, 'mouseenter'))
                    pinInfobox = null;
                // create the infobox
                pinInfobox = new Microsoft.Maps.Infobox(location, options);
                // hide infobox on mouseleave
                    = Microsoft.Maps.Events.addHandler(pinInfobox, 'mouseleave', pinInfoboxMouseLeave);
                // stop the infobox hide timer on mouseenter
                    = Microsoft.Maps.Events.addHandler(pinInfobox, 'mouseenter', pinInfoboxMouseEnter);
                // add it to the map.

        function hideInfobox(e) {
            if (pinInfobox != null)
                pinInfobox.setOptions({ visible: false });

        // This function starts a count-down timer that will hide the infoBox when it fires.
        // This gives the user time to move the mouse over the infoBox, which disables the timer
        // before it can fire, thus allowing clickable content in the infobox.
        function startInfoboxTimer(e) {
            // start a count-down timer to hide the popup.
            // This gives the user time to mouse-over the popup to keep it open for clickable-content.
            if (pinInfobox.pinTimer != null) {
            // give 300ms to get over the popup or it will disappear
            pinInfobox.pinTimer = setTimeout(timerTriggered, 300);

        // Clear the infoBox timer, if set, to keep it from firing.
        function stopInfoboxTimer(e) {
            if (pinInfobox != null && pinInfobox.pinTimer != null) {

        function mapViewChange(e) {
        function pinMouseOver(e) {
        function pinMouseOut(e) {
            // TODO: detect if the mouse is already over the infoBox
            //  This can happen when the infobox is shown overlapping the pin where the mouse is at
            //    In that case, we shouldn't start the timer.
        function pinInfoboxMouseLeave(e) {
        function pinInfoboxMouseEnter(e) {
            // NOTE: This won't fire if showing infoBox ends up putting it under the current mouse pointer.
        function timerTriggered(e) {
<body onload="GetMap();">
    <div id='myMap' style="position: relative; width: 500px; height: 500px;">

, ,


Web Services for both AJAX Based and Silverlight Based Clients – Part I

Back in the nineties, I did some development on the old Palm Pilot devices and later tinkered with some Windows CE development as well as .Net development on Windows Mobile 5.x devices, but the experience always left me feeling a little underwhelmed. However the recent popularity of touch screen based mobile devices such as the iPhone, iPad, various Android based devices, and now the new Windows Phone 7 launch has inspired me to investigate mobile software development again.

In that pursuit, I decided to attend the local Microsoft Presents — Windows Phone 7 Unleashed event put on by the South Colorado .NET User Group in Colorado Springs. The event turned out to be a good time and provided some good hands on labs to experience what development on a Windows Phone 7 device entails. Let me just say that nearly two years ago I had heard that bringing Silverlight development to the mobile market was in the works at Microsoft and the potential prospects that could provide was a factor in my decision to invest time in learning Silverlight on the desktop. Microsoft has done a good job of ensuring those skills transfer to the mobile development experience on Windows Phone 7. I was amazed at how easily a colleague and I were able to put together a Silverlight and WCF Service enabled Windows Phone 7 application during our hands-on labs.

Given my positive experience with Windows Phone 7 mobile application development, I began wondering how I might tailor the design and architecture of the web applications I’m building today to be ready for mobile integration in the near future. The web applications I build today are generally done as ASP.Net and/or Silverlight clients and frequently make use of AJAX and/or Web Services to access server-side systems and data. Additionally, I’ve been making a strong push to leverage jQuery in my solutions so being able to work with jQuery’s AJAX services would be a huge plus.

Service Categories

For the purpose of this discussion, let me start by defining two general categories of web services, each targeted at solving different problems. The first are what I’d call private application services, where the client-side and server-side components of the system are generally controlled by the same developing party. Their interfaces and features are often purpose-specific and aren’t necessarily intended for general public consumption. These types of services are well suited to JSON and AJAX implementations.

The second type of service category could be described as public services, where the service itself is the application and its purpose is to expose data and features to any number of clients both known and unknown. Often these are implemented as RESTful services because of their discoverable nature and cross-platform compatibility. It’s the first category of services that I plan to discuss today.

Private application services generally fall into two groups that differ primarily in intended consumption scope – local or application wide. For those who follow the MVC or MVVM design patterns, local scope web services can be thought of as view-specific callbacks with very limited scope such as a specific page or component on a page. In standard ASP.Net web applications they are often implemented as a WebMethod on an ASPX page or using traditional web services (.ASMX files). Application wide services, where the reuse of the service is unlikely to extend beyond multiple pages in an application or maybe a set of related applications, are often implemented as standard web services using ASMX files or using WCF.

A number of projects I’m currently working on have a need for both local and application wide services which can be called using jQuery’s AJAX features. I also have a desire to share the application wide services with Silverlight clients and want to leverage the binary XML serialization that was made available starting with Silverlight 3 when accessing WCF services. This requirement falls right in line with Windows Phone 7 requirements and would allow me to work with these same services when doing mobile development on that platform.

So to cut short what is turning out to be a fairly long blog post, I believe I’ve come up with a project configuration that meets all my requirements.

  1. I am able to easily make JSON formatted AJAX calls, using jQuery, to page-level Web Methods, traditional ASMX Web Service methods, and WCF service methods using the same client-side syntax. This will allow me to organize my services into functional groups and scope their reuse as appropriate (page, in-application, or cross-application).
  2. I’ve also figured out a way to configure WCF services so that they are simultaneously accessible as JSON based services for AJAX clients as well as binary XML based services for Silverlight and Windows Phone 7 clients. This simply requires specific web.config file settings.

Stay tuned for a follow-up post on this subject, where I will walk you through samples of this configuration using Visual Studio 2010, jQuery and the new templating plug-in, WCF, and Silverlight 4.

, , , , , ,

Leave a comment

%d bloggers like this: