Tuesday, April 26, 2011

How to show a pushpin with information box by Bing Maps V7.0

How to show a pushpin with information box by Bing Maps V7.0
    <div class='mapb'><a href='#'>Show Map</a></div>
    <div id='BingMapDiv'/>



    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0">
    </script>
    <script type="text/javascript">
        var map = null;
        var bingmapkey = "<%=Registration.GeoLocation.BingMapKey%>";
        var pinlatlon = "45.410309,-75.674057";
        var pindescription = 'Push pin title';


        function GetMap() {
            map = new Microsoft.Maps.Map(document.getElementById("BingMapDiv"), { credentials: bingmapkey, showCopyright: false, showDashboard: false, showScalebar: false, showMapTypeSelector: false });

            var pinPoint = pinlatlon;
            var pinlocation = map.getCenter();
            if (pinPoint != "") {
                pinlocation = new Microsoft.Maps.Location(pinlatlon.split(",")[0], pinlatlon.split(",")[1]);
            }

            var pin = new Microsoft.Maps.Pushpin(pinlocation);
            pinInfobox = new Microsoft.Maps.Infobox(pinlocation, { title: 'Title', visible: true, description: pindescription });
            Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox);
            Microsoft.Maps.Events.addHandler(map, 'viewchange', hideInfobox);

            map.entities.push(pin);
            map.entities.push(pinInfobox);

            map.setView({ center: pinlocation, zoom: 10 });
        }

        function displayInfobox(e) {
            pinInfobox.setOptions({ visible: true });
        }
        function hideInfobox(e) {
            pinInfobox.setOptions({ visible: false });
        }

        $('.mapb').live('click'function () { $('#BingMapDiv').toggle(); if ($('#BingMapDiv').is(':visible')) GetMap(); });

    </script>


No comments:

Post a Comment