2013-08-24 218 views
0

我在生成Google地圖並在我的頁面上顯示時遇到問題。 這是我的看法。我能做些什麼才能使其正常工作?Google地圖不顯示(點擊按鈕)

<?php 
    $telephones = $this->telephones; 
    $telephonesa = array(); 
    foreach($telephones as $telephone){ 
     array_push($telephonesa, $telephone); 
    } 
?> 

<div id="googleMap" style="width:500px;height:380px;"></div> 

// client-side 
<script> 
var object = {}; 
    object.dist = 100000; 
    $(document).ready(function() { 
     $("#getclosest").click(function() { 
      $.mobile.loading('show'); 
      getLocation(); 
     }); 
    }); 

    function getLocation() 
    { 
     if (navigator.geolocation) 
     { 
      navigator.geolocation.getCurrentPosition(showPosition); 
     } 
     else{ 
      alert("Geolocation is not supported by this browser."); 
     } 
    } 

    function showPosition(position) 
    { 
     var currlat = position.coords.latitude; 
     var currlon = position.coords.longitude; 

     getClosest(currlat, currlon); 
    } 

    function getClosest(currlat, currlon){ 

     var telephones = <?php echo json_encode($telephonesa); ?>; 

     var length = telephones.length, 
      element = null; 
     for (var i = 0; i < length; i++) { 
      element = telephones[i]; 
      object.distance = getDistanceBetweenLatLongs(currlat, currlon, element.Location.Longitude, element.Location.Latitude); 

      if (object.distance < object.dist){ 
       object.dist = object.distance; 
       object.index = i; 
      } 
     } 

     showToilet(object.index); 
    } 

    function showToilet(index){ 
     var telephones = <?php echo json_encode($telephonesa); ?>; 
     var telephone = telephones[index]; 

     showGooglemaps(telephone.Location.Latitude, telephone.Location.Longitude); 
    } 

    function showGooglemaps(lat,lon){ 
     google.maps.visualRefresh = true; 
     var myCenter=new google.maps.LatLng(lat,lon); 
     var marker; 

     function initialize() 
     { 
      var mapProp = { 
       center:myCenter, 
       zoom:13, 
       mapTypeId:google.maps.MapTypeId.ROADMAP 
      }; 
      var map=new google.maps.Map(document.getElementById("googleMap") 
       ,mapProp); 


      marker=new google.maps.Marker({ 
       position:myCenter, 
       animation:google.maps.Animation.BOUNCE 
      }); 

      marker.setMap(map); 

      google.maps.event.trigger(map, 'load'); 

     } 
     $("#getclosest").hide(); 
     $.mobile.loading('hide'); 

     google.maps.event.addDomListener(window, 'load', initialize); 
    } 

正如你可以看到我有一個函數「ShowGooglemaps」,我稱之爲但輸出顯示什麼...

+0

我在javascript控制檯中顯示語法錯誤。 – geocodezip

+0

我在我的控制檯中沒有錯誤.. – nielsv

回答

1

window火災load -event當所有ressources已加載,但功能當你點擊按鈕時,將會調用ShowGooglemaps。此時通常load -event已經被觸發(並且不會再次觸發,並且回調也不會立即執行,因爲它例如jQuery的就緒偵聽器)。

這條線:

google.maps.event.addDomListener(window, 'load', initialize); 

..是沒用的,initialize將永遠不會被調用。

沒有必要將初始化映射的代碼包裝到initialize函數中,只需執行代碼而無需等待load -event。