在Google Maps V3中,如何获得可拖动的标记以平移地图? [英] In Google Maps V3, how can I get a draggable marker to pan the map?

查看:105
本文介绍了在Google Maps V3中,如何获得可拖动的标记以平移地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了几个V2示例,介绍拖动标记时如何平移地图。例如: http://www.putyourlightson.net/projects/coordinates

  //创建地图并添加控件
var map = new GMap2(document.getElementById(map));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

//设定地图中心点
var centrePoint = new GLatLng('53 .34870686020199','-6.267356872558594');
map.setCenter(centrePoint,14);

//添加一个可拖动的标记
var marker = new GMarker(centrePoint,{draggable:true});
map.addOverlay(marker);

//在地图中添加一个拖动监听器
GEvent.addListener(marker,dragend,function(){
var point = marker.getPoint();
map.panTo(point);
document.getElementById(latitude)。value = point.lat();
document.getElementById(longitude)。value = point.lng() ;
});

这个页面似乎在标记被拖拽时会自动平移;注意它唯一的事件监听器是dragend。但我向你保证,当标记被拖拽时,地图会平移。



我试图用V3 API实现同样的功能,但没有取得任何成功。我甚至试图在图标被拖动时调用map.panTo(),结果令人不满意: http:// www p
$ b $ pre $ 函数初始化()
{
格洛斯特= new google.maps.LatLng(42.6159285,-70.6619888);

myOptions =
{
zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
center:Gloucester,
streetViewControl :false
}

map = new google.maps.Map(document.getElementById(map_canvas),myOptions);

marker = new google.maps.Marker({position:Gloucester,title:Gloucester,MA});
marker.setMap(map);
marker.setDraggable(true);

google.maps.event.addListener(marker,'drag',function(event)
{
//平移到这个位置(不起作用!)
map.panTo(marker.getPosition());
});


对我来说这是行不通的,因为在将地图移动到地图的中心时,将标记放置在地图的中心位置是伪造的。



是否像V2 API一样简单,自动执行此操作,而V3 API不?如何才能在V3 API中实现这种效果?



谢谢。

解决方案

Google已经解决了这个问题: http:// code .google.com / p / gmaps-api-issues / issues / detail?id = 2404



您可以在 http://www.publicgloucester.com/test2.html 。忽略评论说它不做我想要的;该评论已过时。


I have found several V2 examples of how to pan the map while a marker is being dragged. For example: http://www.putyourlightson.net/projects/coordinates

    // create map and add controls
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());        
map.addControl(new GMapTypeControl());

// set centre point of map
var centrePoint = new GLatLng('53.34870686020199', '-6.267356872558594');
map.setCenter(centrePoint, 14); 

// add a draggable marker
var marker = new GMarker(centrePoint, {draggable: true});
map.addOverlay(marker);

// add a drag listener to the map
GEvent.addListener(marker, "dragend", function() {
    var point = marker.getPoint();
    map.panTo(point);
    document.getElementById("latitude").value = point.lat();
    document.getElementById("longitude").value = point.lng();
});

This page seems to "auto-pan" while the marker is being dragged; note that its only event listener is for "dragend". But I assure you that that map pans while the marker is being dragged.

I am trying to achieve the same thing with the V3 API, without any success. I even tried calling map.panTo() while the icon is being dragged, with unsatisfying results: http://www.publicgloucester.com/test.html

function initialize ()
   {
   Gloucester = new google.maps.LatLng (42.6159285, -70.6619888);

   myOptions = 
      {
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: Gloucester,
      streetViewControl: false
      }

   map = new google.maps.Map (document.getElementById ("map_canvas"), myOptions);

   marker = new google.maps.Marker ({position: Gloucester, title: "Gloucester, MA"});
   marker.setMap (map);
   marker.setDraggable (true);

   google.maps.event.addListener (marker, 'drag', function (event) 
      {
      // Pan to this position (doesn't work!)
      map.panTo (marker.getPosition());
      });

   }

It makes sense to me that this wouldn't work, since panning to place the marker in the center of the map, while the map is moving, is bogus.

Is it as simple as the V2 API doing this automatically, while the V3 API does not? How can I achieve this effect with the V3 API?

Thanks.

解决方案

Google has fixed this: http://code.google.com/p/gmaps-api-issues/issues/detail?id=2404

You can see the fix in action at http://www.publicgloucester.com/test2.html. Ignore the comment that says "it doesn't do what I want"; that comment is obsolete.

这篇关于在Google Maps V3中,如何获得可拖动的标记以平移地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆