display-google-map-using-curl-in-php

Display Google Map Using Curl in PHP


Display Google Map Using Curl, this is the preferred way it gives much more control, its also faster then file_get_contents.

1.First Set the url like :

$url ="http://maps.google.com/maps/api/geocode/json?address=PUNE+INDIA&sensor=false";

The important part to point out there is the address=PUNE+INDIA the address expects an address also since this is a web URL it cannot have spaces instead use + in place of a space.

Going to the URL in a browser it get JSON object

2. Set the curl options, the fist option is the url to be called the second option tells the request to bring back data, the rest specify the port and hosts. curl_exec actually runs the curl request.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_PROXYPORT,3128); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); $response = curl_exec($ch); curl_close($ch);

3. The data from the curl request are returned and stored in $response from there, decode the json object.

$response = json_decode($response);

5. To specify the item in the object desired go down the result set accordingly, in this case bring back the longitude and latitude and store them to variables.

$lat = $response->results[0]->geometry->location->lat;

$long = $response->results[0]->geometry->location->lng;

Add $lan, $long variable value in to following HTML:

<!DOCTYPE html>
<html>
<head>
<title>Geocoding Example</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
#map_canvas { height: 330px; width:550px; }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $long; ?>);
var addressMarker = new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $long; ?>);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);

marker = new google.maps.Marker({ map:map, position: addressMarker });
}

</script>
</head>
<body onload="initialize()">
<h2>Geocoding Example</h2>
<div id="map_canvas"></div>
</body>
</html>

Example

<?PHP

$url = "http://maps.google.com/maps/api/geocode/json?address=baner+pune&sensor=false";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response);

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;
?>

<!DOCTYPE html>
<html>
<head>
<title>Map Example</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
#map_canvas { height: 330px; width: 550px; }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $long; ?>);
var addressMarker = new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $long; ?>);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);

marker = new google.maps.Marker({ map:map, position: addressMarker });
}

</script>
</head>
<body onload="initialize()">
<h2>Map Example</h2>
<div id="map_canvas"></div>
</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s