Thursday, 25 April 2013

How to add Pushpins to a Bing Map from database using ASP.NET


How to add Pushpins to a Bing Map from database using ASP.NET




Introduction
In this article I will explain how to add Pushpins to a Bing map from locations (Latitude/Longitude) stored in a SQL Server database table
Add Pushpins to Bing Map from Database in ASP.NET
Description
We will use “Bing Maps AJAX Control 7.0 ISDK” to create Bing map. You have to create a key to use Bing map in your applications. You can refer to my article“How to integrate Bing map in your website” to create an application key.
First we will create a table with some sample data in SQL Server database to store latitudes and longitudes, then we will use these lat longs to plot Pushpins to the map. We will add click event to these pushpins that will zoom the map with clicked pushpin as its center
Pushpin
A pushpin is created using Microsoft.Maps.Pushpin class whose constructor takes Location object as parameter. It is added to the map using map.entities.push method which takes Pushpin object as parameter. Events can be added to a pushpin using Microsoft.Maps.Events.addHandler method that takes pushpin, event type and callback function name as parameters
Example:
1
2
3
4
5
var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(19.890723, 84.624022), null);
map.entities.push(pushpin);
Microsoft.Maps.Events.addHandler(pushpin, 'mouseup', ZoomIn);
 
function ZoomIn(e){}
Step:1 Create a table in SQL Server named “Locations” with two columns, Latitude and Longitude
Microsoft SQL Server Management Studio Express_2013-02-07_22-23-19
Step:2 Create a new ASP.NET Application and add a DIV tag with an id “myMap” to hold the map and a Literal control to output script at runtime
1
2
3
4
5
6
7
8
9
10
<body onload="GetMap();">
     <form id="form1" runat="server">
          <div id="myMap"
               style="position:relative; width:600px; height:600px;">
          </div>
 
          <asp:Literal ID="Literal1" runat="server">
          </asp:Literal>
     </form>
</body>
Step:3 Write a method in the code behind file to generate pushpin objects. We have also added event handler to the pushpins to zoom map after clicking on the pushpins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private string GetLocations()
{
     string Locations = "";
     using (SqlConnection con =
     new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
     {
          SqlCommand cmd = new SqlCommand("SELECT Latitude, Longitude FROM Locations", con);
          con.Open();
          SqlDataReader reader = cmd.ExecuteReader();
          while (reader.Read())
          {
               Locations += "var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location("+
                   reader["Latitude"].ToString()+", "+reader["Longitude"].ToString()+
                   "), null);Microsoft.Maps.Events.addHandler(pushpin, 'mouseup', ZoomIn);map.entities.push(pushpin);";
          }
     }
     return Locations;
}
It will generate pushpin objects for all the rows in the table like following:
var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(26.273714, 84.902344), null);
Microsoft.Maps.Events.addHandler(pushpin, 'mouseup', ZoomIn);map.entities.push(pushpin);
var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(19.890723, 84.624022), null);
Microsoft.Maps.Events.addHandler(pushpin, 'mouseup', ZoomIn);map.entities.push(pushpin);
Step:4 Write following in the page load event. It adds pushpins to the script by appending pushpin objects returned using GetLocations method. GetMap function is called from BODY onLoad event. Another function ZoomIn is called after clicking on the pushpin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
protected void Page_Load(object sender, EventArgs e)
{
     string Locations = GetLocations();
     Literal1.Text= @"
          <script type='text/javascript' src='http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0'>
          </script>
          <script type='text/javascript'>
               var  map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
                          credentials: 'Your Bing Map Key'
               });
 
               function GetMap() {
                    map.entities.clear();
                    " + Locations+ @"
 
                    function ZoomIn(e){
                         if (e.targetType == 'pushpin'){
                               var location = e.target.getLocation();
                               map.setView({
                                    zoom:5,
                                    center: location
                               });
                   }
              }
       }
</script>";
}

4 comments:

  1. Can you help me with another as-well.

    I need to show a popup box while we pint at pushpins. The pop up box should display data which is in SQL..

    ReplyDelete
  2. Integrating Pushpins into a Bing Map from a database using ASP.NET is a practical and efficient way to enhance user experience. Similarly, understanding the benefits of good food on your health empowers individuals to make informed choices. Both actions require thoughtful planning and execution, emphasizing the importance of a structured approach. Just as precision matters in coding, a well-balanced diet is crucial for overall health and vitality

    ReplyDelete

  3. Mastering ASP.NET for Bing Maps integration is a game-changer! As you navigate coding complexities, remember to balance screen time. Incorporate breaks, and for a natural boost, explore methods like activated charcoal or oil pulling to Remove Black Stains From Teeth Naturally. A healthy smile and a well-coded map make a winning combination for web development success!

    ReplyDelete