我正在尝试将Google地图集成到我的WordPress项目中。我确实有用户的地址。我没有使用任何WordPress插件,我使用PHP和curl来显示我的谷歌地图与标记和所有其他选项。
代码:
<?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 } ?>
我正在获得所需的谷歌地图,但除了谷歌地图上的保存选项,我无法获取获取路线选项。我为此尝试了不同的参数,但都是徒劳的。我很确定我错过了一些参数,因为除了获取路线选项,我得到了所有内容。任何人都可以帮助这个。任何帮助或建议表示赞赏。在此先感谢各位.. :))
答案 0 :(得分:1)
很奇怪但是如果你只是从660px
将你的iframe宽度增加到400px
,它会显示地图上的方向按钮。
<强> Working DEMO 强>
旁注:你会得到lat long,就像在结果数组0
键中一样,
$latitude = $geo_json['results'][0]['geometry']['location']['lat'];
$longitude = $geo_json['results'][0]['geometry']['location']['lng'];