简体   繁体   English

访问地标的Point信息以修改高度

[英]Accessor to the Point's information of a placemark to modify the altitude

I have a web working with google-earth API where I use multiple KML layers to show data. 我有一个使用google-earth API的网站,在其中我使用多个KML图层来显示数据。

With the interactions of the user it's necessary to modify some properties of the placemark like styles and names in order to change the placemarks display information. 通过用户的交互,有必要修改地标的某些属性,例如样式和名称,以更改地标的显示信息。

In some cases I would like to modify at the Altitude of the placemarks, information included in the Point information of the geometry specified at the KML. 在某些情况下,我想在地标的Altitude处修改包含在KML中指定的几何图形的Point信息中的信息。 Actually it is clampedToGround and I would like to modify it to any number in order to be in top of other elements. 实际上,它是clampedToGround ,我想将其修改为任何数字,以便位于其他元素的顶部。

This is an example of how I modify the placemarks information: 这是我如何修改地标信息的示例:

var pm = ge.getElementByUrl(UrlOfLayer#IdOfPlacemark);
pm.setStyleUrl(UrlOfLayer#IdOfStyleMap);
pm.setName(NewName);

This is working perfectly and all changes are applied without any problem. 这可以正常工作,所有更改都可以毫无问题地应用。 The structure of the KML placemarks is as it follows: KML地标的结构如下:

<Placemark id="1">
   <name>Name</name>
   <visibility>1</visibility>
   <open>0</open>
   <Point>
      <extrude>1</extrude>
      <tessellate>1</tessellate>
      <altitude>clampedToGround</altitude>
      <coordinates>1,1,1</coordinates>
   </Point>
   <styleUrl>#style</styleUrl>
</Placemark>

With this structure and the Placemark in Javascript got by getElementByUrl I can access the fields name, visibility, etc just by getName() , getVisibility() , etc. But, this doesn't work with Point information, since that is included inside the Geometry object of the placemark. 通过这种结构和getElementByUrl获得的Java地标,我可以仅通过getName()getVisibility()等访问字段名称,可见性等。但是,这不适用于Point信息,因为该信息包含在地标的Geometry对象。

I can access the Geometry information by using pm.getGeometry() , but after that, the 'Point' can't be accessed because as Google earth documentation sais: 我可以使用pm.getGeometry()访问Geometry信息,但是在此之后,由于Google Earth文档说:

The KmlGeometry object is an abstract object and cannot be used directly. KmlGeometry对象是一个抽象对象,不能直接使用。 It provides a placeholder object for all derived Geometry objects. 它为所有派生的Geometry对象提供一个占位符对象。

Looking at some examples where the Point information is used, it's only used to create a Point and after that include it at the Geometry, but in my case I can't create the point in Javascript because I can't get the coordinates information and the new point would overwrite it. 在一些使用Point信息的示例中,它仅用于创建Point,然后将其包含在Geometry中,但是在我的情况下,由于无法获取坐标信息而无法在Javascript中创建Point新点将覆盖它。

So, is there any accessor I haven't been able to find to accomplish this purpose? 那么,有没有我找不到的访问器来实现此目的?

The documentation is a bit unclear on that point; 关于这一点,文档尚不清楚。 it should perhaps say that KmlGeometry objects cannot be instantiated directly. 也许应该说不能直接实例化 KmlGeometry对象。 There are, in fact, several methods on a KmlGeometry object, which you can see in the documentation if you click through the "List of all members" link (the most relevant ones are inherited from KmlObject ). 实际上,在KmlGeometry对象上有几种方法,如果单击“所有成员的列表”链接, 则可以在文档中看到这些方法(最相关的方法是从KmlObject继承的)。

All the different geometry types inherit from KmlGeometry , but since you know that this geometry is a KmlPoint , you can just treat it as a KmlPoint (in other languages you might have to explicitly cast the variable, but, for better or worse, Javascript is more forgiving). 所有不同的几何类型都继承自KmlGeometry ,但是由于您知道该几何是KmlPoint ,因此可以将其视为KmlPoint (在其他语言中,您可能必须显式KmlPoint该变量,但是无论好坏,Javascript都是更多的宽容)。 In your case, you could do 就您而言,您可以

var point = pm.getGeometry();
point.setAltitude(newAltitudeValue);
point.setLatLng(...);

etc. You can use all the normal KmlPoint methods . 等。您可以使用所有常规的KmlPoint方法

Incidentally, if you don't know the specific type of geometry (for instance, if the geometry is returned as the target on a click event and could be anything), you can always ask it with getType inherited from KmlObject (in this case, pm.getGeometry().getType() would return 'KmlPoint' ), which you could then act upon. 顺便说一句,如果您知道几何的特定类型(例如,如果将几何作为click事件的目标返回,并且可以是任何东西),则可以始终使用从KmlObject继承的getType进行KmlObject (在这种情况下, pm.getGeometry().getType()将返回'KmlPoint' ),然后您可以对其进行操作。 Not a perfect reflection system, but it works. 这不是一个完美的反射系统,但它可以工作。

Addendum: Your example KML contains the line 附录:您的示例KML包含以下行

<altitude>clampedToGround</altitude>

which is not valid KML. 这是无效的KML。 What you probably mean is 你可能的意思是

<altitudeMode>clampToGround</altitudeMode>

Earth discards the element it doesn't understand (though a validator will show this error), but it doesn't especially matter in this case, since clampToGround is the default altitudeMode anyway. Earth会丢弃它不了解的元素(尽管验证器会显示此错误),但是在这种情况下它并不重要,因为无论如何, clampToGround是默认的heightMode。 This is the cause of any altitude changes being dropped, as clampToGround "indicates to ignore an altitude specification" ( per the docs ), so the point will be placed at ground level regardless of its altitude value. 这是导致任何海拔高度变化掉落的原因,如clampToGround “指示忽略海拔高度规范”( 根据文档 ),因此该点将放置在地面上,而与海拔高度值无关。

To be able to change the altitude, either change the KML you're feeding it, using relativeToGround or absolute as the altitudeMode, or use the API to change it, eg 为了能够更改海拔高度,请使用relativeToGroundabsolute作为海拔高度模式更改您正在喂食的KML,或者使用API​​进行更改,例如

pm.getGeometry().setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);

Then any altitude you set via pm.getGeometry().setAltitude(...) should work. 然后,您通过pm.getGeometry().setAltitude(...)设置的任何高度都应该起作用。

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

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