【问题标题】:google map mouseevent in firefox shows incorrect coordinatesFirefox中的谷歌地图鼠标事件显示不正确的坐标
【发布时间】:2015-08-19 12:53:07
【问题描述】:

当地图 div 不在浏览器窗口的左上角 (0,0) 时,我有一个谷歌地图 api 页面,它不会从 Firefox 中的鼠标事件返回正确的坐标。如果我在地图 div 上放置任何 css 填充或边距,谷歌地图中的 mouseevent 原点仍然从浏览器窗口的左上角开始,而不是地图 div。 mouseevent 在 IE 和 Chrome 中正常工作,返回正确的 lat/lng 但不是 Firefox。大家有什么建议可以改正吗?

我在http://jsfiddle.net/fNPvf/15426/ 创建了一个非常简单的示例,它显示了鼠标在地图上移动时的坐标。您可以在地图图像的左上角看到,坐标应该是 0,0,但 Firefox 显示的是 50,50。信息窗口显示地图中心的正确纬度/经度,当您将鼠标移动到该点时,您可以看到坐标的差异(左上角移动 50 像素)。

<html>
<head>
<meta charset="utf-8">
<style>
 body    {font:12px arial; margin:0px}
 #map {
    height:400px;
    width:400px;
}
</style>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
    var map;
    coord = new google.maps.LatLng(38.939201, -94.747640)

    function initialize() {
        var mapOptions = {
            zoom: 16,
            center: coord
        };
        map = new google.maps.Map(document.getElementById('map'), mapOptions);

        google.maps.event.trigger(map, 'resize');

        var coordInfoWindow = new google.maps.InfoWindow();
        coordInfoWindow.setContent('38.939201, -94.747640');
        coordInfoWindow.setPosition(coord);
        coordInfoWindow.open(map);

        google.maps.event.addListener(map, "mousemove", function (event) {
            document.getElementById("footer").innerHTML = "Mouse X Y:" + event.pixel.toString() + " - Lat Lng:" + event.latLng.toString() + "<br />"
                });
     }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map" style="margin-left:50px;margin-top:50px;"></div>
<div id="footer" style="margin-left:50px; padding:10px;"></div>
</body>
</html>

【问题讨论】:

  • 在 FF(38.0.5) 中对我来说是正确的
  • 太棒了,谢谢。看起来像是 Firefox 从 v38 到 39 的版本更改问题。
  • 前几天在Bugzilla中添加了这个bug:bugzilla.mozilla.org/show_bug.cgi?id=1180744
  • mouseover 听众的谷歌地图多边形相同的问题。它也适用于 firefox 38.0.5 版本。

标签: javascript google-maps firefox mouseevent


【解决方案1】:

解决方案:

在调用 google maps api 时添加参数 v=3。 user4974882 提供的小提琴正在调用 3.exp - 不起作用。

作品:

<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>

Live example

无法正常工作:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>

Live example

(来自 user4974882 的两个小提琴)

背景:

问题出现在 Firefox 版本 39。Mozilla 团队实施了一些关于 offsetX/Y 的新功能(https bugzilla.mozilla.org/show_bug.cgi?id=69787)。不幸的是,Google Maps API 已经以某种方式解决了这个问题(https code.google.com/p/gmaps-api-issues/issues/detail?id=8278),所以——一旦推出 FF39——问题就出现了。

【讨论】:

    【解决方案2】:

    如前所述 - 此错误与 FF 39 一起出现。

    在您的示例中,将 v=3.exp 参数替换为 v=3

    原来如此

    src="https://maps.googleapis.com/maps/api/js?v=3"
    

    【讨论】:

      【解决方案3】:

      创建标记时使用选项 {optimized:false}。它解决了 Firefox 39.0 中的问题。

          <!DOCTYPE html>
      <html>
        <head>
          <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
          <meta charset="utf-8">
          <title>Simple markers</title>
          <style>
            #map-canvas
            {
              width: 640px;
              height: 480px;
              margin: 64px;
            }
          </style>
          <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
          <script>
      function initialize() {
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
        var mapOptions = {
          zoom: 4,
          center: myLatlng
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Hello World!',
            optimized: false
        });
      }
      
      google.maps.event.addDomListener(window, 'load', initialize);
      
          </script>
        </head>
        <body>
          <div id="map-canvas"></div>
        </body>
      </html>
      

      【讨论】:

        【解决方案4】:

        我可以确认以下是错误的,给定一个固定大小和 64 像素边距的地图,“Hello World!”仅当您将鼠标指针放在相对于标记的 -64,-64 时才会出现。看来在 Maps 中,指针位置相对于 Firefox 客户区的左上角偏移了地图元素的 x/y

        最近几天出现了这个问题

        <!DOCTYPE html>
        <html>
          <head>
            <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
            <meta charset="utf-8">
            <title>Simple markers</title>
            <style>
              #map-canvas
              {
                width: 640px;
                height: 480px;
                margin: 64px;
              }
            </style>
            <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
            <script>
        function initialize() {
          var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
          var mapOptions = {
            zoom: 4,
            center: myLatlng
          }
          var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        
          var marker = new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: 'Hello World!'
          });
        }
        
        google.maps.event.addDomListener(window, 'load', initialize);
        
            </script>
          </head>
          <body>
            <div id="map-canvas"></div>
          </body>
        </html>
        

        【讨论】:

          【解决方案5】:

          最新版本的 FF v39 存在问题。以前在 v38.0.5 中工作过。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-05-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多