logos

With Andrii Grytsenko


Technical Diary - With Andrii Grytsenko

Google maps API to show users location

I will show you how to use google maps to print location of your site visitors.

First of all you need to get Google KEY, to do this you should have google account. If you have it visit google key page to generate the map key.

After you’ve done it -> you will be ready for next steps.

Below the script should be downloaded in your html directory as location.cgi for example:

#!/usr/bin/perl                                              

use strict;
use LWP;   

sub GetCode {
 my ($map_key,$lat,$long,$zoom)=@_;

 my $head = <<HEAD;
 <title>Your current location</title>
 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=$map_key"
 type="text/javascript"></script>
 <script type="text/javascript">
 function initialize() {
 if (GBrowserIsCompatible()) {
 var map = new GMap2(document.getElementById("map_canvas"));
 map.setCenter(new GLatLng($lat, $long), $zoom);
 map.setUIToDefault();
 }
 }
 </script>
HEAD
 my $body= <<BODY;
 <body onload="initialize()" onunload="GUnload()">
 Your current location is $ENV{REMOTE_ADDR}:<br>
 <div id="map_canvas" style="width: 500px; height: 300px"></div>
 </body>
BODY
return($head,$body);
}                                                                                                

sub GetHTMLCode {
 my ($head,$body)=@_;
 my $html = <<HTML;
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 $head
 </head>
 $body
HTML
 return $html;
}

sub GetCoordinates {
 my $ip = shift;
 my ($lat,$long);
 my $ua = new LWP::UserAgent;
 $ua->agent("Opera/9.0 " . $ua->agent);
 $ua->timeout("5");

 my $req = new HTTP::Request( GET => "http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=$ip");
 my $res = $ua->request($req);
 if (! $res->is_success) {
 die "can't get acccess to server \n";
 }
 my $content=$res->content;
 foreach my $str (split(/\n/,$content)){
 $lat = $1 if ($str =~ /meta name=.latitude. content=.(\d+\.\d+)./);
 $long = $1 if ($str =~ /meta name=.longitude. content=.(\d+\.\d+)./);
 }
 return($long,$lat);
}
my $map_key="";
my $zoom="10";
my $ip_addr=$ENV{REMOTE_ADDR};
my ($long,$lat)=GetCoordinates($ip_addr);
my ($head,$body)=GetCode($map_key,$lat,$long,$zoom);
my $html = GetHTMLCode($head,$body);
print "Content-type: text/html\n\n";
print $html;

This script also available by this link.
Your google key should be defined as variable map_key in the code.

Function GetCode – return two values:

  1. $head – this block should be put into the <head> section of your html code.
  2. $body – into the <body> section.

Function GetCoordinates return longitude and latitude of got ip.

Function  GetHTMLCode was used to generate example html code.

You also can implement these functions into your page as module. Download google_map.pm and save it in your perl modules directory or do it as describe in this post. After, you can easily call this functions from your code as show below:

#!/usr/bin/perl

use lib "path/to/directory/where/you/save/google_map.pm";
require google_map;

my $map_key="your_map_key_was_described_below";
my $zoom="10";
my $ip_addr=$ENV{REMOTE_ADDR};
my ($long,$lat)=google_map::GetCoordinates($ip_addr);
my ($head,$body)=google_map::GetCode($map_key,$lat,$long,$zoom);
my $html = google_map::GetHTMLCode($head,$body);
print "Content-type: text/html\n\n";
print $html;

To see how it work check this link out.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories

Translate