ポリゴンを表示する

ソースコード

//YOLP操作のためのJavaScriptインターフェースオブジェクト
var YOLPSL;
//YOLP地図オブジェクト(Mapオブジェクト)
var ymap;


///(カセットを含む)YOLPSLロード完了イベント
/// map:YOLP地図オブジェクト
/// cassettes:カセットオブジェクトのディクショナリ(キーはカセット名)
function onLoadComplete(map, cassettes) {
    ymap = map;
    var slPlugin = document.getElementById("slPlugin");
    if (slPlugin && slPlugin.Content) {
        YOLPSL = slPlugin.Content.jsInterface;
        
        var SliderZoomControlHorizontal = YOLPSL.CreateInstance("SliderZoomControlHorizontal");
        ymap.AddControl(SliderZoomControlHorizontal);
        
        var LayerSetControl = YOLPSL.CreateInstance("LayerSetControl");
        ymap.AddControl(LayerSetControl);
        
        document.getElementById("addPolygon").onclick       = addPolygon;
        document.getElementById("addPolygons").onclick      = addPolygons100;
        document.getElementById("addCustomPolygon").onclick = addCustomPolygon;
        document.getElementById("rmvPolygon").onclick       = removePolygon;
        document.getElementById("rmvPolygons").onclick      = removePolygons100;
        document.getElementById("rmvCustomPolygon").onclick = removeCustomPolygon;
    }
}


//1ポリゴン追加
var polygon = null;
function addPolygon() {
    if (polygon != null) {
        return;
    }
    
    //地理座標リストを生成
    var llList = YOLPSL.CreateList("LatLng");
    for (var i=0; i<3; i++) {
        llList.push(getRandomLL());
    }
    
    polygon = YOLPSL.CreateInstanceWithArgs("YPolygon", llList);
    ymap.AddFeature(polygon);
    
}


//1ポリゴン削除
function removePolygon() {
    ymap.RemoveFeature(polygon);
    polygon = null;
}



//100ポリゴン追加
var polygonList = null;
function addPolygons100() {
    if (polygonList != null) {
        return;
    }
    
    //リスト生成
    polygonList = YOLPSL.CreateList("Feature");
    for (var i=0; i<100; i++) {
        
        //地理座標リストを生成
        var llList = YOLPSL.CreateList("LatLng");
        for (var j=0; j<3; j++) {
            llList.push(getRandomLL());
        }
        
        var l = YOLPSL.CreateInstanceWithArgs("YPolygon", llList);
        polygonList.push(l);
    }
    
    ymap.AddFeatures(polygonList);
    
}


//100個削除
function removePolygons100() {
    if (polygonList == null) {
        return;
    }
    for (var i=0; i<polygonList.length; i++) {
        ymap.RemoveFeature(polygonList[i]);
    }
    
    polygonList = null;
}



//カスタムポリゴン追加
var customPolygon = null;
function addCustomPolygon() {
    
    if (customPolygon != null) {
        return;
    }
    
    var foption = YOLPSL.CreateInstance("FeatureOption");
    foption.Title = "カスタムポリゴン";
    
    
    //ポリゴンオプション指定
    //線のオプション設定
    foption.StrokeStyle = YOLPSL.CreateInstanceWithArgs("YStyle", "0x00FF00", 2, 1.0);
    
    //破線設定(線4 - 空1 - 線2 - 空1)
    var dashArray = YOLPSL.CreateArray("Double", 4);
    dashArray[0] = 4.0;
    dashArray[1] = 1.0;
    dashArray[2] = 2.0;
    dashArray[3] = 1.0;
    foption.StrokeStyle.DashArray = dashArray;
    
    //塗りつぶし形式指定
    foption.FillStyle = YOLPSL.CreateInstanceWithArgs("YStyle", "0x00CC00", 2, 0.6);
    
    
    //地理座標リストを生成
    var llList = YOLPSL.CreateList("LatLng")
    for (var j=0; j<5; j++) {
        llList.push(getRandomLL());
    }
    
    
    customPolygon = YOLPSL.CreateInstanceWithArgs("YPolygon", llList, foption);
    ymap.AddFeature(customPolygon);
    
    
}


//カスタムポリゴン削除
function removeCustomPolygon() {
    ymap.RemoveFeature(customPolygon);
    customPolygon = null;
    
}



//画面内のランダムな地理座標オブジェクトを取得
function getRandomLL() {
    
    var geoBox = ymap.GetBounds();
    var min = geoBox.GetSouthWest();
    var max = geoBox.GetNorthEast();
    var dx = max.Lon - min.Lon;
    var dy = max.Lat - min.Lat;
    
    var lat = min.Lat + dy * Math.random();
    var lon = min.Lon + dx * Math.random();
    
    var latlng = YOLPSL.CreateInstanceWithArgs("LatLng", lat, lon);
    return latlng;
    
}
Webサービス by Yahoo! JAPAN