javascript – 在本地测试谷歌地图V3地理位置?

前端之家收集整理的这篇文章主要介绍了javascript – 在本地测试谷歌地图V3地理位置?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的计算机上测试一些地理定位代码,但我甚至无法运行这些示例.
虽然当我尝试从我的计算机打开html文件时它们完全从文档网站运行,但我得到一个空白页面,我所做的只是试图检测我的位置……

这是代码

<!DOCTYPE html>
<html>
  <head>
    <Meta name="viewport" content="initial-scale=1.0,user-scalable=no">
    <Meta charset="UTF-8">
    <link href="/apis/maps/documentation/javascript/examples/default.css"
      rel="stylesheet" type="text/css">
    <!--
    Include the maps javascript with sensor=true because this code is using a
    sensor (a GPS locator) to determine the user's location.
    See: http://code.google.com/apis/maps/documentation/javascript/basics.html#SpecifyingSensor
    -->
    <script type="text/javascript"
        src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>

    <script type="text/javascript">
      var map;

      function initialize() {
        var myOptions = {
          zoom: 6,mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);

        // Try HTML5 geolocation
        if(navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

            var infowindow = new google.maps.InfoWindow({
              map: map,position: pos,content: 'Location found using HTML5.'
            });

            map.setCenter(pos);
          },function() {
            handleNoGeolocation(true);
          });
        } else {
          // Browser doesn't support Geolocation
          handleNoGeolocation(false);
        }
      }

      function handleNoGeolocation(errorFlag) {
        if (errorFlag) {
          var content = 'Error: The Geolocation service Failed.';
        } else {
          var content = 'Error: Your browser doesn\'t support geolocation.';
        }

        var options = {
          map: map,position: new google.maps.LatLng(60,105),content: content
        };

        var infowindow = new google.maps.InfoWindow(options);
        map.setCenter(options.position);
      }

      google.maps.event.addDomListener(window,'load',initialize);
    </script>
  </head>
  <body>
    <div id="map_canvas"></div>
  </body>
</html>

请帮忙,你能找出我做错了什么吗?

解决方法

您需要将样式表中的链接更改为绝对链接
<link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css">
原文链接:https://www.f2er.com/js/152826.html

猜你在找的JavaScript相关文章