我想獲取谷歌地圖的網站API工作在我的PHP網站。 因爲我有一個PHP頁面加載地圖的用戶變量。 JavaScript在網頁中使用默認值正常工作,但我無法將$ php變量加載到腳本中。PHP的谷歌地圖的網站API javascript
也許一些JavaScript專家可以給手
function onProfileDisplay() {
$user = CFactory::getRequestUser();
$document =& JFactory::getDocument();
$document->addStyleSheet($css);
$my = CFactory::getUser();
$pluginParams = $this->params;
$param = new stdClass;
$param = $Lat = $pluginParams->get('Lat');
$param = $Lng = $pluginParams->get('Lng');
$param = $width = $pluginParams->get('width');
$param = $height = $pluginParams->get('height');
$param = $zoom = $pluginParams->get('zoom');
return '<body onload="onprofileDisplay()"> <div id="map_canvas" style="width:800px; height:500px"></div>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function onProfileDisplay() {
var latlng = new google.maps.LatLng(57.0442, 9.9116);
**var zoom = <?php echo ($zoom) ?>;**
var settings = {
**zoom: zoom,**
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var companyPos = new google.maps.LatLng(57.0442, 9.9116);
var companyMarker = new google.maps.Marker({
position: companyPos,
map: map,
title:"Some title"
});
}
</script>
';
}
測試我只是試圖將PHP變量$變焦加載到JavaScript的,但它不工作, 的JavaScript內:
var zoom = <?php echo ($zoom) ?>;
zoom: zoom,
但它不起作用,如果我將javascript更改爲原來的 remove var zoom =; zoom:15, 那麼它的工作原理,但與靜態默認值
幫助讚賞。
這是outuput代碼:
<script type="text/javascript">
function onProfileDisplay() {
var latlng = new google.maps.LatLng(57.0442, 9.9116);
var zoom = ".$zoom.";
var settings = {
".$zoom.":".$zoom.",
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var companyPos = new google.maps.LatLng(57.0442, 9.9116);
var companyMarker = new google.maps.Marker({
position: companyPos,
map: map,
});
}
</script>
原來的代碼如下所示:
public function onProfileDisplay() {
$user = CFactory::getRequestUser();
$document =& JFactory::getDocument();
$document->addStyleSheet($css);
$my = CFactory::getUser();
$pluginParams = $this->params;
$param = new stdClass;
$param = $api_key = $pluginParams->get('api_key');
$param = $Lat = $pluginParams->get('Lat');
$param = $Lng = $pluginParams->get('Lng');
$param = $width = $pluginParams->get('width');
$param = $height = $pluginParams->get('height');
$param = $zoom = $pluginParams->get('zoom');
return '<body onload="onProfileDisplay()">
<div id="map_canvas" style="width:800px; height:500px"></div
</body>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function onProfileDisplay() {
var latlng = new google.maps.LatLng(57.0442, 9.9116);
var zoom = ".$zoom.";
var settings = {
".$zoom.":".$zoom.",
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var companyPos = new google.maps.LatLng(57.0442, 9.9116);
var companyMarker = new google.maps.Marker({
position: companyPos,
map: map,
});
}
</script>';
}
}
?>
感謝您的幫助,腳本工作,但縮放值不是來自PHP,我用document.write(縮放)測試它;並且輸出是$ zoom,而不是變量$ zoom – Alforreca
'code'var zoom =「。$ zoom。」的php值。 var settings = {zoom。「:」。$ zoom。「, center:latlng,'code'你實際上並沒有改變腳本,google地圖圖層顯示空白,因爲值被傳遞對於變焦是$ zoom而不是php變量 – Alforreca
在輸出上做一個「查看源代碼」,並將它放在問題 – ethrbunny