2014-11-06 67 views
0

我很笨,爲什麼這不會在我的手機瀏覽器上運行,但它會運行在我的PC瀏覽器,確切地說鉻。Javascript谷歌地圖API加載在網絡瀏覽器,但不是Android瀏覽器

您能否提供任何反饋意見。

鏈接到服務器,我在測試這個:

54.172.35.180/chat/chatlist.php

<script type="text/javascript"> 

    function success(position) { 

     var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     var myOptions = { 
     zoom: 15, 
     center: latlng, 
     }; 
     var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions); 

     var marker = new google.maps.Marker({ 
      position: latlng, 
      map: map, 
      title:"You are here! (at least within a "+position.coords.accuracy+" meter radius)" 
     }); 
    } 

    google.maps.event.addDomListener(window, 'load', navigator.geolocation.getCurrentPosition(success)); 

</script> 

我原本只是複製並從API粘貼代碼網站,它運行良好,但一旦我開始拉動GPS數據,它停止在我的移動設備的瀏覽器上工作。任何幫助,將不勝感激。

回答

1

navigator.geolocation.getCurrentPosition不返回位置。所以你不能使用這個結果作爲addDomListener的參數(窗口,...

它做什麼,是發送一個請求,當這個請求準備好時,一個回調被觸發,在這個回調中,你可以觸發你的函數成功()(我把它命名爲初始化)。


爲了表明這是怎麼回事。 您仍然可以選擇,如果這是進行一個很好的方式。 隨意命名,擴展,...

<script src="http://maps.googleapis.com/maps/api/js"></script> 
<script type="text/javascript"> 
    function initialize(position, accuracy) { 
     var latlng = position || new google.maps.LatLng(50.45, 4.45); // set your own default location. This gets called if the parameters are left blank. 
     var myOptions = { 
     zoom: 15, 
     center: latlng 
     }; 
     var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions); 
     if (position) { 
     var marker = new google.maps.Marker({ 
      position: latlng, 
      map: map, 
      title: "You are here! (at least within a " + accuracy + " meter radius)" 
     }); 
     } 
     else { 
     // position of the user not found 
     } 
    } 
    function locationFound(position) { 
     initialize( 
     new google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
     position.coords.accuracy 
    ); 
    } 
    function locationNotFound() { 
     initialize(null, null); 
    } 
    function page_loaded() { 
     // the page is loaded, we can send a request to search for the location of the user 
     navigator.geolocation.getCurrentPosition(locationFound, locationNotFound); 
    } 
    google.maps.event.addDomListener(window, 'load', page_loaded); 
</script> 
<style> 
#map-canvas { 
    width: 500px; 
    height: 400px; 
} 
</style> 
<div id="map-canvas"></div> 

也許這是一個更好的IDE a首先加載地圖並設置默認位置。 找到位置後,更改中心並設置標記。 現在注意:如果找不到位置,在調用locationNotFound之前需要相當長的時間

相關問題