当我尝试在网络上显示某个组织的所有公司的地图时,我遇到了问题。
当我访问我的网站时,我发现地图未显示。 我要检查项目,我得到错误" initMap不是函数"。
我不知道为什么会出现这个错误。 你能救我吗?
我附上了代码:
<?php
if($_SERVER['REMOTE_ADDR']=='80.32.126.235'){
error_reporting(E_ALL);
ini_set('display_errors', '1');
}
$json = file_get_contents('https://aplicatiuspin.cat/aplicatiu/webServices/views/?v=empreses/json&idAplicatiu=59&idEtiqueta=1&template=none');
$empreses = json_decode($json, true);
?>
<div id="map" style="height: 500px; width: 100%;"></div>
<script type="text/javascript">
function initMap() {
var map_cnt = {lat:41.563211, lng:2.008875};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: map_cnt,
scrollwheel: false
});
var mapBound = new google.maps.LatLngBounds();
var mapInfoWindow = new google.maps.InfoWindow();
<?php
foreach ($empreses as $e) {
if (is_numeric($e['latitud']) && is_numeric($e['longitud'])) {
?>
var marker<? echo $e['id'];?> = new google.maps.Marker({
position: new google.maps.LatLng(<? echo $e['latitud'];?>, <? echo $e['longitud'];?>),
map: map,
title: '<? echo addslashes($e['nom']); ?>'
});
mapBound.extend(marker<? echo $e['id'];?>.getPosition());
google.maps.event.addListener(marker<? echo $e['id'];?>, "click", function () {
var tmp = jQuery("#divEmpresa<?=$e['id']; ?>");
tmp.popup('show');
});
map.fitBounds(mapBound);
<?
}
}
?>
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
<script src="/webServices/views/js/jquery.popupoverlay.js"></script>
<div style="overflow: hidden; height: 1px; width: 1px;">
<?php
foreach ($empreses as $e):?>
<div class="divEmpresa" id="divEmpresa<?=$e['id']; ?>" style="background-color: #ffffff; padding: 20px; border-radius: 5px; width: 50%;">
<div style="text-align: right; padding: 0 5px;">
<a href="#" class="divEmpresa<?=$e['id']; ?>_close" >
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</div>
<div class="row">
<!--<div class="col-sm-4"><img src="/webServices/views/img/empresa<?=$e['id']; ?>.jpg"> </div>-->
<div class="col-sm-8"><h2><?=$e['nom']; ?></h2></div>
</div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-8">
<?=$e['adreca']; ?><br>
<?=$e['cp']; ?> - <?=$e['poblacio']; ?> <br>
<?=$e['telefon']; ?><br>
<a href="<?=$e['web']; ?>" target="_blank"><?=$e['web']; ?></a>
</div>
</div>
</div>
<?
endforeach;
?>
</div>
<script>
jQuery( document ).ready(function() {
jQuery('.divEmpresa').popup({
transition: 'all 0.3s',
scrolllock: true
});
});
</script>
&#13;
答案 0 :(得分:0)
更新代码
<?php
if($_SERVER['REMOTE_ADDR']=='80.32.126.235'){
error_reporting(E_ALL);
ini_set('display_errors', '1');
}
$json = file_get_contents('https://aplicatiuspin.cat/aplicatiu/webServices/views/?v=empreses/json&idAplicatiu=59&idEtiqueta=1&template=none');
$empreses = json_decode($json, true);
?>
<div id="map" style="height: 500px; width: 100%;"></div>
<script type="text/javascript">
function initMap() {
var map_cnt = {lat:41.563211, lng:2.008875};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: map_cnt,
scrollwheel: false
});
var mapBound = new google.maps.LatLngBounds();
var mapInfoWindow = new google.maps.InfoWindow();
<?php
foreach ($empreses as $e) {
if (is_numeric($e['latitud']) && is_numeric($e['longitud'])) {
?>
var marker<? echo $e['id'];?> = new google.maps.Marker({
position: new google.maps.LatLng(<? echo $e['latitud'];?>, <? echo $e['longitud'];?>),
map: map,
title: '<? echo addslashes($e['nom']); ?>'
});
mapBound.extend(marker<? echo $e['id'];?>.getPosition());
google.maps.event.addListener(marker<? echo $e['id'];?>, "click", function () {
var tmp = jQuery("#divEmpresa<?=$e['id']; ?>");
tmp.popup('show');
});
map.fitBounds(mapBound);
<?
}
}
?>
}
</script>
<script async defer src="https://maps.google.com/maps/api/js?v=3.25&key=enter-your-key-here&callback=initMap"></script>
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="https://cdn.rawgit.com/vast-engineering/jquery-popup-overlay/1.7.13/jquery.popupoverlay.js"></script>
<div style="overflow: hidden; height: 1px; width: 1px;">
<?php
foreach ($empreses as $e):?>
<div class="divEmpresa" id="divEmpresa<?=$e['id']; ?>" style="background-color: #ffffff; padding: 20px; border-radius: 5px; width: 50%;">
<div style="text-align: right; padding: 0 5px;">
<a href="#" class="divEmpresa<?=$e['id']; ?>_close" >
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</div>
<div class="row">
<!--<div class="col-sm-4"><img src="/webServices/views/img/empresa<?=$e['id']; ?>.jpg"> </div>-->
<div class="col-sm-8"><h2><?=$e['nom']; ?></h2></div>
</div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-8">
<?=$e['adreca']; ?><br>
<?=$e['cp']; ?> - <?=$e['poblacio']; ?> <br>
<?=$e['telefon']; ?><br>
<a href="<?=$e['web']; ?>" target="_blank"><?=$e['web']; ?></a>
</div>
</div>
</div>
<?
endforeach;
?>
</div>
<script>
jQuery( document ).ready(function() {
jQuery('.divEmpresa').popup({
transition: 'all 0.3s',
scrolllock: true
});
});
</script>
您已创建Google API密钥并将其粘贴到“在此输入您的密钥”位置。
要创建Google API密钥,请单击下面的链接
https://developers.google.com/maps/documentation/javascript/get-api-key
干杯!!
答案 1 :(得分:0)
您必须忘记添加google-api-key
,如下所示。
在此处获取YOUR_API_KEY并添加它..
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>