我有一个while循环,显示来自我的数据库的数据,如姓名地址等。我希望谷歌地图循环使用数据库中的地址。目前,谷歌地图只显示一次,我不认为它的循环与地图旁边的列表相同。
<script src="https://maps.google.com/maps/api/js?key=[KEY-HERE]&signed_in=true&callback=initMap"async defer></script>
while ($row = mysql_fetch_assoc($query)) {
echo "<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'>
<div class='titlebox'>
<h2>" . $row['company_name'] . "</h2>
</div>
<div class='box'>
" . ((empty($row['address_1'])) ? '' : "<strong> Address: </strong>" . $row['address_1']) . ((empty($row['address_2'])) ? '' : ", " . $row['address_2']) . ((empty($row['town'])) ? '' : ", " . $row['town']) . ((empty($row['county'])) ? '' : ", " . $row['county']) . ((empty($row['postcode'])) ? '' : ", " . $row['postcode']) . "
</div>
<div class='box'>
" . ((empty($row['tel'])) ? '' : "<strong> Telephone: </strong>" . $row['tel']) . ((empty($row['mobile'])) ? '' : "<strong> Mobile: </strong>" . $row['mobile']) . "
</div>
</div>
<div class='rightbox'>
<div class='service-pic'>
<img src='img/buddy.png' class='hvr-bob'>
</div>
<div id='map' style='width: 250px; height: 250px;'></div>
</div>
</div><br/>";
}
<script>
var address = "$row['address_1'], $row['town'], $row['postcode'], UK";
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
zoom: 6
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
map.setCenter(results[0].geometry.location);
}
});
</script>
答案 0 :(得分:1)
如果我正确理解了这个问题,您需要在页面上创建多个地图实例。
您必须为脚本标记中指定为回调的initMap()
JavaScript函数生成代码。这个函数将创建地图的多个实例,为此,我们可以引入$counter
变量以确保我们确实有不同的实例。另外,我从最近弃用的脚本标记中删除了signed_in参数。
代码段如下:
<script src="https://maps.google.com/maps/api/js?key=[KEY-HERE]&callback=initMap" async defer></script>
<?php
$counter = 0;
$jsInitFunction = "function initMap() { var geocoder = new google.maps.Geocoder(); ";
while ($row = mysql_fetch_assoc($query)) {
echo "<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'>
<div class='titlebox'>
<h2>" . $row['company_name'] . "</h2>
</div>
<div class='box'>
" . ((empty($row['address_1'])) ? '' : "<strong> Address: </strong>" . $row['address_1']) . ((empty($row['address_2'])) ? '' : ", " . $row['address_2']) . ((empty($row['town'])) ? '' : ", " . $row['town']) . ((empty($row['county'])) ? '' : ", " . $row['county']) . ((empty($row['postcode'])) ? '' : ", " . $row['postcode']) . "
</div>
<div class='box'>
" . ((empty($row['tel'])) ? '' : "<strong> Telephone: </strong>" . $row['tel']) . ((empty($row['mobile'])) ? '' : "<strong> Mobile: </strong>" . $row['mobile']) . "
</div>
</div>
<div class='rightbox'>
<div class='service-pic'>
<img src='img/buddy.png' class='hvr-bob'>
</div>
<div id='map" . $counter . "' style='width: 250px; height: 250px;'></div>
</div>
</div><br/>";
$jsInitFunction .= "var address" . $counter . " = '" . $row['address_1'] . "," . $row['town'] . "," . $row['postcode'] .", UK';
var map" . $counter . " = new google.maps.Map(document.getElementById('map" . $counter . "'), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
zoom: 6
});
geocoder.geocode({
'address': address" . $counter . "
}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map" . $counter . "
});
map" . $counter . ".setCenter(results[0].geometry.location);
}
});";
$counter++;
}
$jsInitFunction .= "}";
?>
<script>
<?php print $jsInitFunction; ?>
</script>