PHP Google Maps Tutorial
Google Maps is one of the most popular Internet mapping services. Google's mapping programming interface can be used for Web page development in a variety of languages. PHP is a server-side scripting language used by Web developers to create dynamically generated content along with the Google Maps API. You must register for a developer key to access the API from your website.
Instructions
-
-
1
Open your Integrated Development Environment by double-clicking on the desktop program icon or under the "Program Files" sub-menu of the "Start" menu.
-
2
Open a new PHP file by selecting the appropriate file menu option on your IDE and enter the following variables that will be used to store position data in the PHP Web page:
$myLongitude = "";
$myLatitude = "";
$myPrecision = ""; -
-
3
Define the query string variables for accessing the Google Maps API, including your Google Maps API Key, the city and state of the target location, and an output type and location for the information looked up through the Google Maps API. To use the code for a significant amount of data, change the output type from comma separated values (csv) to XML:
$myKey = "YOUR Google Maps API Key";
$myAddress = urlencode("lexington KY");
$myUrl =
"http://maps.google.com/maps/geo?q=".$myAddress."&output=csv&key
=".$myKey; -
4
Define a CURL request, to prevent information headers from being returned, and complete other basic setup information for the Google Maps API call with the following PHP code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $myUrl);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$myData = curl_exec($ch);
curl_close($ch); -
5
Display the latitude and longitude of the city and state entered for the "myAddress" variable in the previous step:
$myData = explode(",",$data);
$myPrecision = $data[1];
$myLatitude = $data[2];
$myLongitude = $data[3];
echo "Latutide: ".$myLatitude."<br>";
echo "Longitude: ".$myLongitude."<br>"; -
6
Save and view the PHP generated webpage.
-
1