Support Forums

Full Version: Display Google Map based on users location
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Credits and Source: http://www.dreamincode.net/

Name: Display Google Map based on users location
Description: Snippet that gets the users IP address and displays a Google Map based on their location. If no location can be generated a default image is shown (expected as one of the function parameters) .Changed to use GeoBytes.Com for more accurate results.
Snippet:

PHP Code:
//Code for betting 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.com/apis/maps/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));?>' /> 

Instructions: Need a Google Maps API key which can be obtained here http://code.google.com/apis/maps/signup.html

That it,
Thankyou for reading. Be happy always Smile