javascript - 在 Google Earth Plug-in API 中将事件附加到 groundOverlay

标签 javascript google-earth-plugin

我正在尝试使用以下代码将鼠标事件附加到 groundOverlay 功能:

     var groundOverlay = ge.createGroundOverlay('');
        var icon = ge.createIcon('');
        icon.setHref("http://www.google.com/logos/earthday08.gif");
        groundOverlay.setIcon(icon);
        var latLonBox = ge.createLatLonBox('');
        latLonBox.setBox(48.80, 48.75, -121.77, -121.85, 0);
        groundOverlay.setLatLonBox(latLonBox);
        ge.getFeatures().appendChild(groundOverlay);    

          google.earth.addEventListener(groundOverlay, 'click', function(e) { 
                    e.preventDefault();
                    console.log("hello"); 
                    });

但是点击没有结果。有什么想法吗?

谢谢! 账单

最佳答案

由于 KmlGroundOverlays 尚未生成鼠标事件,另一种解决方法是从地球接收鼠标事件,然后确定鼠标是否在覆盖范围内(“点击测试”)。下面是使用 mouseMove 事件执行此操作的代码的起点。它生成两个重叠的叠加层(一个旋转)并将鼠标下方的叠加层带到表面(通过操纵 drawingOrder)。

它在自己的结构中维护有关叠加层的信息(而不是遍历 KML,这可能是可能的)。

我无法找到从 GE 获取旋转叠加层顶点位置的方法,因此旋转是使用笛卡尔近似在代码中完成的。此代码可能会在极点、边界或大多边形上中断。

一旦顶点已知,在鼠标移动时运行的 HitTest 将基于 How can I determine whether a 2D Point is within a Polygon? 上的精彩讨论。

所以这段代码说明了以下方面的起始想法:

  • 如何从叠加层合成鼠标事件(KML 叠加层可以生成鼠标事件之前的解决方法)
  • 如何近似旋转叠加层的顶点(以找到叠加层的位置)
  • 对叠加层进行 HitTest (来自上述引用资料)
  • 使用 drawOrder 在鼠标悬停时将重叠叠加层带到表面

http://jsfiddle.net/pudkg/ 有演示

这是代码和html:

<!DOCTYPE html>

<html>
<head>

<script type="text/javascript" src="https://www.google.com/jsapi"> </script>

<script type="text/javascript">
var ge;
var overlays = [];   // record information about overlays (filled by 'addOverlay')
var drawOrder = 0;   // drawOrder value of topmost overlay
google.load("earth", "1");

function Point (lat, lon) {
  this.lat = lat;
  this.lon = lon;
}

function Overlay (groundOverlay, points, drawOrder) {
  this.overlay = groundOverlay;  // KML object
  this.points = points;          // array of Points (vertices of overlay)
  this.drawOrder = drawOrder;    // integer, higest displayed topmost
}
Overlay.prototype.hitTest = function (lat, lon) {   // return true if lat/lon is within overlay
    // Based upon https://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test
    var isInside = false;
    var minLon = this.points[0].lon, maxLon = this.points[0].lon;
    var minLat = this.points[0].lat, maxLat = this.points[0].lat;
    for (var n = 1; n < this.points.length; n++) {
      var q = this.points[n];
      minLon = Math.min(q.lon, minLon);
      maxLon = Math.max(q.lon, maxLon);
      minLat = Math.min(q.lat, minLat);
      maxLat = Math.max(q.lat, maxLat);
    }
    if (lon < minLon || lon > maxLon || lat < minLat || lat > maxLat)
      return false;

    var i = 0, j = this.points.length - 1;
    for (i, j; i < this.points.length; j = i++)
      if ( (this.points[i].lat > lat) != (this.points[j].lat > lat) &&
          lon < (this.points[j].lon - this.points[i].lon) * (lat - this.points[i].lat) /
          (this.points[j].lat - this.points[i].lat) + this.points[i].lon )
        isInside = !isInside;
    return isInside;
  }

function init() {
  google.earth.createInstance('map3d', initCB, failureCB);
}

function initCB(e) {
  ge = e;
  ge.getWindow().setVisibility(true);

  var lat = 37.204193;
  var lon = -112.934429;
  var dlat = 0.003;
  var dlon = 0.005;
  var offset = 0.004;

  var la = ge.createLookAt('');  // position camera
  la.set(lat, lon, 0, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 30, 2000);
  ge.getView().setAbstractView(la);

  for (var i = 0; i < 2; i++)  // generate two overlays, overlapping; second one rotated
    addOverlay('http://www.google.com/logos/earthday08.gif',
        lat + dlat + offset*i, lat - dlat + offset*i,
        lon + dlon + offset*i, lon - dlon + offset*i, 30*i);
  // KML overlays can't (yet) generate mouse events, so look for events from globe
  google.earth.addEventListener(ge.getGlobe(), 'mousemove', function(event) {
    var lat = event.getLatitude();
    var lon = event.getLongitude();
    // show that a move event was received:
    document.getElementById('logMove').innerHTML = event.getLatitude();
    topmost = -1, zMax = 0;  // find topmost overlay
    for (var i = overlays.length - 1; i >= 0; i--)
      if (overlays[i].hitTest(lat, lon)) {  // if mouse is within overlays[i]
        document.getElementById('logHit').innerHTML = i + '; ' + overlays[i].drawOrder;
        if (overlays[i].drawOrder > zMax) {  // if this overlay is higher than any previous
          topmost = i;
          zMax = overlays[i].drawOrder;
        }
      }
    if ((topmost >= 0) && (overlays[topmost].drawOrder < drawOrder)) {
      // if in an overlay and it is buried, make it top-most
      overlays[topmost].overlay.setDrawOrder(++drawOrder);
      overlays[topmost].drawOrder = drawOrder;  // update local structure
    }
    document.getElementById('logOver').innerHTML = topmost + '; ' + zMax;
  });
}

function addOverlay(url, north, south, east, west, rotation) {
  var groundOverlay = ge.createGroundOverlay('');  // create overlay
  var icon = ge.createIcon('');
  icon.setHref(url);
  groundOverlay.setIcon(icon);
  var latLonBox = ge.createLatLonBox('');
  latLonBox.setBox(north, south, east, west, rotation);
  groundOverlay.setLatLonBox(latLonBox);
  groundOverlay.setDrawOrder(++drawOrder);
  ge.getFeatures().appendChild(groundOverlay);
  var points = [];  // figure out lat/lon of the corners of the overlay
  var sinTheta = Math.sin(rotation * Math.PI / 180.0);
  var cosTheta = Math.cos(rotation * Math.PI / 180.0);
  // rotation is about the center of the overlay; find midpoint:
  var midPoint = new Point((north + south) / 2, (west + east) / 2);
  // To do cartesian rotation, need to consider that the distance between
  // units of longitude diminish as one goes north, to zero at pole:
  var cosLat = Math.cos(midPoint.lat * Math.PI / 180.0);  // longitude compression factor
  west = (west - midPoint.lon) * cosLat, east = (east - midPoint.lon) * cosLat;
  north -= midPoint.lat, south -= midPoint.lat;
  // use cartesian rotation (good enough approximation for UI away from pole, boundaries)
  // after rotation, restore (expand) longitudes by compression factor
  points.push(new Point(midPoint.lat + west * sinTheta + north * cosTheta,
      midPoint.lon + (west * cosTheta - north * sinTheta) / cosLat));
  points.push(new Point(midPoint.lat + east * sinTheta + north * cosTheta,
      midPoint.lon + (east * cosTheta - north * sinTheta) / cosLat));
  points.push(new Point(midPoint.lat + east * sinTheta + south * cosTheta,
      midPoint.lon + (east * cosTheta - south * sinTheta) / cosLat));
  points.push(new Point(midPoint.lat + west * sinTheta + south * cosTheta,
      midPoint.lon + (west * cosTheta - south * sinTheta) / cosLat));
  overlays.push(new Overlay(groundOverlay, points, drawOrder));
}

function failureCB(errorCode) {
  alert("GE init fail");
}

google.setOnLoadCallback(init);
</script>
</head>

<body>

<div id=map3d style='height: 400px; width: 600px'></div>
<p>Mouse over the two overlays.  The one under the mouse should come to surface.</p>
<p>Latitude of mouse: <span id=logMove></span></p>
<p>Index of last overlay hit; its drawOrder: <span id=logHit></span></p>
<p>Index of topmost overlay; max drawOrder: <span id=logOver></span></p>

</body>
</html>

关于javascript - 在 Google Earth Plug-in API 中将事件附加到 groundOverlay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12956636/

相关文章:

javascript - Google 地球回调未触发

google-maps-api-3 - 使用 Maps V3 在 Google Earth API 中进行地理编码

javascript - 如何让nodejs返回对象 promise 以外的东西

javascript - 无法在 GridView 内单击按钮时打开弹出式 div

javascript - 我什么时候应该使用批处理并最终关闭 pg-promise 中的客户端连接?

javascript - setAbstractView() 防止 mousedown 事件传播到 KmlFeatures

javascript - 允许下载通过 JavaScript 和 Google Earth API 创建的 KML 文件

javascript - Google Earth api 允许使用未注册证书进行 https 连接

javascript - 有一个可以在 Chrome 和 Firefox 中运行但不能在 IE 中运行的 Javascript

javascript - 一个变量破坏了我的简单程序,为什么?