Javascript Google Maps API在Web浏览器中加载,但不在Android浏览器中加载

Javascript Google Maps API loading in Web Browser but not Android Browser

本文关键字:加载 浏览器 Android Google Web Javascript Maps API      更新时间:2023-09-26

我不知道为什么它不会在我的移动浏览器上运行,但会在我的PC浏览器上运行。

你们中有人能提供反馈吗。

链接到我正在测试的服务器:

54.172.35.180/帽子/帽子列表.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数据时,它就停止了在移动设备浏览器上的工作。如有任何帮助,我们将不胜感激。

navigator.geolocation.getCurrentPosition不返回位置。因此,您不能将该结果用作addDomListener的参数(window,…

它的作用是发送一个请求。当该请求准备就绪时,将触发回调。在该回调中,您可以触发函数success()(我将其命名为initialize)。


只是为了展示正在发生的事情。你仍然可以选择这是否是一个好的方式继续。请随意重命名、扩展。。。

<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>

也许最好先加载地图并设置默认位置。找到位置后,可以更改中心并设置标记。现在请注意:如果找不到位置,则需要相当长的时间才能将locationNotFound称为