2012-05-08 42 views
0

我想獲取谷歌地圖的網站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>'; 
       } 
    } 
    ?> 

回答

0

你爲什麼把PHP到從PHP的回報> javascript函數?看起來像一個麻煩的方式來獲得你的結果。

在任何情況下 - 爲什麼不直接從PHP函數中放入實際值?一旦它們在客戶端的JavaScript中,您將無法使用服務器端的值。

例如:

功能onProfileDisplay(){
變種經緯度=新google.maps.LatLng(57.0442,9.9116);
VAR變焦= 「$縮放。」
變量設置= {
「$縮放。 」:「 $縮放。」
中心:經緯度,

+0

感謝您的幫助,腳本工作,但縮放值不是來自PHP,我用document.write(縮放)測試它;並且輸出是$ zoom,而不是變量$ zoom – Alforreca

+0

'code'var zoom =「。$ zoom。」的php值。 var settings = {zoom。「:」。$ zoom。「, center:latlng,'code'你實際上並沒有改變腳本,google地圖圖層顯示空白,因爲值被傳遞對於變焦是$ zoom而不是php變量 – Alforreca

+0

在輸出上做一個「查看源代碼」,並將它放在問題 – ethrbunny