我认为自己是一个普通的PHP编码器,但我几乎无法编辑我在网站上需要的一些JavaScript代码。我在PHP变量中有一个地址,我希望在我的网页上显示一个简单的地图。我在互联网上做了一些研究,我找到了很多运算地理编码,将地址转换为Google API理解的位置,但我完全迷失了。有没有简单的代码可以嵌入我的变量?
您可以使用iframe并将地址作为URL参数传递。
<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $yourAddress; ?>&output=embed"></iframe>
你必须使用Geocoder API。
在这里你去(原始可以找到here):
<head>
...
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom: 12, //Change the Zoom level to suit your needs
mapTypeId: google.maps.MapTypeId.ROADMAP
}
//map_canvas is just a <div> on the page with the id="map_canvas"
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
//Your Variable Containing The Address
var address = "<?php echo $AddressVariable; ?>";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
//places a marker on every location
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
...
</head>
将其添加到页面的开头body
标记以初始化地图:
<body onload="initialize()">
并且here's是Geocoder
类的文档。
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<?php
$address = str_replace(" ", "+",$address_variable);
?>
<iframe style="width:100%;height:50%;" frameborder="0" id="cusmap" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $address; ?>&output=embed"></iframe>