Atlaslog
Atlaslog / Examples / Cesium / Double Click Locate
Cesium · Runnable Example

Double Click Locate

Runnable Cesium example: Double Click Locate. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All Cesium examples
/demos/cesium-double-click-locate.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-double-click-locate.html
<!--
  示例:双击地球定位到目标
  要点:先移除 Viewer 默认的双击缩放,再绑自定义双击回调,flyTo 带缓动与透视机位
  依赖:CesiumJS 1.121 · jsDelivr CDN,无外部数据
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>双击定位</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
  html,body{height:100%;margin:0}
  body{background:#0e1116;color:#e6e8ec;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  #c{height:100%}
  .hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
       border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
       color:#a2a9b6;max-width:262px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:6px}
  .hud .val{color:#cfe4ff;font-family:Consolas,Menlo,monospace;font-size:12px}
  input[type=range]{width:100%}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
  <div><b>双击定位</b></div>
  <div class="row">双击地球表面,相机飞向该点</div>
  <div class="row">目标 <span class="val" id="target">—</span></div>
  <div class="row">飞行时长 <span class="val" id="dur">2.0</span> 秒</div>
  <div class="row"><input id="duration" type="range" min="0.5" max="5" step="0.5" value="2"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
  'use strict';

  var viewer = new Cesium.Viewer('c', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: false, timeline: false, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });

  var scene = viewer.scene;
  scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  scene.skyBox.show = false;
  scene.sun.show = false;
  scene.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;

  var targetEl = document.getElementById('target');
  var durEl = document.getElementById('dur');
  var duration = 2;

  document.getElementById('duration').addEventListener('input', function () {
    duration = parseFloat(this.value);
    durEl.textContent = duration.toFixed(1);
  });

  /* Viewer 自带双击缩放,先摘掉它,否则两个行为会同时触发 */
  viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
    Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
  );

  var marks = [];

  function mark(lng, lat, text) {
    var e = viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(lng, lat, 0),
      point: {
        pixelSize: 10,
        color: Cesium.Color.fromCssColorString('#3fe0b0').withAlpha(0.9),
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 2
      },
      label: {
        text: text,
        font: '500 12px Consolas, monospace',
        fillColor: Cesium.Color.fromCssColorString('#3fe0b0'),
        pixelOffset: new Cesium.Cartesian2(0, -22),
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      }
    });
    marks.push(e);
    if (marks.length > 5) viewer.entities.remove(marks.shift());
  }

  var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);

  handler.setInputAction(function (click) {
    var cartesian = viewer.camera.pickEllipsoid(click.position, scene.globe.ellipsoid);
    if (!Cesium.defined(cartesian)) return;

    var c = Cesium.Cartographic.fromCartesian(cartesian);
    var lng = Cesium.Math.toDegrees(c.longitude);
    var lat = Cesium.Math.toDegrees(c.latitude);

    targetEl.textContent = lng.toFixed(4) + ', ' + lat.toFixed(4);
    mark(lng, lat, lng.toFixed(3) + ', ' + lat.toFixed(3));

    viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(lng, lat - 0.012, 2600),
      orientation: {
        heading: Cesium.Math.toRadians(0),
        pitch: Cesium.Math.toRadians(-42),
        roll: 0
      },
      duration: duration,
      easingFunction: Cesium.EasingFunction.QUINTIC_IN_OUT,
      maximumHeight: 9000000
    });
  }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(108, 28, 15000000),
    orientation: { heading: 0, pitch: Cesium.Math.toRadians(-88), roll: 0 },
    duration: 0
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More Cesium examples

Cesium · EXAMPLEDrag Draw Polygon Cesium · EXAMPLEDrawing on Terrain Cesium · EXAMPLEDynamic Wall