我的谷歌地图javascript代码显示在桌面浏览器,但不是我的手机浏览器

My Google Map javascript code displays on desktop browser but not my mobile phone browser

本文关键字:浏览器 我的 手机 桌面 谷歌地图 javascript 代码 显示      更新时间:2023-09-26

好的,我已经查过了,只找到了一页。

(Google Maps API GeoLocation不支持移动设备)

我的问题是,我有一些javascript代码调用谷歌地图API和显示刚刚好,当我查看它使用我的浏览器在我的桌面,我使用的浏览器是谷歌浏览器,但当我试图查看它使用我的安卓手机它根本不显示。我不确定问题是什么,我让它运行了几天,但地图崩溃并停止显示。从那时起,我不得不重新做代码,让它再次显示。我终于把它修好了,但现在又有了新问题。这是我使用的代码。

    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <link rel="Stylesheet" type="text/css" href="css/drop.css" />
        <link rel="Stylesheet" type="text/css" href="css/login.css" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?   sensor=false"></script>
        <script type="text/javascript">
            function getLocation() {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(getMap, showError);
                }
                else {
                   alert("Geolocation is not supported by this browser.");
                }
            }
           function initialize( lat, lon ) {
               pLatitude = lat;
               pLongitutde = lon;
           var mapOptions = {
              center: new google.maps.LatLng(lat, lon),
              zoom: 14,
              mapTypeId: google.maps.MapTypeId.ROADMAP,
              mapTypeControl: true
           }
           var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
           var marker = new google.maps.Marker({
              position: new google.maps.LatLng(lat, lon),
              map: map,
              title: "Current Location"
           });
        }
        function getMap(position) {
             lat = position.coords.latitude;
             lon = position.coords.longitude;
             google.maps.event.addDomListener(window, 'load', initialize(lat, lon));
        }
        function showError(error) {
             switch (error.code) {
                 case error.PERMISSION_DENIED:
                    alert("User denied the request for Geolocation.");
                    break;
                case error.POSITION_UNAVAILABLE:
                    alert("Location information is unavailable.");
                    break;
                case error.TIMEOUT:
                    alert("The request to get user location timed out.");
                    break;
                case error.UNKNOWN_ERROR:
                    alert("An unkown error occurred.");
                    break;
        }
    }
</script>
</head>
<body onload="getLocation()">
   <div id="map-canvas"></div>
</body>
</html>

你犯了一些错误。我更新了你的代码,并添加了注释的解释:http://jsfiddle.net/cj5xF/1/

还有一个全屏视图(在手机上测试):http://jsfiddle.net/cj5xF/1/embedded/result/

固定代码的片段:

    // When the page has loaded, call the getLocation function.
    // Be sure not to use parenthesis after getLocation, or you will
    // call it, instead of passing a reference to it to be called later (callback)
    google.maps.event.addDomListener(window, 'load', getLocation);

这将在页面加载后调用getLocation函数。getLocation函数将执行地理位置检查,如果可用,调用initialize并将包含坐标的position对象传递给它。然后initialize将使用这些坐标创建一个谷歌地图。

相关文章: