GE 只提供了很少的api供开发者使用,2014相比之前GE的api有了许多重要的改变。
首先是存取器上的变化。
1.getUrl()
由于谷歌地球中的KML文件需要网址导入,所以通过这个选择器可以找到该KML文件的地址
如果是由API直接创建的对象,那么它没有网址,这些对象的地址有#字符以及ID组成。
var placemarkUrl =placemark.getUrl();
2.getElementTypeByUrl()
如果系统抓取了 http://code.google.com/foo.kml,且该网址包含以下元素:
<kml> <Document> <Placemark id='elvis_was_here'> ... </Placemark> </Document> </kml>
要获取id为‘elvis_was_here’对象的类型
var type = ge.getElementByUrl( http://code.google.com/foo.kml#elvis_was_here);
3.getElementById()
var overlay = ge.createPhotoOverlay('graceland_panorama');
ge.getFeatures().appendChild(overlay);
var myPhotoOverlay = ge.getElementById('graceland_panorama');
4.getElementsByType()
通过以字符串的形式向 getElementsByType() 传递某个特定类型,您可以获取该类型中所有元素组成的数组。
var placemarks = ge.getElementsByType('KmlPlacemark'); for (var i = 0; i < placemarks.getLength(); ++i) { var placemark = placemarks.item(i); }
5.getComputedStyle()
以 KMLStyle 对象的形式返回对象的样式属性,从而将任意内嵌样式与从 setHref() 或 StyleUrl 导入的样式合并起来。
var placemark = ge.createPlacemark('');
placemark.setStyleUrl(
'http://kml-samples.googlecode.com/svn/trunk/kml/Style/styles.kml#blueIcons');
var icon = ge.createIcon('');
icon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
var style = ge.createStyle('');
style.getIconStyle().setIcon(icon);
style.getIconStyle().setScale(5);
placemark.setStyleSelector(style);
var placemarkStyle = placemark.getComputedStyle();
alert('Icon color: '
+ placemarkStyle.getIconStyle().getColor().get()
+ '\n'
+ 'Icon scale: '
+ placemarkStyle.getIconStyle().getScale());
本文介绍了2014年GE API的重要更新,包括新的存取器方法如getUrl(), getElementTypeByUrl(), getElementsByType()等,并展示了如何通过这些方法操作KML文件中的对象。
206

被折叠的 条评论
为什么被折叠?



