Hi guys,
Just started playing with the API. I am trying to add a pushpin to general points e.g. Cities, Provinces, Postal Codes but everytime I try to the pushpin appears in a totally different point.
Any insight would be much appreciated.
Thanks
K![]()
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"></script>
<script>
var map = null;
var pinID = 1;
function GetMap()
{
map = new VEMap('myMap');
map.LoadMap();
FindTO();
AddPin();
}
function FindTO()
{
map.FindLocation('Toronto (city), Ontario (province), Canada (country)')
}
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:centre; width:80%; height:60%;"></div>
</body>
</html>

How to Add PushPin using FindLocation (without providing lat and long)
dakerson
RakkaRage
I'm trying to set up a system wherby the map will display all the people selling our stuff around a given postcode. We have an accessible address book which stores postcodes, so the pins can only be placed by postcode.
There needs to be a simple Location to Coordinate function, it would make life much easier.
tjanuario
Good stuff Caleb, thanks! No hectic behaviour of markers and works beautifully in FF 1.5 as well. Everything is easy once you know the answer...
I may take this opportunity and ask one more question. Is it possible to set a scale bar to km I'm sure I've seen it done somewhere but now I can't find it ...
RhysDavies
Yeah, we seemed to have drifted on a tangent there...so here are 2 thoughts on how to handle this:
1. Ideally, of course, the Find() and FindLocation() methods would fire a callback, and you could just put your AddPushpin code in the callback. Until that is fixed, you could do this nifty hack (thanks, Steve). Basically, use a global flag to indicate whether a search is being done, and use that in the onchangeview event handler to determine whether to draw the pushpin.
<html>
<head>
<title>Geocoding Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"></script>
<script>
var map = null;
var doingsearch = false;
var pinid=0;
function OnPageLoad()
{
map = new VEMap('mymapcontroldiv');
map.LoadMap();
map.AttachEvent("onchangeview", function(e) {
if (doingsearch==true){
var ll = map.GetCenter();
var pin = new VEPushpin(pinid, ll,
'http://www.myfavoriteplaceonearth.com/images/target.gif','Center of map','');
map.AddPushpin(pin);
pinid++;
doingsearch=false;
document.getElementById('resultarea').innerHTML = " Geocode Result: " + ll.toString();
}
});
}
function SearchClick() {
var whereterm = document.getElementById('txtFind').value;
doingsearch = true;
map.Find('', whereterm,1);
}
</script>
</head>
<body onLoad="OnPageLoad()">
<div>Geocoding Demo<br><br></div>
<table>
<tr>
<td>
<div id="searchfields">
address to find<br>
<INPUT id="txtFind" type="text" name="Find" size="40"> <INPUT id="btnSearch" type="button"
value="Find" name="Search" onclick="return SearchClick()">
</div>
</td>
<td>
<div id="resultarea"></div>
</td>
</tr>
</table>
<table>
<tr valign="top">
<td>
<div id="mymapcontroldiv" STYLE="HEIGHT: 600px; WIDTH: 800px; OVERFLOW: hidden; POSITION:relative;
border: 1px solid;">
</div>
</td>
<td>
<div id="searchresults"></div>
</td>
</tr>
</table>
</body>
</html>
2. You could do an AJAXy call to one of the geocoders (MWS, Yahoo, etc), and wait for the response, and then call AddPushpin().
Hope that helps,
Caleb
Robert L. Stinnett
Have a look at the example I provide in my article here http://www.viavirtualearth.com/VVE/Articles/GettingStartedWithV3Pt2.ashx
Look in the Finding section of the article.
solnt
I tried this but get an "Exception thrown and not caught" error when I try to do this with more then one pin. My app will be passing several addresses to the find function and need to add those points to the screen. The center of the map idea will not work for me so I'm not sure what to do The other find function that VE offers requires a "What" which I don't plan on passing, just an address. That one returns the cords I need for the pins.
SucceedEgg
You're right there--VE does not provide a geocoding function that doesn't also automatically change the map view. As with similar threads on this fourm, one answer is: use the MapPoint Web Service. Solid geocoding (including reverse geocoding and batch jobs) is one of the reasons people pay for MWS. I've seen other posts whcih suggest using other (free I'm not sure) services that provide geocoding.
Anyway, using MWS or another service, you could do a batch geocoding, save the results in a GeoRSS file, and open the whole thing with one call to AddLayer(). That's likely your best bet.
Cheers,
Caleb
FC-Shiro
What appeard to be a "trivial" task turned into a nightmare for me... I am struggling with putting even a single point on the map. My search of the internet returned nothing. Did anyone manage to solve the puzzle of how to put a marker on-map-load AFTER geocoding address
This is a bit of code that appears to work but... behaves strangly when using slider zoom in/out control (ie marker gets repositioned "randomly") and I cannot get it working in FF (should it Dr Neil's example doesn't work either...):
var map = null;
var pinID = 1;
var where = "8 this street,suburb,state,country";
function OnPageLoad()
{
map = new VEMap('myMap');
map.LoadMap(new VELatLong( some_latitude, some_longitude), 10 ,'r' ,false); //initial map to be loaded
map.FindLocation(where); //finds location and zooms and centers the map
map.AttachEvent('onchangeview', onChangeView);
function onChangeView(e)
{
var pin = new VEPushpin(pinID,
map.GetCenter(), // new center of the map on 'where'
null,
'Title',
'description goes here'
);
map.AddPushpin(pin);
}
}
Alternative approach would be to "reuse" LatLong of new 'where' point to draw the marker but I'm at a loss to figure it out how...
Any pointers much appreciated!
"Rusty"
Santor
It has a 5000 query limit per 24 hours.
jklegseth
jasinb
I too am very interested to see if such a script exists.
Passing city names/states into a function that returns the lat and lon for pushpin creation would be a very useful thing. I'm a newbie so maybe it exists somewhere already. If so please tell us where.
All I've been able to find is an elaborite and impressive bit of coding at http://www.xml.com/pub/a/2006/03/01/seattle-movie-finder-AJAX-REST-virtual-earth-mashup.html
omar_rapid
Thanks for the feedback, getting to understand better.
Then I guess, progressing from your argument, what would be the best way to add a pushpin. I thought it would be using the a VELatLong object but what if I wanted to get a pushpin with just the name of the city how would I convert or find the equivalent latitude and longitude.
I tried find, findresults nothing worked.
Any ideas.
Kevin Dente
Sorry I took so long to reply serious beef in the office so many knifes in my back its not even funny.
I tried the suggestions too,
But it still didn't work, there must be a more elegant way. If I had 2000 locations to map, that would translate to the map moving 2000 times too. The user must wait for all of this to start using the map.
Maybe there are futher improvements on this in coming revisions of the API but currently man, I am stuck
Ganesh.P.A
Hi,
The problem here is that you are setting the pushpin to the center of the current map, and this is happening before the map has moved & zoomed to your location.
Your FindTO function simply calls a VE function that does the actual moving of the map.
FindTO returns back to your GetMap function as soon as it has called teh VE FindLocation function.
Your AddPin function is then called before the map has had a chance to move and zoom, and so the pushpin is in the wrong place.
This could also happen if someone moves the map during your AddPin function, so (in my opinion) is a very unreliable way to add a pushpin.