【问题标题】:how can i bind touch events for a marker in google maps?如何为谷歌地图中的标记绑定触摸事件?
【发布时间】:2012-07-04 12:20:05
【问题描述】:

google maps api 不支持触​​摸事件

但现在我需要在地图和标记上做一些事情,例如长按地图,点击标记,拖动标记。

我想做的和iOS的谷歌地图客户端一样

【问题讨论】:

    标签: javascript html google-maps google-maps-api-3 touch-event


    【解决方案1】:

    我以与 shreedhar 相同的方式实现触摸事件,但使用了“mousedown”事件。我发现在 web 视图(即 PhoneGap)中使用 Google Maps API 时不会在移动设备上触发“点击”,但通过点击移动设备或点击网络会触发“mousedown”事件。

    window.infowindow = new google.maps.InfoWindow({
       content: 'content'
    });
    
    google.maps.event.addListener(marker, 'mousedown', function(){
       window.infowindow.open(marker.get('map'), marker);
    });
    

    另外,请务必在您的页面上仅定义一个 infowindow 变量,并将其重新用于所有标记,因此我将 infowindow 定义为“全局”变量 window.infowindow。

    【讨论】:

      【解决方案2】:

      在此链接中,您可以获得许多示例。 https://google-developers.appspot.com/maps/documentation/javascript/examples/

      最近我在谷歌地图上工作,下面是你可以拖放标记的代码,它在移动网络浏览器中完美运行。

      gmap : function(lat,lng){
                  var stockholm = new google.maps.LatLng(lat, lng);
                  var parliament = new google.maps.LatLng(lat, lng);
                  var marker;
                  var map;
                  function initialize() {
                      var mapOptions = {
                          zoom: 13,
                          disableDefaultUI: true,
                          mapTypeId: google.maps.MapTypeId.ROADMAP,
                          center: stockholm
                      };
                      map = new google.maps.Map(document.getElementById("gmapDiv"),mapOptions);
                      marker = new google.maps.Marker({
                          map:map,
                          draggable:true,
                          animation: google.maps.Animation.DROP,
                          position: parliament
                      });
                     var infowindow = new google.maps.InfoWindow({
                         content: 'content'
                     });
                      google.maps.event.addListener(marker, 'click', function(){
                        infowindow.open(marker.get('map'), marker);
                        });
                  }
                  initialize();
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-03
        • 2011-03-08
        • 1970-01-01
        • 2018-07-31
        • 2015-02-06
        • 1970-01-01
        相关资源
        最近更新 更多