22

我希望有人能指出我正确的方向。

我有一个从 XML 文件加载标记的谷歌地图,以及其他各种地图元素,效果很好。但是,我现在需要将 PNG 图像叠加到地图上。

我已经尝试了几个小时将 PNG 正确对齐到网站顶部,但找不到我需要的确切两个坐标(西南和东北)。有这样做的工具吗?理想情况下上传图像并拖动角落以适应,它会输出您需要的两个坐标(纬度/经度)?

我试过使用这个工具: http: //overlay-tiler.googlecode.com/svn/trunk/upload.html - 但它有三个联系点。

我也尝试过使用这个工具: http: //www.birdtheme.org/useful/customoverlay.html - 但是一旦上传到地图,你就不能调整图像的大小,你有机会点击西南标记!

我也可以使用 Google Earth 完美对齐 PNG,但我看不到输出 lat/lng 点的方法。

任何建议将不胜感激。

4

3 回答 3

26

这是 André Dion 解释的一个工作示例。

http://jsfiddle.net/4cWCW/3/

var overlay;

DebugOverlay.prototype = new google.maps.OverlayView();

function initialize() {
  var mapOptions = {
    zoom: 15,
    center: new google.maps.LatLng(40.743388,-74.007592)
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  var swBound = new google.maps.LatLng(40.73660837340877, -74.01852328);
  var neBound = new google.maps.LatLng(40.75214181, -73.99661518216243);
  var bounds = new google.maps.LatLngBounds(swBound, neBound);

   console.log(map);
  var srcImage = 'http://robincwillis.com/top/splash/04.jpg';

  overlay = new DebugOverlay(bounds, srcImage, map);

  var markerA = new google.maps.Marker({
            position: swBound,
            map: map,
            draggable:true
        });

    var markerB = new google.maps.Marker({
        position: neBound,
        map: map,
        draggable:true
    });

    google.maps.event.addListener(markerA,'drag',function(){

        var newPointA = markerA.getPosition();
        var newPointB = markerB.getPosition();
        var newBounds =  new google.maps.LatLngBounds(newPointA, newPointB);
        overlay.updateBounds(newBounds);
    });

  google.maps.event.addListener(markerB,'drag',function(){

      var newPointA = markerA.getPosition();
      var newPointB = markerB.getPosition();
      var newBounds =  new google.maps.LatLngBounds(newPointA, newPointB);
      overlay.updateBounds(newBounds);
  });

    google.maps.event.addListener(markerA, 'dragend', function () {

        var newPointA = markerA.getPosition();
        var newPointB = markerB.getPosition();
        console.log("point1"+ newPointA);
        console.log("point2"+ newPointB);
    });

    google.maps.event.addListener(markerB, 'dragend', function () {
        var newPointA = markerA.getPosition();
        var newPointB = markerB.getPosition();
        console.log("point1"+ newPointA);
        console.log("point2"+ newPointB);
    });

}

function DebugOverlay(bounds, image, map) {

  this.bounds_ = bounds;
  this.image_ = image;
  this.map_ = map;
  this.div_ = null;
  this.setMap(map);
}

DebugOverlay.prototype.onAdd = function() {

  var div = document.createElement('div');
  div.style.borderStyle = 'none';
  div.style.borderWidth = '0px';
  div.style.position = 'absolute';
  var img = document.createElement('img');
  img.src = this.image_;
  img.style.width = '100%';
  img.style.height = '100%';
  img.style.opacity = '0.5';
  img.style.position = 'absolute';
  div.appendChild(img);
  this.div_ = div;
  var panes = this.getPanes();
  panes.overlayLayer.appendChild(div);
};

DebugOverlay.prototype.draw = function() {
  var overlayProjection = this.getProjection();
  var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
  var div = this.div_;
  div.style.left = sw.x + 'px';
  div.style.top = ne.y + 'px';
  div.style.width = (ne.x - sw.x) + 'px';
  div.style.height = (sw.y - ne.y) + 'px';
};


DebugOverlay.prototype.updateBounds = function(bounds){
    this.bounds_ = bounds;
    this.draw();
};

DebugOverlay.prototype.onRemove = function() {
  this.div_.parentNode.removeChild(this.div_);
  this.div_ = null;
};

initialize();
于 2013-11-27T23:03:44.540 回答
9

不久前,我不得不为客户解决这个确切的问题。我的解决方案是简单地创建两个标记;每个LatLng覆盖层的LatLngBounds一个,当移动时,将更新覆盖层并将其记录LatLndBoundsconsole供我参考。我还添加了在拖动标记时显示的标线(垂直和水平线),以便更容易在视觉上定位叠加层。

我无法与您分享解决方案中的代码,但它涉及使用 aOverlayView以便LatLng通过MapCanvasProjection.

像您一样,我们不需要为最终用户提供此功能,因此我添加了一个“创作模式”,通过向debug解决方案的查询字符串提供一个参数来切换它。

于 2013-07-04T13:05:42.710 回答
2

我喜欢使用位于此处的那个:

http://googlemapsapi.blogspot.com/2007/05/v280-making-image-overlays-easy-with.html

它远非理想,它会生成 Google Maps 版本 2 API 代码,但您仍然可以使用对数/纬度坐标。

于 2013-10-16T14:36:42.213 回答