Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[PHP] Display Google Map based on user location


  • Please log in to reply
4 replies to this topic

#1 PsychoCoder

PsychoCoder

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 27 August 2010 - 01:03 PM

//Code for getting users IP address
/*
* Function to get the users IP address. We "attempt"
* to determine if the user is coming through a proxy server
* by checking if HTTP_X_FORWARDED_FOR is set, then we
* use a regular expression to ensure the IP address
* found is in the proper format
*/
function getIP()
{
// here we check if the user is coming through a proxy
// NOTE: Does not always work as proxies are not required
// to provide this information
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
//reg ex pattern
$pattern = "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/"
// now we need to check for a valid format
if(preg_match($pattern, $_SERVER["HTTP_X_FORWARDED_FOR"]))
{
//valid format so grab it
$userIP = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
//invalid (proxy provided some bogus value
//so just use REMOTE_ADDR and hope for the best
$userIP = $_SERVER["REMOTE_ADDR"];
}
}
//not coming through a proxy (or the proxy
//didnt provide the original IP)
else
{
//grab the IP
$userIP = $_SERVER["REMOTE_ADDR"];
}

//return the IP address
return $userIP;
}

/*
* Function for displaying a Google Map based on the users
* location, which we get from their IP address then use
* HostInfo.info to get their location. Function expects
* 4 parameters:
* 1) Your Google Maps API key
* * Can be obtained here http://code.google.c...aps/signup.html
* 2) Width you want the returned image
* 3) Height you want the returned image
* 4) Zoom value
*
*
* Returns an image source, either one for the Google Map
* or the default image specified
*/
function ShowGoogleMap($api_key, $width, $height, $zoom)
{
$user_ip = getIP();
$meta_tags = get_meta_tags('http://www.geobytes.com/IPLocator.htm?GetLocation&template=php3.txt&IPAddress=' . $user_ip) or die('Error getting meta tags');

$city = $meta_tags['city'];
$state = $meta_tags['regioncode'];
$country = $meta_tags['fips104'];
$lat = $meta_tags['latitude'];
$long = $meta_tags['longitude'];

//encode the city & country into $address variable
//for use in the Google Map API url
$address = urlencode($city . ', ' . $state . ', ' . $country);

//create the image course for displaying the Google map
$img_source = 'http://maps.google.com/staticmap?key=' . $api_key . '&size=' . $width . 'x' . $height . '&markers=' . $lat . ',' . $long . '&zoom=' . $zoom;

//return the image source
return $img_source;
}

//Sample usage
<img src='<? echo(ShowGoogleMap('YourKey', 555, 280, 4));?>' />

  • 1
SELECT * FROM Users WHERE Clue > 0;
ERROR: 0 results returned
Posted Image

#2 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 10 September 2010 - 08:00 AM

Thanks, PsychoCoder. This is nice. I was always wondering how they converted the user IP to a location on the map. Is there a Yahoo map equivalent?
I'll be using this on an upcoming project.
+rep
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#3 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 10 September 2010 - 08:37 AM

@Roger:
Yahoo requires any of the following: street, city, state, zip, what the aformentioned GeoIP service will output: (say with get_meta_tags()):
[known] => true
    [locationcode] => CABCVANC
    [fips104] => CA
    [iso2] => CA
    [iso3] => CAN
    [ison] => 124
    [internet] => CA
    [countryid] => 43
    [country] => Canada
    [regionid] => 43
    [region] => British Columbia
    [regioncode] => BC
    [adm1code] => CA02
    [cityid] => 1320
    [city] => Vancouver
    [latitude] => 49.2500
    [longitude] => -123.1330
    [timezone] => -08:00
    [certainty] => 91

Which is ample data to feed into the request.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 10 September 2010 - 08:37 AM

@Roger:
Yahoo requires any of the following: street, city, state, zip. What the aformentioned GeoIP service will output: (say with get_meta_tags()):
[SIZE=1] [known] => true
    [locationcode] => CABCVANC
    [fips104] => CA
    [iso2] => CA
    [iso3] => CAN
    [ison] => 124
    [internet] => CA
    [countryid] => 43
    [country] => Canada
    [regionid] => 43
    [region] => British Columbia
    [regioncode] => BC
    [adm1code] => CA02
    [cityid] => 1320
    [city] => Vancouver
    [latitude] => 49.2500
    [longitude] => -123.1330
    [timezone] => -08:00
    [certainty] => 91[/SIZE]
Which is ample data to feed into the request.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 10 September 2010 - 08:48 AM

@NW: I see. Let me look into that. I'm debating which map to use right now for a project. I wonder what are the pros and cons of Google map vs. Yahoo map. I personally use G Map, but see a lot of sites that still use Yahoo or Bing map. I wonder what are the design considerations.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download