I am trying to integrate Google map in my WordPress project. I do have address of the users.I am not using any WordPress plugin, I am using PHP and curl to show my Google map with marker and all other options.
Code:
<?php
if(isset($_POST['show']))
{
$iframe_width = '400px';
$iframe_height = '400px';
$address = $_POST['address'];
//$address = '12215 East Sprague Avenue, Spokane Valley, WA 99206';
//echo 'Address to find: ' . $address . '<br>';
$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
//echo 'URL: ' . $url . '<br>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
$data = curl_exec($ch);
curl_close($ch);
$geo_json = json_decode($data, true);
if ($geo_json['status'] == 'OK') {
$latitude = $geo_json['results']['geometry']['location']['lat'];
$longitude = $geo_json['results']['geometry']['location']['lng'];
$map_params = array(
'f' => 'q',
'source' => 's_q',
'hl' => 'en',
'q' => $address,
'aq' => 0,
'ie' => 'UTF8',
'hnear' => $address,
't' => 'm',
'll' => $longitude .','. $latitude,
'z' => 12,
'output' => 'embed'
);
$url = 'http://maps.google.com/maps?' . http_build_query($map_params);
?>
<iframe width="<?php echo $iframe_width; ?>" height="<?php echo $iframe_height; ?>" frameborder="0"
scrolling="no" marginheight="0" marginwidth="0" src="<?php echo $url ?>"></iframe>
<?php
} else {
echo "<p>No Address Available</p>";
}
}
else
{
?>
<form action="#" method="post">
<table align="center">
<tr>
<td align="right">
Enter Address :
</td>
<td>
<input type="text" style="width:500px;" name="address">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="show" value="true">
<input type="submit" name="map" value="Show MAP">
</td>
</tr>
</table>
</form>
<?php } ?>
I am getting desired google map but I am not able to fetch get Directions option besides save option on google map. I tried different parameters for this but all in vain. I am quite sure that I am missing some parameter as I am getting everything except get directions option. Can anyone please help with this. Any help or suggestion is appreciated. Thanks in advance guys..:)
Its strange but if you just increase your iframe width to
660px
from400px
it will show you the direction button on the map.Working DEMO
Side Note: you will get lat long as in the results array
0
key,