在Google Maps api v3中制作可拖动的部分

时间:2015-08-02 16:12:44

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

是否可以在Google地图上绘制一个圆圈,只制作内部零件位置/拖动整个地图?

我尝试了这个和其他方法,但没有成功。 Beneath是我想要实现的快速photoshop示例。

Example screenshot

1 个答案:

答案 0 :(得分:2)

放置一个洞的一个选择"在地图的中心,基于对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);
&#13;
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>
&#13;
&#13;
&#13;