简体   繁体   English

处理3D模型上的点击事件

[英]Handle click event on 3d model

I want to put inside a GoogleEarth instance, new 3D Region - objects, at the ground level. 我想在地面上放置一个GoogleEarth实例(新的3D区域-对象)。 These objects should be clickable. 这些对象应该是可单击的。 At this live demo page: 在此实时演示页面上:

https://code.google.com/apis/ajax/playground/?exp=earth#creating_3d_models https://code.google.com/apis/ajax/playground/?exp=earth#creating_3d_models

.. I tried to modify executable code, to add simple mouse-event listener, to handle mouse clicks on 3D_Box object (placemark variables). ..我试图修改可执行代码,添加简单的鼠标事件侦听器,以处理3D_Box对象(地标变量)上的鼠标单击。

I can't get the code works. 我无法使代码正常工作。 I believe that it is possible: 我相信有可能:

google.earth.addEventListener(placemark, 'mousedown', function(event) {
    console.log(event);    
    console.log(event.getTarget().getType());
});

How?? 怎么样??

Custom 3D models appended to GE don't trigger those kind of events (unfortunately). 附加到GE的自定义3D模型不会触发此类事件(不幸的是)。 You have some workarounds to make that kind of behaviour work, depending on the level of precision you want: 您有一些变通办法可以使这种行为起作用,具体取决于所需的精度级别:

  • Draw a Placemark in the same place of the 3D model with just the name of the element and capture clicks - the user must know he has to click on the name, might not be intuitive 仅使用元素名称在3D模型的同一位置绘制地标并捕获点击-用户必须知道他必须单击名称,这可能不直观
  • Draw a Polygon in the same place of the 3D model - depending on the angle of the model you might not detect clicks 在3D模型的相同位置绘制多边形-根据模型的角度,您可能无法检测到点击
  • Draw multiple Polygons around the 3D model - heavier, but more precise 围绕3D模型绘制多个多边形-较重,但更精确

Regarding performance, if you only draw it once, it is just another element in GE, so it shouldn't be noticeable any issues, but if you have to constantly update its position than you will lose a bit of performance, but it will depend on the frequency of updates and the number of elements to manipulate. 关于性能,如果只绘制一次,它只是GE中的另一个元素,因此它不会引起任何问题,但是,如果您必须不断更新其位置,则可能会损失一些性能,但这取决于更新频率和要操纵的元素数量。

This is an example of creating a "Cube" around a 3D Model position: 这是一个围绕3D模型位置创建“多维数据集”的示例:

function createCube(lat, lon, offset, alt, altOffset, name) {
    var altitudeMode = window.ge.ALTITUDE_ABSOLUTE;
    //create the back polygon of the cube
    var backPoly = window.ge.createPolygon('');
    backPoly.setAltitudeMode(altitudeMode);

    var cubeBack = window.ge.createLinearRing('');
    cubeBack.setAltitudeMode(altitudeMode);
    // Square outer boundary.
    cubeBack.getCoordinates().pushLatLngAlt(lat - offset, lon - offset, alt); 
    cubeBack.getCoordinates().pushLatLngAlt(lat - offset, lon - offset, alt+altOffset); 
    cubeBack.getCoordinates().pushLatLngAlt(lat - offset, lon + offset, alt+altOffset); 
    cubeBack.getCoordinates().pushLatLngAlt(lat - offset, lon + offset, alt);
    backPoly.setOuterBoundary(cubeBack);

    //create the front polygon of the cube
    var frontPoly = window.ge.createPolygon('');
    frontPoly.setAltitudeMode(altitudeMode);

    var cubeFront = window.ge.createLinearRing('');
    cubeFront.setAltitudeMode(altitudeMode);
    // Square outer boundary.
    cubeFront.getCoordinates().pushLatLngAlt(lat + offset, lon - offset, alt);
    cubeFront.getCoordinates().pushLatLngAlt(lat + offset, lon - offset, alt+altOffset);
    cubeFront.getCoordinates().pushLatLngAlt(lat + offset, lon + offset, alt+altOffset);
    cubeFront.getCoordinates().pushLatLngAlt(lat + offset, lon + offset, alt);
    frontPoly.setOuterBoundary(cubeFront);

    //create the left polygon of the cube
    var leftPoly = window.ge.createPolygon('');
    leftPoly.setAltitudeMode(altitudeMode);

    var cubeLeft = window.ge.createLinearRing('');
    cubeLeft.setAltitudeMode(altitudeMode);
    // Square outer boundary.
    cubeLeft.getCoordinates().pushLatLngAlt(lat - offset, lon - offset, alt);
    cubeLeft.getCoordinates().pushLatLngAlt(lat - offset, lon - offset, alt+altOffset);
    cubeLeft.getCoordinates().pushLatLngAlt(lat + offset, lon - offset, alt+altOffset);
    cubeLeft.getCoordinates().pushLatLngAlt(lat + offset, lon - offset, alt);
    leftPoly.setOuterBoundary(cubeLeft);

    //create the right polygon of the cube
    var rightPoly = window.ge.createPolygon('');
    rightPoly.setAltitudeMode(altitudeMode);

    var cubeRight = window.ge.createLinearRing('');
    cubeRight.setAltitudeMode(altitudeMode);
    // Square outer boundary.
    cubeRight.getCoordinates().pushLatLngAlt(lat - offset, lon + offset, alt);
    cubeRight.getCoordinates().pushLatLngAlt(lat - offset, lon + offset, alt+altOffset);
    cubeRight.getCoordinates().pushLatLngAlt(lat + offset, lon + offset, alt+altOffset);
    cubeRight.getCoordinates().pushLatLngAlt(lat + offset, lon + offset, alt);
    rightPoly.setOuterBoundary(cubeRight);

    //create the multigeometry object
    var multiGeometry = window.ge.createMultiGeometry(''); 
    multiGeometry.getGeometries().appendChild(backPoly); 
    multiGeometry.getGeometries().appendChild(frontPoly); 
    multiGeometry.getGeometries().appendChild(leftPoly); 
    multiGeometry.getGeometries().appendChild(rightPoly);

    //create the cube placemark
    var cubePlacemark = window.ge.createPlacemark('');
    cubePlacemark.setGeometry(multiGeometry);

    //Create a style and set width and color of line and polygons 
    cubePlacemark.setStyleSelector(window.ge.createStyle('')); 
    var polyStyle = cubePlacemark.getStyleSelector().getPolyStyle(); 
    polyStyle.setFill(0);
    var lineStyle = cubePlacemark.getStyleSelector().getLineStyle(); 
    lineStyle.setWidth(1);

    lineStyle.getColor().set('012F2F2F');

    //append the placemark to the geplugin
    ge.getFeatures().appendChild(cubePlacemark);

    //set the cube name
    cubePlacemark.setName(name);

    /*
     * Click event listener
     * Show a menu with some nice options - For now its ugly
     */
    google.earth.addEventListener(cubePlacemark, 'click', function(event) {
        event.preventDefault();

        setTimeout(function() {
            if(console) console.log("Cube click");
            else alert("Cube click");
        },0);
    });//click
}

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM