joegeiger


I have been successful in getting the map to show up on my web page but I have not been able to set a default pushpin without the user clicking on a link that has an AddPin() function assigned to it. I have looked at the examples from the interactive SDK but they all call the AddPin function assigned to linked text to display the pushpin. All I want is to display the pushpin on the map by default when the user loads the page. I have also tried calling the AddPin function in the <body onload="GetMap(); AddPin();"> but this doesn't seem to work . And I tried calling it in the GetMap() function and this hasn't worked either.

I have looked through the forums and even consulted a friend who is fluent in programming but have found nothing on how to do this. I was able to get this to work using Google maps but the map info is incorrect so I can't use it on the page for directions.

Any suggestions




Re: Setting a default pushpin?

SoulSolutions


thats odd. What you say should work. I've tested this and it is fine.

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js"></script>
<script>
var map = null;
var pinID = 1;
function GetMap()
{
map =
new VEMap('myMap');
map.LoadMap();
AddPin();
}
function AddPin()
{
var pin = new VEPushpin(
pinID,
map.GetCenter(),
null,
'My pushpin',
'This is pushpin number '+pinID
);
map.AddPushpin(pin);
pinID++;
}
</script>
</head>
<body onload="GetMap();">
<div id='myMap' style="position:relative; width:400px; height:400px;"></div>
</body>
</
html>

John







Re: Setting a default pushpin?

joegeiger

I think I found the source of the problem... I am using Firefox 2.0. I copied your example into a test HTML doc and opened it in Firefox 2.0... no default pushpin showed up. I then opened the file in IE6 and... tada... the default pushpin appeared. I tried your example running Firefox 1.5 on my iMac at home and it worked there too (but not in Safari, which is no suprise). It must be something about the 2.0 version.

I'm not using the 3D features of VE in my page because I want it to run on as many browsers as possible and currently only IE6 & 7 support it.. I set it to run in 2D mode in the GetMap function.

I have one other question... Why do the pins seem to shift to the south the more you zoom out Is there a way to make it stick to a particular location and keep it there as the map is zoomed out

Thanks for your help John.

Regards,

Joe








Re: Setting a default pushpin?

SoulSolutions

Yes there is an issue with Firefox2 but one of the guys here made a simply hack that makes it all happy.

I've just started a VE code wiki that maybe of great help to you Joe.

Check out:

http://www.viavirtualearth.com/Wiki/Firefox2.ashx

oops nothing on the pins being centred...i'll add it now.

Basically the pins get aligned centred by default. You need to set a cssclass when you add them and then provide the offset for your pin. Check out the wiki later today and i'll have it up.

John.






Re: Setting a default pushpin?

XXLRG

Negative, all is not happy in FF 2.0!

When I attempt to set a push pin in the attached onload function the pin doesnĄ¯t show up.

IĄ¯ve noticed that the pin IS visible until the image tiles render then the pin goes away.





Re: Setting a default pushpin?

JGWingfield

I ran into this same problem and spent most of yesterday afternoon being driven nuts by it. The code above does work - usually. What I could not seem to do was set the map to a specific location and then add the pushpin. When using a fast internet connection and loading a default map, the pushpin will display as the code example above shows. However, if a web service process affecting the display of the map has not finished, the AddPushpin method does not seem to be available. In my case I am using FindLocation() to seed the map before calling LoadMap().

var map = null;

var pinID = 1;

function GetMap()

{

map = new VEMap('myMap');

map.FindLocation("3128 157th Avenue NE, Redmond, WA");

map.LoadMap();

AddDefaultPin();

}

Remove the call to FindLocation() and it works. Add it, and the map loads okay but the code in AddDefaultPin() fails because apparently the FindLocation() call is not quite finished.

So my inelegant 'hack' is to add an event handler that fires the AddDefaultPin() code once the previous webservice calls are complete. My final code is something like this:

var map = null;

var pinID = 1;

function GetMap()

{

map = new VEMap('myMap');

map.FindLocation("3128 157th Avenue NE, Redmond, WA");

map.LoadMap();

map.HideDashboard();

map.SetZoomLevel(19);

map.AttachEvent("onchangeview", AddDefaultPin);

}

function AddDefaultPin()

{

var pin = new VEPushpin (pinID, map.GetCenter(), null, "John's office", "Building 28");

map.AddPushpin(pin);

map.AttachEvent("onchangeview", null);

pinID++;

}

The 'onchangeevent' fires when the web service finishes setting the map view. The AddDefaultPin() code executes, adding the pushpin and then removing itself from being the event handler. The reason for the latter step is obvious is you omit the step and then make any changes to the map - like reposition or change the zoom level.

Things I don't know yet about this approach:

1. How effective is it with a slow connection

2. Could the onchangeview event fire before the map is fully loaded (There is not enough public documentation - that I could find - on the event model.)





Re: Setting a default pushpin?

JGWingfield

D'oh!

Might be better to use...

map.DetachEvent(event, function)





Re: Setting a default pushpin?

SoulSolutions

Ok there are callbacks that can be fired when things happen, like when the map is loaded you call the find, whne the find returns the result, you could add the pin. This is a better way to do it:

<html>
<head>
<title>VE Find on load Callback</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="
http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js"></script>
<script>
var map = null;
var pinID = 1;

function GetMap()
{
map = new VEMap('myMap');
map.onLoadMap = fnFind;
map.LoadMap();
}

function fnFind()
{
map.FindLocation("3128 157th Avenue NE, Redmond, WA", AddDefaultPin);
}

function AddDefaultPin()
{
var pin = new VEPushpin (pinID, map.GetCenter(), null, "John's office", "Building 28");
map.AddPushpin(pin);
pinID++;
}
</script>
</head>
<body onload="GetMap();">
<div id='myMap' style="position:relative; width:400px; height:400px;"></div>
</body>
</html>

You can also use the actual latlong value from the search rather then the map center, this should be more reliable, as there is little chance of the pin being added to the wrong position. Modify the function:

function AddDefaultPin(results)
{
var pin = new VEPushpin (pinID, results[0].LatLong, null, "John's office", "Building 28");
map.AddPushpin(pin);
pinID++;
}

John.






Re: Setting a default pushpin?

JGWingfield

Cool, John! Like your way lots better.

I haven't tried the LatLong yet but will do so next. I ended up using the FindLocation() prior to the LoadMap() was to avoid image flicker on the page. Laziness was my only excuse for not trying with a LatLong object.