yeodanni


hi,

The code below allow me to load only a single pushpin into the map but I need to load all pushpin to the map.How can I do it

server code:

Private Sub DisplayRecord()

'Display record based on User ID

Try

'retrieve data from database and place in textbox

cnn.Open()

Dim mySelectQuery As String = "SELECT * from Map"

Dim cmd As SqlCommand = New SqlCommand(mySelectQuery)

cmd.Connection = cnn

Try

cmd.ExecuteNonQuery()

cmd.Parameters.AddWithValue("Lat", txtLat.Text)

cmd.Parameters.AddWithValue("Long", txtLong.Text)

cmd.Parameters.AddWithValue("Level", txtLevel.Text)

cmd.Parameters.AddWithValue("Name", txtName.Text)

Dim dtr As SqlDataReader = cmd.ExecuteReader()

Do While dtr.Read()

txtLat.Text = dtr("Latitude")

txtLong.Text = dtr("Longtitude")

txtLevel.Text = dtr("ZoomLevel")

txtName.Text = dtr("Title")

Loop

Finally

cmd.Connection.Close()

End Try

Catch ex As Exception

Throw New Exception(ex.Message)

End Try

End Sub

Client Code :

function GetMap()

{

map = new VEMap('myMap');

map.LoadMap(new VELatLong(36.80928470205939, 97.998046875), 4 ,'r' ,false);

// map.AttachEvent('onclick', onMapClick);

map.AttachEvent('onclick', myEventTester); //change to your event

map.AttachEvent("onchangeview", GetPinData);

DoBestMap(locs);

}

function GetPinData()

{

var pin_id=0;

var locs = new Array;

var loc = new VELatLong(document.getElementById('txtLat').value, document.getElementById('txtLong').value);

locs.push(loc);

for (i=0; i < locs.length; i++)

{

var pin1 = new VEPushpin(pin_id, locsIdea, null, 'A point', 'location number '+ i);

map.AddPushpin(pin1);

pin_id++;

}

}

function DoBestMap(locs)

{

map.SetMapView(locs);

}




Re: How to do a while loop to load pushpin into map

wlogarbo


Hello,

I researched this issue over the weekend. It's not a simple solution, but I learned a ton. In a nutshell, I used Ajax to call a web service through the Javascript that allowed me to pass in all the data for all the pushpins in my database. I aso created an admin page to add new pushpins at anytime. The actual map is a usercontrol that I can just drop on any page in my project. The references i used were the Virtual Earth SDK, the Ajax site on asp.net, and various articles on using Ajax to call a web service.

The code isn't pretty in some parts, and I'm still in developement, but here goes:

My user control:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="mapWorld.ascx.vb" Inherits="User_Controls_mapWorld" %>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<script src="http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js" type="text/javascript"></script>

<script type="text/javascript" src="../Scripts/map.js">

</script>

</head>

<body onload="LoadMap();">

<asp:ScriptManager ID="ScriptManager1" runat="server">

<Services>

<asp:ServiceReference Path="~/WebServices/service.asmx" />

</Services>

</asp:ScriptManager>

<div id='myMap' style="position:relative; width:600px; height:600px;"></div>

</body>

external .js file:

var map = null;

var _Pins = [];

function LoadMap()

{

GetMap();

World.service.GetPins(LoadPins, onError);

}

function GetMap()

{

map = new VEMap('myMap');

map.LoadMap(new VELatLong(30.265137, -90.920085), 10 ,'h' , false);

//map.HideDashboard();

//AddMyControl();

}

function LoadPins(results)

{

if (results != null && results != 'undefined') {

//document.write(results.Pins);

_Pins = results.Pins;

AddPins();

}

if (results = null ) {

document.write("No Results");

}

}

function AddPins()

{

//document.write("Debug");

for (var i=0;i<_Pins.length;i++) {

var pin = new VEPushpin(

_PinsIdea.ID,

new VELatLong( _PinsIdea.Lat, _PinsIdea.Lon),

_PinsIdea.IconURL,

_PinsIdea.Title,

_PinsIdea.Details

);

map.AddPushpin(pin);

}

}

function onError(results)

{

document.write("Error");

}

As you can see I call the webservice World.service.GetPins(LoadPins, onError); where the namespace is World, the web service name is service and the function is GetPins. This calls to a PinManger file that gets the pins from the database. I did use Dan Wahlin's weblog alot, as I found it very useful http://weblogs.asp.net/dwahlin/. Remeber that when you create the webservice, you have to add in scripting services so that the Javascript can use it.

After I was done with that phase of the project, I looked back to see that is really a great solution. Just had to battle through learning all of it. I had very limited knowledge of web services and Ajax before, but alot more now. If you have any further issues or questions, you can post here or contact me directly. Posting here would be better, as it will help others.

Hope this helps.







Re: How to do a while loop to load pushpin into map

yeodanni

Hi,

Other than web service is there any way to do it Can I populate the data into a datagrid and then read from there Do anyone know how to retrieve data from datagrid document.getElementById('txtLat').value is to get textbox value,so how to get datagid value






Re: How to do a while loop to load pushpin into map

Derek Chan

What your looking to do is throw all your data inside a <div> tag, maybe separated by an identifier (eg: comma), and then parse out all the data inside that tag. It does however, have the drawback of the data easily being viewed through the source (which you would experience reading from generated datagrids anyways).

However if you really need it inside datagrids, your going to need to encase the text inside an html element with an associated id, such as a <div> tag which again really defeats the purpose of using datagrids (hence what I suggest above)

Hope that helps,




Re: How to do a while loop to load pushpin into map

Jared H

For purposes of displaying collections, I'd make your database call and format the output as a GeoRSS file and then add the pins as a single layer rather than looping over an array of pins and calling the individual add pushpin method each time. This would definitely be quicker for larger datasets and reduce the processing overhead on your clients' browser(s).