在本地测试谷歌地图V3地理位置? [英] test google maps V3 geolocation locally ?

查看:228
本文介绍了在本地测试谷歌地图V3地理位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的电脑上测试一些地理位置代码,但我甚至无法运行这些示例。
虽然当我尝试从我的电脑打开html文件时,它们完全从文档网站运行,但我得到一个空白页面,我所做的只是试图检测我的位置......

以下是代码:

 <!DOCTYPE html> 
< html>
< head>
< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no>
< meta charset =UTF-8>
< link href =/ apis / maps / documentation / javascript / examples / default.css
rel =stylesheettype =text / css>
<! -
包含地图javascript与sensor = true,因为此代码使用
传感器(GPS定位器)来确定用户的位置。
请参阅: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;

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

//尝试HTML5地理定位
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,
位置:pos,
content:'使用HTML5找到的位置。'
});

map.setCenter(pos);
},function(){
handleNoGeolocation(true);
});
} else {
//浏览器不支持地理定位
handleNoGeolocation(false);



函数handleNoGeolocation(errorFlag){
if(errorFlag){
var content ='错误:地理定位服务失败。
} else {
var content ='错误:您的浏览器不支持地理位置。
}

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 =stylesheettype =text / css> 


I'm trying to test some geolocation codes on my computer, but I'm not even able to run the examples. Although they run perfectly from documentation website when I try to open the html file from my computer I get a blank page, all I do is trying to detect my position...

Here is the code:

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

Please help, can you find out what am I doing wrong?

解决方案

You need change the link in the stylesheet to an absolute link:

<link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css">

这篇关于在本地测试谷歌地图V3地理位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆