尝试为javascript的Google Earth API插件创建搜索框
我能够解析KMLFile并在GE API中加载,现在我必须按KML加载的地标名称嵌入搜索。

使用纬度和经度编码

var lookAt = ge.createLookAt('');
lookAt.set(point.y, point.x, 600, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 00, 0);
ge.getView().setAbstractView(lookAt);

是否可以使用Placemarker名称(字符串值)而不是使用LAT,LONG搜索LookAt

最佳答案

是的,根据您的设置,有几种方法可以执行此操作。

一种通用的方法是为每个地标赋予唯一的ID,然后使用该ID进行查看。

例如,假设您具有以下kml地标,并且已从URL http://localhost/foo.kml加载到api中

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark id="myPlacemark">
    <name>Myplacemark</name>
    <Point>
      <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
    </Point>
  </Placemark>
</kml>

然后,您可以像这样“查看”地标myPlacemark
var placemark = ge.getElementByUrl('http://localhost/foo.kml#myPlacemark');
var point = placemark.getGeometry();
var lookAt = ge.createLookAt('');
lookAt.set(point.getLatitude(), point.getLongitude(), 600, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 00, 0);
ge.getView().setAbstractView(lookAt);

可以做一个简单的函数,因此您只需传递正确的ID即可查看从具有ID的KML加载的任何点地标。
var myLookAt = function(id) {
  var placemark = ge.getElementByUrl(id);
  if('getGeometry' in placemark &&
    placemark.getGeometry().getType() == 'KmlPoint') {
    var point = placemark.getGeometry();
    var lookAt = ge.createLookAt('');
    lookAt.set(point.getLatitude(), point.getLongitude(), 600, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 00, 0);
    ge.getView().setAbstractView(lookAt);
  }
};

// useage
myLookAt('http://localhost/foo.kml#myPlacemark');

您显然可以更改myLookAt函数以查找lookAtcamera元素,或者也可以处理其他类型的对象-例如多几何体等。

10-06 01:26