2014-11-06 63 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之前需要相当长的时间