Click here to Skip to main content
16,004,406 members
Articles / Programming Languages / C#

Bing Maps in SharePoint 2010 Web Part

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 May 2010CPOL5 min read 55.2K   827   10  
This article shows how to create a simple web part in SharePoint 2010 and use Bing Maps service to render maps.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using VisualWebPartProject1.GeocodeService;
using VisualWebPartProject1.ImageryService;
using VisualWebPartProject1.ControlTemplates;

namespace VisualWebPartProject1.BingMapsWebPart
{
    public partial class BingMapsWebPartUserControl : UserControl
    {
        const string longititudeKey = "longititude";
        const string latitudeKey = "latitude";
        const string zoomKey = "zoom";

        double longititude;
        double latitude;
        int zoom = 15;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Visible = true;
            if (ViewState[longititudeKey] != null)
            {
                longititude = (double)ViewState[longititudeKey];
                latitude = (double)ViewState[latitudeKey];
                zoom = (int)ViewState[zoomKey];
            }

            //if (!Page.IsPostBack)
            {
                ((BingMapNav)BingMapNav1).ZoomOutButtonClick += new BingMapNav.ButtonClickDelegate(ZoomOut);
                ((BingMapNav)BingMapNav1).ZoomInButtonClick += new BingMapNav.ButtonClickDelegate(ZoomIn);
                ((BingMapNav)BingMapNav1).ScrollDownButtonClick += new BingMapNav.ButtonClickDelegate(ScrollDown);
                ((BingMapNav)BingMapNav1).ScrollLeftButtonClick += new BingMapNav.ButtonClickDelegate(ScrollLeft);
                ((BingMapNav)BingMapNav1).ScrollRightButtonClick += new BingMapNav.ButtonClickDelegate(ScrollRight);
                ((BingMapNav)BingMapNav1).ScrollUpButtonClick += new BingMapNav.ButtonClickDelegate(ScrollUp);
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState[longititudeKey] = longititude;
            ViewState[latitudeKey] = latitude;
            ViewState[zoomKey] = zoom;
        }

        protected void ZoomOut()
        { 
            zoom -=2;
            lblError.Text = "New Zoom " + zoom;
            mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);
        }

        protected void ZoomIn()
        {
            zoom += 2;
            lblError.Text = "New Zoom " + zoom;
            mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);
        }

        protected void ScrollDown()
        {
            latitude -= 0.2;
            lblError.Text = "New latitude " + latitude;
            mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);
        }

        protected void ScrollLeft()
        {
            longititude -= 0.2;
            lblError.Text = "New longititude " + longititude;
            mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);
        }

        protected void ScrollRight()
        {
            longititude += 0.2;
            lblError.Text = "New longititude " + longititude;
            mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);
        }

        protected void ScrollUp()
        {
            latitude += 0.2;
            lblError.Text = "New latitude " + latitude;
            mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);
        }


        protected void btnGetMap_Click(object sender, EventArgs e)
        {
            lblError.Visible = false;
            try
            {
                //Get map

                GeocodeResponse resp = BingMapHelper.GeocodeAddressGeocodeResponse(txtAddress.Text);
                latitude = resp.Results[0].Locations[0].Latitude;
                longititude = resp.Results[0].Locations[0].Longitude;

                //Get URI and set image
                mapImage.ImageUrl = BingMapHelper.GetImagery(latitude, longititude, zoom);

                //Show image
                mapImage.Visible = true;
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = ex.Message + "<BR>" + ex.StackTrace;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions