tbrown10


I am trying to write a script that puts multiple pushpins on a page the problem I run into is that all the pushpins then have the same text as the last one placed any ideas

Code Snippet
<html>
<head>
<script src="http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js"></script>
<script>
var map = null;
var pinID = 1;
var title="";
var text="";
function GetMap()
{
map = new VEMap('myMap');
map.LoadMap();
title="Endicott Police Department";
map.FindLocation('1101 Park Street Endicott NY',onFindLocation);
title="Abuse";
map.FindLocation('100 West Valley Street Endicott Ny',onFindLocation);
}


function onFindLocation(res)
{
// always plot a marker at the lat/long of the first location
var text = "text";
//var title = "title";
pinID++; // increment the pinId to guarantee a unique id
var pin = new VEPushpin(pinID, res[0].LatLong, null, title, text);
map.AddPushpin(pin);
if(res.length > 1) gDisambiguationInProcess = true;
}
/* function onFindResults(findResults)
{
var results="Find Results:\n";
for (r=0; r < findResults.length; r++)
{
results += findResults[r].Name + ", ";
results += findResults[r].Description + ": ";
results += findResults[r].Phone + "\n";
}
alert(results);
}*/
</script>
</head>
<body onLoad="GetMap();">
<div id='myMap' style="position:relative; width:400px; height:400px;"></div>
</body>
</html>




Re: Pushpins all show the same text

dhoenig


So you are saying that both of your push pins have the same title = "Abuse"






Re: Pushpins all show the same text

tbrown10

Yes I have tried many different things to fix this I even created multiple OnFindLocation functions with each being called for a different push pin and they still have the same title





Re: Pushpins all show the same text

tbrown10

Any help would be greatly appreciated




Re: Pushpins all show the same text

dhoenig

I think I know why this is happening...hopefully. The following code is causing the problem and here's why.

When you make an AJAX call to the server using map.FindLocation your code continues after the start of that call. So your title is set first to "Endicott Police Department" and then it makes the call to find the location of it. BUT, while your code is making the AJAX call to the server the javascript code continues on to the next line, which in this case resets your title variable to "Abuse". So when the onFindLocation function gets called from the first call, your title has already changed.

Like I said, I think this is whats happening. An easy way to test this is to create two variables, title1 and title2. If it works correctly when you use two separate variables you know that this is the cause of your problem.

title="Endicott Police Department";
map.FindLocation('1101 Park Street Endicott NY',onFindLocation);
title="Abuse";
map.FindLocation('100 West Valley Street Endicott Ny',onFindLocation);
Code Snippet






Re: Pushpins all show the same text

Jared H

I agree...the optional callback function is asynchronous, and the browser is falling through to the variable value setting (title = 'abuse'Wink before it is returning...so that the browser has already set the variable well before the callback is done.



Re: Pushpins all show the same text

tbrown10

Your comment above does make sense but I tried that and it still does the same thing I even made it in to 2 different onfindlocation functions and it still does it





Re: Pushpins all show the same text

dhoenig

Could you post the new code with both onFindLocation functions





Re: Pushpins all show the same text

tbrown10

Code Snippet

<html>
<head>
<script src="http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js"></script>
<script>
var map = null;
var pinID = 1;
var title="";
var text="";
var text1="";
var text6="";
function GetMap()
{
map = new VEMap('myMap');
map.LoadMap();
title1="Endicott Police Department";
map.FindLocation('1101 Park Street Endicott NY',onFindLocation1);
title6="Abuse";
map.FindLocation('100 West Valley Street Endicott Ny',onFindLocation6);
}

function onFindLocation1(res)
{
// always plot a marker at the lat/long of the first location
var text = "text";
//var title = "title";
pinID++; // increment the pinId to guarantee a unique id
var pin = new VEPushpin(pinID, res[0].LatLong, null, title1, text);
map.AddPushpin(pin);
// if just 1 location was returned, we're done, but if multiples
// were returned, we may have to plot a different marker once the
// disambiguation has completed... but we have to do it via a mapChange
// event because this method won't be called again
if(res.length > 1) gDisambiguationInProcess = true;
}
function onFindLocation6(res)
{
// always plot a marker at the lat/long of the first location
var text = "text";
//var title = "title";
pinID++; // increment the pinId to guarantee a unique id
var pin = new VEPushpin(pinID, res[0].LatLong, null, title6, text);
map.AddPushpin(pin);
// if just 1 location was returned, we're done, but if multiples
// were returned, we may have to plot a different marker once the
// disambiguation has completed... but we have to do it via a mapChange
// event because this method won't be called again
if(res.length > 1) gDisambiguationInProcess = true;
}
</script>
</head>
<body onLoad="GetMap();">
<div id='myMap' style="position:relative; width:400px; height:400px;"></div>
</body>
</html>






Re: Pushpins all show the same text

Jared H

This is because of the asynchronous processing; what you need to do is see if an array of FindResult classes are returned and then set your titles from within looping over the array. Otherwise that title variable is always going to have the value you set while the callback function is waiting to be called.



Re: Pushpins all show the same text

tbrown10

I am not exactly sure I understand do you have an example





Re: Pushpins all show the same text

dhoenig

Try this...

Code Snippet

<html>
<head>
<script src="http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js"></script>
<script>
var map = null;
var pinID = 0;
var pin = "";
var title="";
var titleArray = ['Endicott Police Department','Abuse'];
function GetMap()
{
//debugger;
map = new VEMap('myMap');
map.LoadMap();
var array = ['1101 Park Street Endicott NY','100 West Valley Street Endicott Ny'];
for(var i=0;i<2;i++)
{
map.FindLocation(array[i],onFindLocation1);
}
}

function onFindLocation1(res)
{
//debugger;
title = titleArray[pinID];
// always plot a marker at the lat/long of the first location
var text = "text";
pinID++; // increment the pinId to guarantee a unique id
pin = new VEPushpin(pinID, res[0].LatLong, null, title, text);
map.AddPushpin(pin);
// if just 1 location was returned, we're done, but if multiples
// were returned, we may have to plot a different marker once the
// disambiguation has completed... but we have to do it via a mapChange
// event because this method won't be called again
if(res.length > 1) gDisambiguationInProcess = true;
}
</script>
</head>
<body onLoad="GetMap();">
<div id='myMap' style="position:relative; width:400px; height:400px;"></div>
</body>
</html>








Re: Pushpins all show the same text

tbrown10

That worked thanks