Problem in Muliple PushPins

Hi,

I have problem in drawing multiple pushpins,

The problem is

I have DataView on the aspx page which contains address,

I have to put push pin on the map for each address in the DataView.

the following is the code to read all address in DataView and drawing pushpins for each address using javascript

function FindLoc()

{

var Address, i=0;

var gvAddress=document.getElementById('grvLoans');

for (var i=1;i<gvAddress.rows.length;i++)

{

var rowElem = gvAddress.rowsIdea;

var status = rowElem.cells[4].innerHTML;

// Show pushpin if status is not empty

if (status !='')

{

add= rowElem.cells[3].innerHTML +", " + rowElem.cells[1].innerHTML +", " + rowElem.cells[2].innerHTML +" " + rowElem.cells[0].innerHTML;

showAddress(add, status)

}

}

returnfalse;

}

function showAddress(address, status)

{

try

{

var image;

image ='Images/pushpin_red.gif';

AddPin(image ,address, address,'');

}

catch(err)

{

window.alert("Unable to process the location");

}

}

function AddPin(image, where, _Title, _Description)

{

if(map.GetMapStyle() == VEMapStyle.Birdseye)

returnfalse;

icon = image

Title = _Title;

Description = _Description;

map.Find(null, where,null, layer,0,20,true,false,true,false,onFindResults);

}

function onFindResults(a,b,c,d,e)

{

alert(pinID);

var LatLon =new VELatLong(c[0].LatLong.Latitude, c[0].LatLong.Longitude);

var shape =new VEShape(VEShapeType.Pushpin, LatLon);

shape.SetTitle(Title);

shape.SetDescription(Description);

shape.SetCustomIcon(icon);

map.AddShape(shape);

pinID++;

map.PanToLatLong(LatLon);

}

The above code is to draw multiple pushpins but it is not working please check the code if there is any error.

it is drawing only one pushpin.

Thanks

DVR Prasad

[10021 byte] By [DVRPrasad] at [2008-1-4]
# 1

I believe you can only have one "Find" query running at once. So, when you call it in your loop in FindLoc, the 2nd Find overwrites the 1st Find, and the 3rd one overwrites the 2nd one, so the only one which actually completes is the last one.

To work around this:

create a global var "index"

make gvAddress global

in FindLoc, only call ShowAddress for the first pin. Set index to 0.

then, in onFindResults, increment "index". If (index < gvAddress.rows.length), have it call showAddress for gvAddress.rows[index].

This seems to work fine for me. (Thanks to several folks at work for providing this code).

The one thing which is sortof annoying about this is that it takes a little while for each pin to appear, so if your DataGridView is big, it can take a few seconds (or half minute) for all of the pins to finish loading. I don't know if there's a better solution.

I'm not much of a web programmer, but I suspect it's possible to save off the lat/lon coordinates of each address, so the second time you load the page, you would just directly add the pins instead of Find()ing them.

JasonGould at 2007-10-3 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 2

Hi Jason,

I didnt get exactly what is the logic that you told.

Please explain me in detail

Thanks

DVR Prasad

DVRPrasad at 2007-10-3 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 3

Something like this should work:

var gIndex=0;

var gvAddress;

function FindLoc()

{

var Address;

gvAddress = document.getElementById('grvLoans');

gIndex = 0;

// just show the first address

// move the "show pushpin if status not empty" logic into showAddress

showAddress(gIndex);

return false;

}

function showAddress(gIndex)

{

var rowElem = gvAddress.rows[index];

var status = rowElem.cells[4].innerHTML;

// show pushpin if status not empty

if (status != '')

{

var address = rowElem.cells[3].innerHTML + ", " + rowElem.cells[1].innerHTML + ", " + rowElem.cells[2].innerHTML + " " + rowElem.cells[0].innerHTML;

try

{

var image;

image = Images/pushpin_red.gif';

AddPin(image, address, address, '');

}

catch(err)

{

window.alert("Unable to process the location");

}

}

}

function AddPin(image, where, _Title, _Description)

{

if(map.GetMapStyle() == VEMapStyle.Birdseye)

return false;

icon = image

Title = _Title;

Description = _Description;

map.Find(null, where, null, layer,0,20,true,false,true,false,onFindResults);

}

function onFindResults(a,b,c,d,e)

{

alert(pinID);

var LatLon = new (VELatLong(c[0].LatLong.Latitude, c[0].LatLong.Longitude);

var shape = new VEShape(VEShapeType.Pushpin, LatLon);

shape.SetTitle(Title);

shape.SetDescription(Description);

shape.SetCustomIcon(icon);

map.AddShape(shape);

pinID++;

map.PanToLatLong(LatLon);

// we just found one pin. Now, start the search for the next pin:

//

gIndex++;

if (gIndex < gvAddress.rows.length)

{

showAddress(gIndex);

}

}

If you call map.Find(null, myPlace, .... onFindResults), then immediately call map.Find(null, somewhereElse, ... onFindResults), I believe that the first "map.Find" may get lost. (Meaning, I think you can only have one "Find" running at once.) So, onFindResults will only get called for "somewhereElse"'s request, and never gets called for myPlace.

In my fixed code, FindLoc only requests one pushpin. Then, when that one has completed, onFindResults gets called. onFindResults then calls showAddress for the next address. When that one's finished, onFindResulsts gets called again, & it calls showAddress again.

Note: I'm not an expert on this, so my assumption about "Find" may be wrong. But I do know that I've seen multiple pushpins work before, using the algorithm mentioned above.

Good luck...

-Jason

JasonGould at 2007-10-3 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 4

Yes It is creating problem for me also. I have added the a long delay (loop) now it is working but slow Sad

Thanks

Virsharma at 2007-10-3 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...

Windows Live Developer Forums

Site Classified