user4974882
user4974882

Reputation: 33

google map mouseevent in firefox shows incorrect coordinates

I have a google map api page that does not return the correct coordinates from the mouse event in firefox when the map div is not at the top left (0,0) of the browser window. If I put any css padding or margin on the map div, the mouseevent origin in google maps still starts from the top left of the browser window and not the map div. The mouseevent works correctly in IE & Chrome returning the correct lat/lng but not Firefox. Anyone have any suggestions to correct?

I created a very simple example at http://jsfiddle.net/fNPvf/15426/ that shows the coordinates as the mouse is moved over the map. You can see at the top left of the map image, the coordinates should be 0,0 but Firefox showing 50,50. The infowindow shows the correct lat/lng of the map center and you can see the difference in coordinates (50 pixel shift to top left) when you move the mouse to that point.

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

Upvotes: 2

Views: 1797

Answers (5)

user5147322
user5147322

Reputation: 26

Solution:

Add parameter v=3 once calling the google maps api. The fiddle provided by user4974882 is calling 3.exp - doesn't work.

Works:

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

Live example

Doesn't work correctly:

<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

(both fiddles from user4974882)

Background:

The problem occured with Firefox Version 39. The Mozilla-team implemented some new functionality regarding offsetX/Y (https bugzilla.mozilla.org/show_bug.cgi?id=69787). Unfortunately the Google Maps API already somehow plays around with that (https code.google.com/p/gmaps-api-issues/issues/detail?id=8278) and so - once FF39 was rolled out - the problem popped up.

Upvotes: 1

Jules
Jules

Reputation: 1

As stated - this bug crept in with FF 39.

In your example, replace the v=3.exp parameter with just v=3

So it would be

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

Upvotes: 0

DarkerBG
DarkerBG

Reputation: 1

When creating a marker use the option {optimized:false}. It solves the issue in 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>

Upvotes: 0

Pez
Pez

Reputation: 162

I can confirm that the following is bugged, given a map with a fixed size and a 64px margin, "Hello World!" will only appear when you place the mouse pointer at -64,-64 relative to the marker. It appears that inside Maps, the pointer position is offset by the x/y of the map element relative to the top left corner of Firefox' client area

This issue has appeared in the last few days

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

Upvotes: 1

user4974882
user4974882

Reputation: 33

Problem with latest release of FF v39. Worked previously in v38.0.5.

Upvotes: 1

Related Questions