I create and assign a style the following way (C# for example):
// ge — IGEPlugin instance
var placemark1 = ge.createPlacemark("placemark1");
var style1 = ge.createStyle("style1");
style1.getLineStyle().setWidth(2);
placemark1.setStyleSelector(style1);
// ... add placemark to ge features...
How can I check if the element with the ID style1
already exists in GE ? If I call ge.createPlacemark("placemark1")
, the second time I do it I get an COM
error.
I can't get the element with ge.getElementById("style1")
— it always returns null.
There are a couple of things, firstly there is an accessor getComputedStyle
that will allow you to get an objects style properties as a KMLStyle object.
dynamic placemarkStyle = placemark1.getComputedStyle();
string id = placemarkStyle.getId();
Maybe you can use this method to reference the style object as you require...
You can also obtain an array of all elements of a certain type by passing the type name as a string to getElementsByType
. Something like this should work (although it is untested...)
public bool DoesStyleExist(string id)
{
var styles = ge.getElementsByType('style');
int l = styles.getLength();
if(l == 0)
{
// no styles
return false;
}
for (var i = 0; i < l; ++i) {
var style = style.item(i);
var styleid = style.getId();
if(id == styleid)
{
// matching style id
return true;
}
}
// no match
return false;
}
EDIT
Based on your comment you must have an error somewhere in your code - I have tested the following and it works as expected.
// create the placemark
placemark = ge.createPlacemark('pm1');
var point = ge.createPoint('');
point.setLatitude(37);
point.setLongitude(-122);
placemark.setGeometry(point);
// add the placemark to the earth DOM
ge.getFeatures().appendChild(placemark);
var styleMap = ge.createStyleMap('');
// Create normal style for style map
var normalStyle = ge.createStyle('style1');
var normalIcon = ge.createIcon('icon1');
normalIcon.setHref('http://maps.google.com/mapfiles/kml/shapes/triangle.png');
normalStyle.getIconStyle().setIcon(normalIcon);
// Create highlight style for style map
var highlightStyle = ge.createStyle('style2');
var highlightIcon = ge.createIcon('icon2');
highlightIcon.setHref('http://maps.google.com/mapfiles/kml/shapes/square.png');
highlightStyle.getIconStyle().setIcon(highlightIcon);
styleMap.setNormalStyle(normalStyle);
styleMap.setHighlightStyle(highlightStyle);
// Apply stylemap to a placemark
placemark.setStyleSelector(styleMap);
alert(ge.getElementById('pm1')); // object
alert(ge.getElementById('style1')); // object
alert(ge.getElementById('style2')); // object
DoesStyleExist('style1'); // true
DoesStyleExist('style2'); // true
DoesStyleExist('foo'); // false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.