2012-08-08 5 views
0

나는 스타일 (예를 들어 C 번호) 다음과 같은 방법을 만들고 할당합니다Google 어스 플러그인 - ID가있는 요소가 이미 있는지 확인하는 방법은 무엇입니까?

ID가 요소가 style1 이미 GE에있는 경우 내가 확인할 수있는 방법
// 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... 

? ge.createPlacemark("placemark1")에 전화하면 두 번째로 COM 오류가 발생합니다.

ge.getElementById("style1")으로 요소를 가져올 수 없습니다. 항상 null을 반환합니다.

+1

ge.getElementById ('style1')는 요소가 이미 GE에 존재하는지 확인합니다. exsist가 아닌 경우는 null를 돌려줍니다. 동일한 ID로 여러 장소 표시를 만들 때이 오류가 발생할 수 있습니다. – Matt

답변

0

두 가지가 있습니다. 먼저 액세서 getComputedStyle을 사용하면 개체 스타일 속성을 KMLStyle 개체로 가져올 수 있습니다. 당신이 필요로

dynamic placemarkStyle = placemark1.getComputedStyle(); 
string id = placemarkStyle.getId(); 

어쩌면 당신은

당신은 또한에 문자열로 유형 이름을 전달하여 특정 유형의 모든 요소의 배열을 얻을 수 있습니다 ... 스타일 객체를 참조하기 위해이 방법을 사용할 수 있습니다 getElementsByType. 내가 테스트 한 다음 그것을 작동합니다 - (이 검증되지 않은 있지만 ...) 이런 식으로 뭔가 당신이 당신의 코드 어딘가에 오류가 있어야합니다 귀하의 의견을 바탕으로

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; 
} 

편집 작업을해야 예상대로

// 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 
+0

장소 표시가 장소 표시에 추가되기 전에 스타일이 있는지를 알고 싶습니다. 장소 표시를 프로그래밍 방식으로 추가하는 동안 특정 스타일이 있다면이를 가져오고 그렇지 않으면 새로 만들려합니다. 'ge.getElementById ("style1")'및'ge.getElementsByType ('style')'을 사용하려고했습니다. 첫 번째는 항상 null을 반환하고 다른 하나는 길이가 0 인 컨테이너를 반환합니다. 하나 'var에 placemark1 = ge.createPlacemark ("placemark1")를 사용할 때 이와 비슷한 상황이 발생,' '(// ge.getFeatures()에 appendChild (placemark1).)' 'VAR placemark2 = ge.getElementById ("장소 표시 1"); // null' 하지만 기능에 스타일을 추가 할 수 없습니다. – Yuri

+0

당신이 여기서 뭐라고 말하려고하는지 모르겠습니다. 'ge.getElementById ("placemark1");을 호출하면 코드에 어딘가에 버그가 있어야합니다. id가 'placemark1' 인 장소 표시를 추가 한 후에 null이됩니다. 이는 의미가 없습니다. 컴파일 할 간단한 예제를 게시 할 수 있습니까? 추측하면 코드에 잘못된 구문 오류가 있거나 동일한 ID로 여러 요소를 만들었거나 액세스하려고하기 전에 해당 요소를 플러그인에 추가하지 않은 것입니다. – Fraser

+0

도 내 편집을 참조하십시오. – Fraser

관련 문제