这可能是一个简单的问题,但我将不胜感激任何帮助 我已经从我的MVC模型中加载了一个$ _SESSION变量,它是地图中纬度和经度点的多维区域。这里我从
加载了这个 for($i=0; $i<count($markets['results']); $i++) {
.
.
$marketinfo["lat"][$i] = floatval($lat);
$marketinfo["long"][$i] = floatval($lon);
};
$_SESSION['MARKETLOCATION']['LAT'] = $marketinfo["lat"];
$_SESSION['MARKETLOCATION']['LONG'] = $marketinfo["long"];
然后在视图部分中,我使用此代码调用_template for header
<script type="text/javascript">
function createmap(LatLong) {
var firstpass = true;
for (var i = 0; i < $LatLong.length; i++) {
var lat = $LatLong['LAT'][i];
var lng = $LatLong['LONG'][i];
if (firstpass === true){
var map = new google.maps.Map(document.getElementById('map-canvas'),{
zoom: 10,
center: new google.maps.LatLng($lat[0]+','+$lng[0]),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
firstpass = false;
}
var infowindow = new google.maps.InfoWindow();
marker = new google.maps.Marker({
position: new google.maps.LatLng($lat[i]+','+$lng[i]),
map: map
});
}
window.onload=function(){("createmap($_SESSION['MARKETLOCATION']");};
}
</script>
</head>
<body>
<div class='Mapbox' id = 'map-canvas' ></div>
问题是地图根本没有显示。我知道我的API密钥是正确的,因为我在创建此代码之前测试了它。所以我认为它与传递会话变量有关 再次提出任何见解
答案 0 :(得分:0)
修复代码:
window.onload=function(){("createmap(<?php echo $_SESSION['MARKETLOCATION'] ?>");};
答案 1 :(得分:0)
Javascript和php是两种不同的语言。你需要从php输出你的变量,但你不能只是回应它,因为$_SESSION['MARKETLOCATION']
是一个数组。
您应该执行以下操作:
var session_variable = <?php echo json_encode($_SESSION['MARKETLOCATION']); ?>;
// now you might have to do some processing on session_variable like parsing it...
// for an example:
var session_object = JSON.parse(session_variable);
window.onload=function(){ createmap(session_object); };