FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Plot Multiple Points on Your Own Map





mannrak2003
Use the Yahoo! Maps Web Service to plot several locations on a map at once.
An online mapping service such as Yahoo! Maps (http://maps.yahoo.com)is a vast improvement over the multifold paper maps that are stashed in your glove compartment. Enter your starting address and an end address at Yahoo! Maps, and you'll get a detailed map, along with directions that include every turn you need to make to reach your destination. Finding a single address on a map is easy, but plotting several unique points on an interactive map used to be a task that only map service providers could accomplish.
Yahoo! has opened this ability to anyone with a bit of scripting knowledge, and you can plot dozens of points on a map instantly with the Yahoo! Maps Web Service (http://developer.yahoo.net/maps). The key to plotting several points is building a Yahoo! Maps Annotation file: an XML file with information about the points you want to plot on your map. If you're familiar with the syndication format RSS, you're most of the way there. The Yahoo! Maps Annotation file uses the RSS 2.0 format with some proprietary extensions that describe locations. This hack shows how to put together an RSS file that Yahoo! Maps will understand, and it explains how that file plots points on a Yahoo! Map.
5.16.1. Assembling Your Locations
Imagine it's Saturday morning, and you've decided to spend the day going to garage sales in your area. You open the paper, find a list of garage sales in the classified ads, and want to choose the most efficient route for visiting the sales. Unless the paper provides a list of sales on a map, it's difficult to visualize where each address is located. This is where the Yahoo! Maps Web Service can help.
To start building your map, you need to have your location data in a digital format. A spreadsheet works well for data entry, so open Excel and add the addresses you want to plot on a map. This hack expects the data in five columns, in this order: address, city, state, Zip Code, and notes about the address. Now you can copy the garage sale addresses from the paper and include notes about what you're most interested in at that particular sale.
Once you've added all the addresses you want to plot, save the file in the plain text comma-separated value (CSV) format and call the file garage_sales.csv. Now that your location data is in a digital format, you can easily convert the CSV file into the Yahoo! Maps Annotation format with some Perl.
5.16.2. The Code
This script accepts a filename for a CSV file and uses the data in the file to create a geo-aware RSS 2.0 file. You won't need any extra modules, so you can simply save the following code to a file called csv_to_geoRSS.pl:
#!/usr/bin/perl
# csv_to_geoRSS.pl
# Converts a CSV file with addresses to geo-aware RSS
# Usage: csv_to_geoRSS.pl <CSV File>

use strict;

# Open the incoming file
open(CSV, "<@ARGV" ) or die "Can't open @ARGV : $!";

# Change the .csv extension to .xml for RSS file
my $RSSfile = $ARGV[0];
$RSSfile =~ s!csv$!xml!;

# Open the RSS file for writing
open(RSS, ">$RSSfile") or die "Can't open file: $!";

print RSS<<"HEADER_END";
<?xml version="1.0"?>
<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
xmlns:ymaps="http://api.maps.yahoo.com/Maps/V1/AnnotatedMaps.xsd">
<channel>
HEADER_END

# Loop through the CSV file, adding items
while(<CSV>) {
chomp;
my($address, $city, $state, $zip, $notes) = split(/,/, $_);

print RSS<<"ITEM_END";
<item>
<title>$address</title>
<link />
<description>$notes</description>
<ymaps:Address>$address</ymaps:Address>
<ymaps:CityState>$city, $state</ymaps:CityState>
<ymaps:Zip>$zip</ymaps:Zip>
<ymaps:Country>us</ymaps:Country>
</item>
ITEM_END
}
# Finish the RSS
print RSS "</channel>\n";
print RSS "</rss>";

# Close the files
close CSV;
close RSS;

As you look through the script, you can see that the RSS format contains the standard <title>, <link>, and <description> tags. It also has some Yahoo!-specific extension tags including <ymaps:Address>, <ymaps:CityState>, <ymaps:Zip>, and <ymaps:Country>. Because the garage sales in this example don't have titles or links, the address is used as a title and the link tags are blank in the final RSS file. You could easily add more columns to the spreadsheet to hold titles or links.
Also keep in mind that if your source spreadsheet file data contains commas (in the address or note columns, for example), you may need to adjust the script to accommodate. Try saving the spreadsheet as a tab-delimited file and changing the line in the code that separates data by commas so that it instead separates data by tabs:
my($address, $city, $state, $zip, $notes) = split(/\t/, $_);

This measure should help ensure your data lines up properly in the RSS file.
5.16.3. Running the Hack
To create the RSS file, pass the name of your CSV file to the script, like so:
perl csv_to_geoRSS.pl garage_sales.csv

The script will create a file called garage_sales.xml that contains the properly formatted RSS for plotting points on a Yahoo! Map. Upload the RSS file to a publicly available web server and note the URL. The RSS file needs to be available online so Yahoo! Maps can use the data.
5.16.4. Building the Map
Once your RSS file is in place, building your map is simply a matter of constructing the proper Yahoo! Maps URL. You'll need a Yahoo! Application ID, which you can pick up at http://api.search.yahoo.com/webservices/register_application. With your Application ID and RSS URL, link to your custom map like this:
http://api.maps.yahoo.com/Maps/V1/AnnotatedMaps?appid=insert App


ID&xmlsrc=insert RSS URL

For this example, the URL would be:
http://api.maps.yahoo.com/Maps/V1/AnnotatedMaps?appid=insert App


ID&xmlsrc=http://example.com/garage_sales.xml

When you browse to the URL, Yahoo! fetches the RSS file and plots the addresses on a map

[/img]
n0obie4life
Copy + Paste.
No quote tags.
No credit.

If you think I'm wrong in anyway, please inform me.

-close-
Related topics
Trackmania Sunrise
HALO Requires no skill
Please explain CS
travelling at/near the speed of light?
Man, am I a loser!
Religion: The Root of All Evil?
MICROSOFT SURFACE - Awaiting your comments
Windows Live Messenger 9.0 Beta
Have you read the Bible?
Like Drawing? Try This Game !!!
Flash Drawing Game, Almost Like Line Rider !!!
Landwirtschafts Simulator 2009
Ding dong the witch is no more
Trackmania˛ Stadium
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.