【问题标题】:Angular OpenLayers not showing custom pointer featureAngular OpenLayers 未显示自定义指针功能
【发布时间】:2020-07-17 00:21:24
【问题描述】:

我正在尝试为具有角度的 openlayers 和 openstreetmap 的地图进行精确定位,但精确定位未显示。

然而,地图确实会显示出来并且是功能性的。 为了显示地图本身,我首先遇到了需要在css中更改宽度和高度的问题,也许这是一样的?但我不知道如何设置图层样式。

import { Component, OnInit } from '@angular/core';
import Map from 'ol/Map';
import View from 'ol/View';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import { fromLonLat } from 'ol/proj.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import VectorSource from 'ol/source/Vector';
import {Icon, Style} from 'ol/style';
import OSM from 'ol/source/OSM';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
  map;
  testp;
  vectorSource;
  vectorLayer;
  rasterLayer;
  constructor() { }

  ngOnInit(): void {
    this.testp = new Feature({
      geometry: new Point(fromLonLat([3.7219431, 51.054633]))
    });

    this.testp.setStyle(new Style({
      image: new Icon(({
        color: '#8959A8',
        crossOrigin: 'anonymous',
        src: '../assets/car-parking.svg',
        imgSize: [20, 20]
      }))
    }));

    this.vectorSource = new VectorSource({
      features: [this.testp]
    });

    this.vectorLayer = new VectorLayer({
      source: this.vectorSource
    });

    this.map = new Map({
      target: 'map',
      layers: [ new TileLayer({
        source: new OSM()
      }), this.vectorLayer ],
      view: new View({
        center: fromLonLat([3.7219431, 51.054633]),
        zoom: 15,
      })
    });
  }

}

【问题讨论】:

    标签: angular typescript openlayers openstreetmap


    【解决方案1】:

    您的图标的路径可能存在问题。这对我有用:

    import Style from 'ol/style/Style';
    import Icon from 'ol/style/Icon';
    
    ...
    
    this.testp.setStyle(new Style({
      image: new Icon(({
        color: '#8959A8',
        crossOrigin: 'anonymous',
        src: 'assets/car-parking.svg',
        imgSize: [20, 20]
      }))
    }));
    

    【讨论】:

    • 奇怪,我改变了它,但它对我不起作用,会不会是别的东西?在另一个文件中?我对角度也很陌生
    • 嗯。到目前为止,您的代码看起来不错,它也对我有用。很难说为什么它也不适合你。你还有其他错误吗?
    • 我没有收到任何错误,标记只是没有显示在地图上。
    • 你的 angular.json 中有 "assets": ["src/favicon.png", "src/assets"] 吗?
    • 它是 SVG,改变它并且它工作。非常感谢您的时间和精力!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    相关资源
    最近更新 更多