1

我想要多种选项来为我们的 IE8 笔记本电脑进行地理定位,并研究了 Google Chrome 框架(有效)和 jquery polyfill(这是我正在尝试使用的)。示例来自的网站是webshims,我已经验证所提到的演示页面在 IE8 中有效,但我无法为自己重新创建示例。这是我的jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8>
    <title>Geolocation</title>
    <script src="<c:url value="/js/jquery-1.6.4.min.js"/>" type="text/javascript"></script>
    <script src="<c:url value="/js/modernizr-latest.js"/>" type="text/javascript"></script>
    <script src="<c:url value="/js/polyfiller.js"/>" type="text/javascript"></script>
    <script type="text/javascript">
      //http://afarkas.github.com/webshim/demos/demos/geolocation.html
      $.webshims.polyfill();
      navigator.geolocation.getCurrentPosition(function(pos){ $("#status").html("found you! latitude: "+ pos.coords.latitude +"/longitude: " + pos.coords.longitude); });
    </script>
  </head>
  <body>
    <p>Finding your location: <span id="status">checking...</span></p>
  </body>
</html>

它适用于任何原生支持地理定位的浏览器,但 polyfill 似乎并没有为 IE8 完成它的工作。我收到以下错误:

网页错误详情

用户代理:Mozilla/4.0(兼容;MSIE 8.0;Windows NT 5.1;Trident/4.0;chromeframe/15.0.874.121;.NET CLR 2.0.50727;.NET CLR 3.0.4506.2152;.NET CLR 3.5.30729)时间戳:星期三, 2011 年 12 月 7 日 17:48:06 UTC

消息:'navigator.geolocation' 为空或不是对象行:13 字符:7 代码:0 URI:../GeoLocation/jquery.action

我是否缺少使此功能正常运行的东西?

4

3 回答 3

3

您需要先激活 polyfill。请参阅头部包含以下脚本的示例源:

$.webshims.setOptions('geolocation', {
            confirmText: '{location} wants to know your position. It is Ok to press Ok.'
        });
        //load all polyfill features
        //or load only a specific feature with $.webshims.polyfill('feature-name');
        $.webshims.polyfill();

更新:这个 HTML 页面在 IE9、IE7 和 Chrome 中都可以正常工作

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8>
    <title>Geolocation</title>
    <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="http://afarkas.github.com/webshim/demos/js-webshim/minified/extras/modernizr-custom.js" type="text/javascript"></script>
    <script src="http://afarkas.github.com/webshim/demos/js-webshim/minified/polyfiller.js" type="text/javascript"></script>
    <script type="text/javascript">
      //http://afarkas.github.com/webshim/demos/demos/geolocation.html
      $.webshims.setOptions('geolocation', {
            confirmText: '{location} wants to know your position. It is Ok to press Ok.'
        });
      $.webshims.polyfill();
      $(function(){
        navigator.geolocation.getCurrentPosition(function(pos){ $("#status").html("found you! latitude: "+ pos.coords.latitude +"/longitude: " + pos.coords.longitude); });
      });
    </script>
  </head>
  <body>
    <p>Finding your location: <span id="status">checking...</span></p>
  </body>
</html>

确保使用 HTTPL// 而不是 FILE:// 来提供它

于 2011-12-07T18:24:53.917 回答
3

我是 webshims lib 的创建者。为了澄清你的问题,这里有一些信息。

  1. 您无需设置选项即可获得地理定位工作。这只是一个选项,而不是初始化代码。(有关设置选项的更多信息)

  2. 是的,您必须初始化您的代码,如果您只需要 geoloaction 而不需要其他功能,您应该使用以下行(有关初始化功能的更多信息):

    $.webshims.polyfill('地理位置');

如果您使用不带任何参数的 polyfill 方法,所有功能都将被实现,这会增加下载大小。

  1. Polyfill-Readiness(原因,您的脚本不起作用的原因)。Webshims 使用脚本加载器来实现这些功能,该加载器实现所有异步操作,因此您必须等待特定的回调。(有关功能就绪性的更多信息)

要么等待 jQuery 的就绪回调(延迟到所有功能都实现:

$(function(){
    //use feature here (all features + DOM are ready
});

或者,如果您想尽快访问您的功能,有一个特殊的就绪回调,它将在 DOM-Ready 之前触发:

$.webshims.ready('geolocation', function(){
    //use geolocation feature here
});

如果您有任何问题或任何建议,包括如何改进文档,请告诉我。

于 2012-01-05T23:32:44.117 回答
0

本地主机上可能navigator.geolocation不允许尝试将其推入 www。:)

于 2011-12-07T18:15:43.700 回答