Jump to content

[PHP] Display Google Map based on user location

- - - - -

  • Please log in to reply
4 replies to this topic

#1
PsychoCoder

PsychoCoder

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
//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));?>' />

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

#2
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
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
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
@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.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
@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.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
@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.
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users