views:

36

answers:

1

Dragging and dropping a marker on a Google Map inside a modal JQuery dialog isn't working properly using JQuery 1.4.3 and JQuery-UI 1.8.5.

The following snippet is reproducing the issue in Chrome 6.0 and Firefox 3.6.10 (but not Opera 10.63 which surprisingly doesn't have the problem), and this was working before upgrading JQuery (from 1.3.x and JQuery-UI 1.7.x as far as I can remember.)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt;

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"&gt;&lt;/script&gt;

    <script language="javascript" type="text/javascript" src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=ABQIAAAA7wvJIKWNmIHy1Zz-OBol4hRU04i6YYXAaKMufrGA2zKwYVdFjhQttL37dNm-ct8ZuquPxt-MCn7PIw"&gt;&lt;/script&gt;

    <style type="text/css"> .ui-dialog { border:8px solid #ccc; } .ui-dialog-titlebar { border-bottom: 2px solid #ccc; padding: 4px; } .ui-dialog-titlebar a { display: none; } </style>

  </head>

  <body onunload="GUnload();">

    <a href="#" class="open modal">Open modal</a>

    <a href="#" class="open">Open non-modal</a>



    <div id="popup" style="display:none;text-align:left;padding: 4px;">

      <div id="map" class="map" style="width:400px;height:300px;"></div>

    </div>

    <script type="text/javascript">

      $(function(){

        $(".open").click(function(){

          $("#popup").dialog({ modal:$(this).hasClass("modal"), // changing to false works ok

                               title:"Drag marker", width:450, position: "center", 

                               buttons: { "Cancel": function(){ $(this).dialog("close"); } },

                               open: function() {

                                       var map = new GMap2(document.getElementById("map"));

                                       map.setCenter( new GLatLng(-37.851414,145.052686), 15);

                                       map.addOverlay(new GMarker(new GPoint(145.052686, -37.851414), {draggable:true}));

                                     }

                              });

        }).eq(0).click();

      });

    </script>

  </body>

</html>

Note, the styling is minimal as I couldn't find a quick CDN link to JQuery-UI CSS/image files.

Using the above, when opening the dialog with modal:true, the marker (and sometimes the map) will get "stuck" to the mouse, i.e. the mouseup event isn't caught. Clicking/moving quickly will sometimes drop it again, but usually you have to press escape to close the dialog.

Opening with modal:false allows the marker to be dragged normally.

Is there any way to fix this without having to make the dialog non-modal?

I found this, http://forum.jquery.com/topic/modal-dialog-and-google-maps, which seems to be the same problem, but I can't figure out how to apply the answer suggested. I've tried adding z-index:1000 to the div.map but it didn't help.

+1  A: 

This works correctly with Google Maps Api v3. (version 2 is deprecated). Below is code for v3 Maps Api

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt;

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"&gt;&lt;/script&gt;

    <script language="javascript" type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;

    <style type="text/css"> .ui-dialog { border:8px solid #ccc; } .ui-dialog-titlebar { border-bottom: 2px solid #ccc; padding: 4px; } .ui-dialog-titlebar a { display: none; } </style>

  </head>

  <body>

    <a href="#" class="modal">Open modal</a>

    <a href="#" class="open">Open non-modal</a>



    <div id="popup" style="display:none;text-align:left;padding: 4px;overflow:hidden">

      <div id="map" class="map" style="width:400px;height:300px;"></div>

    </div>

    <script type="text/javascript">

      $(function(){

        $(".modal").click(function(){

          $("#popup").dialog({ modal:$(this).hasClass("modal"), // changing to false works ok

                               title:"Drag marker", width:450, position: "center", 
                               dragStart: function(e, ui) { e.stopPropagation() },

                               buttons: { "Cancel": function(){ $(this).dialog("close"); } },

                               open: function() {

                                var myLatlng = new google.maps.LatLng(-34.397, 150.644);
  var myOptions = {
    zoom: 8,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map"), myOptions);



                                      var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map, 
      draggable:true,
      title:"Hello World!"
  });   



                                     }

                              });

        });

      });

    </script>

  </body>

</html>
Michal
Good to know, but for various reasons, I'm stuck on v2 for now...
Tom
I also tried various z-index settings (as per the post you quoted) for this with v2. It seems like jQuery interferes with map events (even if the map container is outside the dialog div). If you want it this to work in a modal box you might have to roll your own. (http://foohack.com/tests/vertical-align/dialog.html)
Michal