Click here to Skip to main content
13,252,362 members (66,952 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 16 Feb 2010

Collect data using Virtual Earth Info-Box

, 16 Feb 2010
Rate this:
Please Sign up or sign in to vote.
Location based infomation collection method using Virtual Earth.



This article describes how to collect information from users based on Virtual Earth. On the map, a push pin is displayed, and when the user hovers the mouse over the push pin, an Info-Box is displayed, which contains a controller that's needed to collect user information. The user can enter information related to that location and press the Save button to save the information and move on. In this article, I am using Virtual Earth API, JavaScript, AJAX controls, and HTML to show how to do this.

Using the code

Load a basic map

First, I will describe how to load a Virtual Earth map to an AJAX enabled ASP page (.aspx). Before we go further, let's load a basic map to our web page. These are the things we should do in order to load a Virtual Earth map on an AJAX enabled ASP page:


Within the body tag of the page, we should call the JavaScript function LoadMap():

<body onload="LoadMap()">

Within the form tag, the ScriptManager AJAX controller should be implemented:

<form id="form1" runat="server"> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 

Path="" /> 

And finally, on the HTML document, a div tag should be included, which will be used to display the map. The size of the map displayed is specified in the style tag.

<div style="position:relative;width:808px;
      height:680px;left:10px;top:10px;" id="mapDiv"></div>


In the code-behind page_load() event, I register the script required to run the LoadMap() function called from the HTML code. I can include this JavaScript in the HTML page, but I am using this in the code-behind because that will make further work on it easy to do. Here, I have used a StringBuilder class to add the JavaScript, and ClientScript.RegisterStartupScript to register the script as a start-up script.

protected void Page_Load(object sender, EventArgs e) 
    System.Text.StringBuilder mapScript = 
                new System.Text.StringBuilder(); 
    mapScript.Append("function LoadMap()"); 
    mapScript.Append("myMap = new VEMap('mapDiv');"); 
    mapScript.Append("myMap.SetCenter(new " + 
    Type t = this.GetType(); 
    if (!ClientScript.IsStartupScriptRegistered(t, "MapScript")) 
        ClientScript.RegisterStartupScript(t, "MapScript", 

Now, the page should display a map like this:


Adding pushpins

After successfully displaying a map on the ASP page, we are now going to add a pushpin to the map. Here, I am adding only one push pin, but later on, you can add more pushpins as you like.

//Defining a array to hold shapes
mapScript.Append("var shapes = new Array();");

//shape location
mapScript.Append("var vePoint1 = " + 
   "new VELatLong(-38.374238,145.201842);");

//Point details (title,desc,shape type)
mapScript.Append("var shape1 = " + 
   "new VEShape(VEShapeType.Pushpin,vePoint1);");
mapScript.Append("shapes.push(shape1); ");

// Adding shapes to shape layer and to map 
mapScript.Append("var shapeLayer = new VEShapeLayer();"); 

Setting up the form

After adding a pushpin to the map, the next step is to add controllers to the push pins description section. Here, I am using the shapes description section to add the controls.

mapScript.Append("shape1.SetDescription(\"" + getInfoBox() + "\");");

getInfoBox() is a C# function which generates the HTML code for the controls that we wish to include. I have included a dropdown menu, a text box, and a Save button.

private string getInfoBox() 
    string strInfoBox = ""; 
    strInfoBox = "<FORM NAME='myform'><p>Description :
                  <input type=text id='descbox' value='' />"; 
    strInfoBox += "</br>Type :<br/> <select style='width:155px;' name='type'>"; 
    strInfoBox += "<option selected='yes' value='B'>Bus Station</option>"; 
    strInfoBox += "<option value='C'>Cinema</option>"; 
    strInfoBox += "<option value='S'>Shopping Mall</option>";
    strInfoBox += "</select><br/>"; 
    strInfoBox += "</br><input type='button' value='Save' id='btnSend' 
                   onclick='SaveValues(this.form)' />"; 
    strInfoBox += "</br><input type='hidden' id='pointID' value='1'/>"; 
    strInfoBox += "</p></FORM>"; 
    return strInfoBox; 

Finally, when the user hovers the mouse over the pushpin, the result would be:


Saving information

In the shapes description section, I have included a button named Save to save the user selection. In the onclick event of the Save button, I call the SaveValues() JavaScript function. This is in the head section of the page's HTML code.

<script type="text/javascript"> 
    function SaveValues(form) 
        _pointID = parseFloat(form.pointID.value); 
        _description = form.descbox.value; 
        _type = form.type.value; 
        var tboxPointID = document.getElementById('txtPointID'); 
        if (tboxPointID) 
            tboxPointID.value = _pointID; 
        var tboxDesc = document.getElementById('txtDesc'); 
        if (tboxDesc) 
            tboxDesc.value = _description; 
        var tboxType = document.getElementById('txtType'); 
        if (tboxType) 
            tboxType.value = _type; 
        var btn = document.getElementById('btnSave');;

In the SaveValues function, I save the user selection into JavaScript variables, and then set those values into hidden textboxes. You should include the text boxes below into the HTML document, under the body section:

<input id="txtPointID" runat="server" type="hidden" /> 
<input id="txtDesc" runat="server" type="hidden" /> 
<input id="txtType" runat="server" type="hidden" />

Then, I raise the Click event of the btnSave button in the ASPX page. btnSave button is a server-side button, and its height and width are set to zero in order to make it invisible.

<asp:Button ID="btnSave" runat="server" 

  Style="z-index: 100; position: relative; height: 0px; width: 0px;" 


  OnClick="btnSavePoint_Click" /> 

At the button's onClick() event, we access the data and save it to a database or any other media. I am not going to describe how to so that here, as it is out of the scope of this article.

protected void btnSavePoint_Click(object sender, EventArgs e) 
  string PointID = txtPointID.Value; 
  string Type = txtType.Value; 
  string Description = txtDesc.Value; 

  //Save into database 


Points of interest

The width and height of the btnSave button is set to zero in order to make it invisible; in IE, this won't work as expected, but Firefox gives a good result.


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


About the Author

Rukshan Samanthilaka
Software Developer
Sri Lanka Sri Lanka
I came to the the software industry in 2006 now i am familiar with C#, ASP.NET, SQL Server, JavaScript,Google Maps and Virtual Earth API and looking forward for a long way ahead.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 16 Feb 2010
Article Copyright 2010 by Rukshan Samanthilaka
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid