openlayers 클릭feature 스타일 변경

16764 단어 openlayers

openlayers 클릭feature 스타일 변경

//  
var trackData = [
    {
        "lon":120.124512,
        "lat":35.978006,
        "data": [
            {"name":"zx"},
            {"age":16},
            {"position":"student"}
        ]
    },
    {
        "lon":120.651855,
        "lat":34.867905,
        "data": [
            {"name":"ss"},
            {"age":13},
            {"position":"student"}
        ]
    },
    {
        "lon":121.398926,
        "lat":33.779147,
        "data": [
            {"name":"zas"},
            {"age":19},
            {"position":"student"}
        ]
    },
    {
        "lon":121.530762,
        "lat":32.842674,
        "data": [
            {"name":"aas"},
            {"age":29},
            {"position":"student"}
        ]
    }
]
//  
var features = []
for(var i=0;i<trackData.length;i++){
    //  
    var coordinate = ol.proj.transform([trackData[i].lon,trackData[i].lat],'EPSG:4326','EPSG:3857')
    features[i] = new ol.Feature({
        geometry: new ol.geom.Point(coordinate)
    })
}
var map = new ol.Map({
    target: 'map',
    view: new ol.View({
        center: ol.proj.fromLonLat([116.359506, 35.302920]),
        zoom: 6
    }),
    layers:[
        //  
        new ol.layer.Tile({
            source: new ol.source.OSM()
        }),
        //  
        new ol.layer.Vector({
            source: new ol.source.Vector({
                features: features
            }),
            style: new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 5,
                    stroke: new ol.style.Stroke({
                        color:'#fff'
                    }),
                    fill: new ol.style.Fill({
                        color:'#f00'
                    })
                })
            })
        })
    ]
})

var selectClick = new ol.interaction.Select({
    //  
    condition: ol.events.condition.click,
    //  
    style: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 5,
            fill: new ol.style.Fill({
                color: '#09f'
            }),
            stroke: new ol.style.Stroke({
                color:'#fff',
                width: 2
            })
        })
    })
})

map.addInteraction(selectClick)

좋은 웹페이지 즐겨찾기