--- In yws-maps@yahoogroups.com, "digitaldelisle" <luc@d...> wrote:
>
> I want to load data in an xml file and be able to create markers on
> the map accordingly
>
> The xml would contain the following fields
> Lat, long, description
>
> Can someone please point me to a Java script example of the code that
> would do this using the new BETA of Yahoo maps
There are two APIs that use Javascript. There is JS-Flash where
Javascript controls a Flash map component and there is the AJAX API
where Javascript controls a DHTML version of the map. You didn't
indicate which one you were interested in.
For the JS-Flash API it is really easy. It sounds like you have some
control over your XML file. If so, make it look like this:
<rss version="2.0">
<channel>
<item>
<title>Title for Entry 1</title>
<description>Description for Entry 1</description>
<link>http://example.com/some_link</link>
<geo:lat>51.8421</geo:lat>
<geo:long>-174.6053</geo:long>
</item>
... more items here ...
</channel>
</rss>
Generate that and stick it on your web server. Then in your JS code
do this:
overlay = new GeoRSSOverlay('http://your.server/file.xml');
mymap.addOverlay(overlay);
There is a full example here using the US Geological earthquake data:
http://lerdorf.com/php/ymap/rssquakes.php
If you want to use the JS-DHTML API, then it is a bit harder. The
easiest way is to throw a bit of PHP at the problem server-side. You
read the XML file server-side and then generate a new YMarker for each
item. You can also do it client-side in Javascript and parse the XML
there, but I am a server-side guy and find it easier to grasp from
that end. You still need some Javascript trickery to get the
smartwindow stuff working right. Here is a DHTML version of the
earthquake example:
http://www.lerdorf.com/php/ymap/dquakes.php
There is a source code link at the bottom there. The magic is in
these 4 lines of PHP-enhanced Javascript that are run for each item in
the XML file with $i being the incrementing index.
First, create a YMarker at the lat/long from the XML file.
marker[<?php echo $i?>] = new YMarker(new YGeoPoint(<?php echo
$feed[$i]['lat'][0].','.$feed[$i]['long'][0]?>));
Then add a label. We'll just use the index number. Would probably
look cooler to pick out the earthquake magnitude here.
marker[<?php echo $i?>].addLabel("<b><?php echo $i?></b>");
Now a trick. We'll use a dynamic function which bakes in the per-item
eathquake info which will get called on a click on each marker.
YEvent.Capture(marker[<?php echo $i?>], EventsList.MouseClick, new
Function("marker[<?php echo $i?>].openSmartWindow('<?php echo
$info?>');"));
And finally, we add the marker to the map.
mymap.addOverlay(marker[<?php echo $i?>]);
Not that hard, but a whole lot harder than just calling GeoRSSOverlay.
The one advantage with this latter approach is that you can handle
any XML file format. There is also an example of doing this manually
with the JS-Flash API here: http://www.lerdorf.com/php/ymap/yquakes.php
-Rasmus