Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Map It!

by Mr. Muskrat (Abbot)
on Aug 26, 2002 at 17:05 UTC ( #192950=CUFP: print w/ replies, xml ) Need Help??

Update! Do not use this for commercial purposes or you will be violating the MapBlast Terms of Use.

Several people wanted an easier way to get maps to some places. So with a little help from my good friends Perl, Win32::OLE and DBI, I was able to write a script to help them out. But first I had to create a database of locations (loc_id, name, address1, city, state, zip).

It is a command line script.
Usage: mapit.pl 134849 134919
This would access our database and get the address of both places. It would then tell IE to navigate to MapBlast and generate a map from the first to the second. I have chosen 0 to represent my current location. So you could run it: mapit.pl 0 134919 to get a map from here to location #134919. It is also possible to run it with one location number to have it generate a destination map.

I give you Map It! (name just picked... may already be taken.)

#/usr/bin/perl -w use strict; use Win32::OLE; use DBI; # start IE my $ie = Win32::OLE->new('InternetExplorer.Application') or die("IE di +dn't start"); # create some variables that will be used in the creation of error mes +sages my $header = qq(<html><head><title>Map It!</title></head><body><h1 ali +gn="center">Map It!</h1>); my $usage = qq(<p><b>Usage:</b> $0 (location #1) [location #2]<br />Ma +p It! will return a map showing how to get from location #1 to locati +on #2.<br />However location #2 can be omitted to receive just a dest +ination map.</p><p><b>Examples:</b><br /><tt>$0 134898</tt> will get +a destination map for location 134989.<br /><tt>$0 0 134898</tt> will + get a route map from here to location 134898.</p><p>This example sho +ws using 0 to represent wherre I am. It can be used as the start or +end point.</p>); my $nodata = qq(<p><b>Location Not Found!</b></p><p>Check the location + numbers again.</p>); my $footer = qq(</body></html>); # get our location numbers my @loc = @ARGV; # how many were given? my $locs = @loc; # Make IE visible $ie->{Visible}=1; # If the wrong number of locations is given, show the usage message if ($locs == 0 || $locs > 2) { Error($ie,$header,$usage,$footer); } else { # setup the database stuff my $db = "database"; my $db_engine = "mysql"; my $host = "localhost"; my $user = "username"; my $password = "password"; my $dbh = DBI->connect("DBI:$db_engine:$db:$host",$user, $password,{ + PrintError => 0}) || die $DBI::errstr; my $table = "locations"; my $select = "name, address1, city, state, zip"; # our arrays to hold locations' address info for later my (@street, @csz); foreach my $loc (@loc) { # if $loc is 0, it's here... so skip the database stuff if ($loc == 0) { push(@street, "123 Main St"); # your street address push(@csz, "90210"); # your zip code next; } my $where = "loc_id = $loc"; my $sql = "SELECT $select FROM $table WHERE $where;"; my $sth = $dbh->prepare($sql); if (!$sth) { die "sth error:" . $dbh->errstr . "\n"; } if (!$sth->execute) { die "sth execute error:" . $sth->errstr . "\n"; } my $ref = $sth->fetchrow_hashref; # if $$ref{address1} doesn't exist, give an error message and die if (!exists $$ref{address1}) { Error($ie,$header,$nodata,$footer); die("$loc doesn't exist"); } # add the street address push(@street, $$ref{address1}); # and zip code if known, otherwise city and state push(@csz, ($$ref{zip} || "$$ref{city},$$ref{state}")); $sth->finish(); } $dbh->disconnect(); my $url; if ($locs == 1) { # if there was only one location number given, i +t's a destination only map $url="http://www.mapblast.com/myblast/map.mb?CMD=GEO&CT=&IC=&LV=" ."&GMI=&GAD1=&GAD2=&GAD3=&GAD4=&AD2=$street[0]&noPrefs=&req_action=" ."crmap&skip=&serch=&PHONE=&noBRP=&remLoc=&AD4=USA&AD2_street=$street[ +0]" ."&AD3=$csz[0]&apmenu=&apcode=&x=67&y=11&selCategory="; } else { # otherwise, it's a route map $url = "http://www.mapblast.com/myblast/driveSilo.mb?&AD4=USA&locB +oxNum=" ."&req_action=getdir&AD2=&AD3=&formnum=2&forceAddr=&CT=&GMI=" ."&multiIC=::::&locn_ckieVal=&serch=&skip=&MA=1&LV=11&CMD=FILL"; for my $i (1..2){ $url .= "&remLoc_$i=&AD4_$i=US&apmenu_$i=&apcode_$i=&GAD1_$i=&GA +D2_$i=&GAD3_$i=" ."&GAD4_$i=&phone_$i=&IC_$i=&GMI_$i="; } $url .= "&AD2_street_1=$street[0]&AD2_1=$street[0]&AD3_1=$csz[0]&A +D2_street_2=" ."$street[1]&AD2_2=$street[1]&AD3_2=$csz[1]"; } $ie->Navigate($url); } sub Error { my ($ie,$header,$error,$footer) = @_; $ie->Navigate(''); my $idoc = $ie->{Document}; $idoc->open("text/html","replace"); $idoc->writeln($header.$error.$footer); }

I could use some more comments but there are enough of them to give you a good idea of what is going on!

Update: I forgot to mention that this particular script only works in the United States. However, it would be a very simple task to make it work elsewhere. Just remember that as of right now, MapBlast will not map from one country to another.

Comment on Map It!
Download Code
Re: Map It!
by Mr. Muskrat (Abbot) on Sep 09, 2002 at 15:40 UTC

    This morning I tried to retrieve a map with Map It! and it didn't work. So I immediately went to work on fixing it. It didn't take long to track down the culprit of the problem. MapBlast has changed some of their URLs and forms.

    The section that needs repair is between the database disconnect and the navigate command.

    $dbh->disconnect(); my $loc="http://www.mapblast.com/myblastd/"; # The following lines have been split for readability if ($custs == 1) { $loc .= "createmap.d?remLoc=&AD4=USA&AD2=$street[0]&AD3=$csz[0]&se +lCategory=defaultCat"; $loc .= "&AIR=&E=ahV-jzkx2zJTnrUik5NBcHnqHTIJdm9rZN79CtIrBAeeodMgl +2b2trt_Mb5tbZCurQrHm"; $loc .= "83mt5yY3TIJdm2q459bb9X6F5SaApdx6S6l3Zofcz0nes5mfbCtcyn59F +QfmUv2e-5PxKXpHqmMw"; } else { $loc .= "getdd.d?&mapstyle=line"; # mapstyles are line and standar +d for my $i (1..2){ $loc .= "&AIR_$i=&AD4_$i=USA"; # This needs to change if you are + outside the US of A } $loc .= "&AD2_1=$street[0]&AD3_1=$csz[0]&AD2_2=$street[1]&AD3_2=$c +sz[1]&E=ahV-jdmSnOx7r"; $loc .= "nOPutSKTk0FweeodMgl2b2tk3v0K0isEB56h0yCXZva2u34xvm1tkK6tC +sebzea3nJjdMgl2barjn1"; $loc .= "tv1foXlJoCl3HpLqXdmh9zPSd6zmZ9sK4FezjK6Rnv_T8Sl66uKef6l84 +m3m45ODtYxvz3m45ODswxvz"; } $loc .= "&x=67&y=11"; # What are these for? Any value seems to work + though $ie->Navigate($loc);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://192950]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-07-28 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (209 votes), past polls