搜尋

首頁  >  問答  >  主體

如何在Open Layers地圖上插入一個標記?

目前正在建立一個地圖應用程式。嘗試在互聯網上找到的所有方法來添加標記,但仍然不起作用。我是不是做錯了什麼?

這是我的Angular程式碼:

不知何故,使用Leaflet要容易得多,但我不想那麼早就放棄Open Layers。

export class MapComponent implements OnInit{
  
  map! : Map;
  marker! :any;
  center = fromLonLat([5.5697, 50.6330]);
  iconFeature = new Feature({
    geometry : new Point(fromLonLat([5.5697, 50.6330]))
  })
  
  ngOnInit(): void {

    this.map = new Map({
      view : new View({
        center : this.center,
        zoom : 0
      }),
      layers : [

         new LayerVector({
          source : new SourceVector({
            features : [this.iconFeature]
          }),
          style : new Style({
            image : new Icon({
              anchor : [0.5, 46],
              src : '../assets/istockphoto-1153114937-2048x2048-removebg-preview.png'
            })
          })
         }),
         new TileLayer({
           source : new OSM()
         })
      ],
      target :  "map"
    })   
  }

}

P粉099145710P粉099145710471 天前686

全部回覆(1)我來回復

  • P粉141911244

    P粉1419112442023-09-13 17:31:16

    can u try this example of code

    import { Map, View } from 'ol';
    import { Tile as TileLayer, Vector as LayerVector } from 'ol/layer';
    import { OSM, Vector as SourceVector } from 'ol/source';
    import Feature from 'ol/Feature';
    import Point from 'ol/geom/Point';
    import { fromLonLat } from 'ol/proj';
    import { Icon, Style } from 'ol/style';
    
    export class MapComponent implements OnInit{
      
      map!: Map;
      center = fromLonLat([5.5697, 50.6330]);
    
      ngOnInit(): void {
    
        const marker = new Feature({
          geometry: new Point(fromLonLat([5.5697, 50.6330]))
        });
    
        const markerLayer = new LayerVector({
          source: new SourceVector({
            features: [marker]
          }),
          style: new Style({
            image: new Icon({
              anchor: [0.5, 1],
              src: '../assets/istockphoto-1153114937-2048x2048-removebg-preview.png'
            })
          })
        });
    
        this.map = new Map({
          view: new View({
            center: this.center,
            zoom: 0
          }),
          layers: [
            new TileLayer({
              source: new OSM()
            }),
            markerLayer
          ],
          target: 'map'
        });
      }
    }

    回覆
    0
  • 取消回覆