import * as ol from 'ol'
import * as style from 'ol/style'
import * as layer from 'ol/layer'
import * as source from 'ol/source'
import * as geom from 'ol/geom'
import * as proj from 'ol/proj'
import * as interaction from 'ol/interaction'
import * as coordinate from 'ol/coordinate'
import * as format from "ol/format";

const layerFlag = ['layerName', 'positionLayer']
export default function Location ({map, position = null, wkt = null, zoom = null, color = '#039ff3'}) {
    let _source
    const realLayer = map.getLayers().getArray().filter(v => v.get(layerFlag[0]) === layerFlag[1])
    if (realLayer[0]) {
        _source = realLayer[0].getSource()
        _source.clear()
    } else {
        _source = new source.Vector(); //图层数据源
        const _vector = new layer.Vector({
            zIndex: 9999,
            source: _source,
        });
        _vector.set(layerFlag[0], layerFlag[1])
        map.addLayer(_vector);
    }
    const feat = new format.WKT().readFeature(position ? `POINT(${position[0]} ${position[1]})` : wkt)
    const radius = 25
    const longRadius = radius * Math.SQRT2
    feat.setStyle([new style.Style({ //图层样式
        image: new style.RegularShape({
            stroke: new style.Stroke({
                color: color,
                width: 2,
                lineDash: [
                    (longRadius * 3) / 10,
                    (longRadius * 4) / 10,
                    (longRadius * 3) / 10,
                    0
                ]
            }),
            radius1: radius,
            rotation: Math.PI / (180 / 45),
            points: 4
        })
    })])
    _source.addFeature(feat)
    setTimeout(() => {
        // @ts-ignore
        feat?.getGeometry().setCoordinates([0, 0])
    }, 3000)
    // @ts-ignore
    map.getView().setCenter(feat?.getGeometry().getCoordinates())
    if (zoom) {
        map.getView().setZoom(zoom)
    }
}
