0

私のコードにはかなり奇妙な問題があります。kml ファイルを解析するために geoxml3 を使用しています。すべてのポリラインを正常に解析しますが、マーカーに到達すると、コンソールにはundefined. 奇妙な点は、ページをリロードするたびに正常に動作するのに、新しいタブで開くたびに再び壊れることです。さらに奇妙なことに、条件の前に右を置いて、console.logそれがポリラインかマーカーかを確認すると、ブラウザーのコンソールにmarkerプロパティがあることが示されます。

geoxml3 が必要とする useTheData 関数は次のとおりです。

function useTheData(doc){
console.log("Starts Parse");
console.log(doc[0].placemarks.length);  
for (var i = 0; i < doc[0].placemarks.length; i++){
    console.log("i: "+i+", placemark:");    
    console.log(doc[0].placemarks[i]); //here the .marker property exists in the console
    console.log(".marker:");
    console.log(doc[0].placemarks[i].marker); //here it says it's undefined!
    if(doc[0].placemarks[i].polyline){ //check if it's a polyline
        google.maps.event.addListener(doc[0].placemarks[i].polyline, 'click', select_option);
    }
    else{
        console.log("### i = "+i);
        console.log("1");
        console.log(doc[0].placemarks[i].marker); //here, the exact same object, doesn't have the marker property!
        console.log("2");
        google.maps.event.addListener(doc[0].placemarks[i].marker, 'click', select_option); //Because of that, the first time the page loads, it get's stuck in the function cuz it can't access the .marker
        console.log("3");
        doc[0].placemarks[i].marker.setIcon({
            url: "img/bola.png",
            scaledSize: new google.maps.Size(10, 10),
            anchor: new google.maps.Point(5, 5)
        });
        console.log("4");
    }
}
console.log("End Parse");
google.maps.event.addListener(map, 'click', select_option);
}   
4

1 に答える 1

1

これは、geoxml3 の polys ブランチと kmz ブランチの違いの 1 つが原因です。

geoxml3 の kmz ブランチにはアイコン用の img onload イベント ハンドラがあり、解析操作が完了するまでアイコンを使用できないことがあります。アイコンのサイズ変更はより適切に機能しますが、関数で見られるような問題が発生する可能性がありますafterParse

于 2016-04-30T19:09:33.907 に答える