有没有办法将Google地图标记始终修复到其地图的中心? [英] Is there a way to Fix a Google Maps Marker to the Center of its Map always?

查看:118
本文介绍了有没有办法将Google地图标记始终修复到其地图的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更准确地说,我试图完成的是当Google地图 时,Google地图标记 它始终保持固定在地图的中心位置
这会改善用户体验,我认为,这是我现在这样做的:

To be more precise, what I try to accomplish is that while I Drag a Google Map there's a Google Maps Marker that stays fixed at the center of the map at all times. This will improve User experience in my opinion, This is how I'm doing it now:

var map, marker, options;

options = {
  center: new google.maps.LatLng(latitude, longitude),
  zoom: 15,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControl: false,
  overviewMapControl: false,
  zoomControl: true,
  draggable: true
};

map = new google.maps.Map(mapContainer, options);

marker = new google.maps.Marker({
  position: new google.maps.LatLng(latitude, longitude),
  map: map,
  animation: google.maps.Animation.DROP
});

//This line is what makes the Marker stick to the center of the map
marker.bindTo('position', map, 'center');

我在上面的代码中遇到的问题是:当我将标记拖到标记<强>它不够流畅,即。当拖动结束时,标记会从'最后'中心跳到'新'中心。 (这在移动设备上更明显),因此我需要的是标记真正永久固定到其地图的中心。

The problem I have with the code above is that.. When I drag the map underneath the Marker it's not smooth enough, ie. the Marker makes a wierd jump from the 'last' center to the 'new' center when dragging ends. (this is more noticeable on mobile devices), Therefore what I need is that the Marker is Really permanently fixed to the Center of its Map.

有没有办法做到这一点? (我想我已经在网站上看过一次)

Is there a way to get this done? (I think I've seen this in a web-site once)

让我知道如果你们能帮助我。

Let me know If you guys can help me.

谢谢。

推荐答案

另一种方法并未使用真正的 google .maps.Marker

A different approach, that did not use a real google.maps.Marker:

在映射初始化之后,向地图容器中注入一个div,给它一个className(例如 centerMarker ),更多的事情将通过CSS完成:

after the map has been initialized, inject a div into the map-container, give it a className(e.g. centerMarker), the further things will be done via CSS:

.centerMarker{
  position:absolute;
  /*url of the marker*/
  background:url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;
  /*center the marker*/
  top:50%;left:50%;

  z-index:1;
  /*fix offset when needed*/
  margin-left:-10px;
  margin-top:-34px;
  /*size of the image*/
  height:34px;
  width:20px;

  cursor:pointer;
}

演示:http://jsfiddle.net/doktormolle/jcHqt/

这篇关于有没有办法将Google地图标记始终修复到其地图的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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