【问题标题】:Make draggable part in Google Maps api v3在 Google Maps api v3 中制作可拖动部分
【发布时间】:2015-10-24 18:23:07
【问题描述】:

是否可以在谷歌地图上画一个圆圈,然后只做那个内部位置/拖动整个地图?

我尝试了这个和其他方法,但没有成功。下面是我正在努力实现的快速 Photoshop 示例。

【问题讨论】:

  • 您希望圆形孔保持固定在中心吗?还是固定在地图上?你试过什么?
  • 确实,较暗的部分应该是一个“层”。从视觉上看,这很容易通过在“洞”透明的地方放置一个 png 来实现,但随后拖动地图的功能就消失了(然后单击并拖动图像层,而不是地图)。我尝试使用画布、svg 和数据 URI 制作蒙版,但均未成功。谢谢

标签: javascript html google-maps canvas google-maps-api-3


【解决方案1】:

根据对this question on SO: Google Map API v3 shade everything EXCEPT for polygon 的回答,在地图中心放置一个“洞”的选项

通过将洞的中心绑定到地图中心(而不是在center_changed 事件侦听器中进行),可能会使其不那么紧张

var citymap = {};
citymap['newyork'] = {
  center: new google.maps.LatLng(40.714352, -74.005973),
  population: 8143197
};
var cityCircle;
var bounds = new google.maps.LatLngBounds();
var outerbounds = [
  new google.maps.LatLng(85, 180),
  new google.maps.LatLng(85, 90),
  new google.maps.LatLng(85, 0),
  new google.maps.LatLng(85, -90),
  new google.maps.LatLng(85, -180),
  new google.maps.LatLng(0, -180),
  new google.maps.LatLng(-85, -180),
  new google.maps.LatLng(-85, -90),
  new google.maps.LatLng(-85, 0),
  new google.maps.LatLng(-85, 90),
  new google.maps.LatLng(-85, 180),
  new google.maps.LatLng(0, 180),
  new google.maps.LatLng(85, 180)
];

function drawCircle(point, radius, dir) {
  var d2r = Math.PI / 180; // degrees to radians 
  var r2d = 180 / Math.PI; // radians to degrees 
  var earthsradius = 3963; // 3963 is the radius of the earth in miles
  var points = 64;

  // find the raidus in lat/lon 
  var rlat = (radius / earthsradius) * r2d;
  var rlng = rlat / Math.cos(point.lat() * d2r);

  var extp = new Array();
  if (dir == 1) {
    var start = 0;
    var end = points + 1
  } // one extra here makes sure we connect the ends
  else {
    var start = points + 1;
    var end = 0
  }
  for (var i = start;
    (dir == 1 ? i < end : i > end); i = i + dir) {
    var theta = Math.PI * (i / (points / 2));
    ey = point.lng() + (rlng * Math.cos(theta)); // center a + radius x * cos(theta) 
    ex = point.lat() + (rlat * Math.sin(theta)); // center b + radius y * sin(theta) 
    extp.push(new google.maps.LatLng(ex, ey));
    bounds.extend(extp[extp.length - 1]);
  }
  return extp;
}

var map;
function initialize() {
  // Create the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -95.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);
  var populationOptions = {
    strokeColor: '#000000',
    strokeOpacity: 0.8,
    strokeWeight: 0,
    fillColor: '#000000',
    fillOpacity: 0.35,
    map: map,
    paths: [outerbounds, drawCircle(citymap['newyork'].center, 10, -1)]
  };
  // Add the circle for this city to the map.
  cityCircle = new google.maps.Polygon(populationOptions);
  map.fitBounds(bounds);
  google.maps.event.addListener(map, 'center_changed', redrawPoly);

}

function redrawPoly() {
  // update the circle.
  cityCircle.setPaths([outerbounds, drawCircle(map.getCenter(), 10, -1)]);
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>

【讨论】:

  • 为什么圆周围有角?它看起来更像一个多边形,并不像应有的那样光滑
  • 它不是一个圆,它是一个边数可配置的多边形。如果您想要更多边,请更改数量(但请注意,更多边是更多处理)。
  • 像魅力一样工作!谢谢!只是一些我无法理解的参数。我希望地图放大更多,mapOptions 中的放大不再起作用。知道如何设置地图图层的缩放比例吗? “通过将洞的中心绑定到地图中心,可能可以让它变得不那么紧张”你能进一步解释一下吗?我没有太多的地图知识。再次感谢!
  • 要使用 mapOptions 设置地图中心和缩放,请删除代码中对 map.fitBounds 的调用。请参阅我的回答:How to set zoom level in google map
猜你喜欢
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多