Troubleshooting: Custom pointer feature not displaying in Angular OpenLayers

I am currently working on adding a pinpoint to a map using openlayers and openstreetmap within an Angular project, but unfortunately, the pinpoint is not visible.

Although the map itself is being displayed and functions correctly. Initially, I had to adjust the width and height in the CSS to display the map properly; could this issue be related? I am unsure how to style the layer appropriately.

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,
      })
    });
  }

}

Answer №1

It seems like there might be an issue with the path leading to your icon file. A solution that worked for me was:

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]
  }))
}));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining the time taken for code execution in Typescript by capturing console.timeEnd() as

Can the value of console.timeEnd() be captured as a variable in Typescript? I need it for debugging but am unable to view console outputs while testing on my phone. If not, is there a simple alternative method to achieve this without relying on external G ...

Expanding the Warnings of React.Component

When I create a new class by extending React.Component in the following manner: export default class App extends React.Component<any, any > { constructor (props: React.ReactPropTypes) { super(props); } // other code } I encountere ...

Crystal-clear TextField component in Office UI Fabric

Seeking advice on how to clear a masked text field from Office UI Fabric using a button. Does anyone have a solution for this? I attempted to set the value using a state, but unfortunately, it did not work as expected. ...

The creation of the Angular project was unsuccessful due to the outdated circular-json dependency

After attempting to create a new Angular project with the command: ng new hello-world I encountered an error message: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d6dcc7d6c0d9d4c798dfc6dadbf5859b809b8 ...

Steps to resolve the error message 'Uncaught Error: Unable to find all parameters for AuthService: (?)'

An issue is arising when the application is initiated. No errors are displaying in the terminal, but they appear in the browser console. The last action taken was importing JwtHelperService from '@auth0/angular-jwt'. The project involves Nodejs, ...

I'm looking for a way to create a Redux thunk action creator that will return a promise. How

In my code, I have a primary thunk that is triggered by a button click. Within this thunk, I need to invoke another thunk and ensure it completes before proceeding. The second thunk returns a promise. Below is an excerpt of the code in question: export f ...

Running `tns preview` is successful, however when executing `tns build android --

I recently developed several components using NativeScript/Angular and everything was functioning properly when using tns preview. However, after building the app using tns build android --bundle and installing the generated .apk on my mobile device, I no ...

Component designed to be reusable across multiple different Angular 8 applications

I have multiple components with similar logic. Take for example: import { Component, OnInit } from '@angular/core'; import { Rule } from '@models'; import { ConfirmationDialogComponent } from '@core'; import { RulesSaveCompo ...

Executing a function in a separate component [Angular]

I'm still learning Angular. This question on Stack Overflow is similar to what I'm facing, but it doesn't provide the answer I need. In my case, the two components are independent and not parent-child. They exist at the same directory level, ...

Is it possible to retrieve the HttpsError status from a Firebase function?

Within my firebase function, I deliberately throw an error with a specific status and message using the following code: throw new functions.https.HttpsError('permission-denied', 'Token does not match'); When I receive the server respo ...

Using *ngIf directive in Angular to define and initialize variables

When working with Angular 10, I utilize the *ngIf directive to establish a variable in my HTML template. Here is an example of how I do this: <div *ngIf="function() as foo"> <p>Header</p> <div *ngFor="let x of fo ...

Make sure you have the correct loader in place for dealing with this particular file type while working with Angular and LitElement

I have developed my application using angular 7. To improve productivity, I have incorporated components based on lit-element, specifically those from @lion by ING Bank. However, I encountered an error when running the application: ERROR in ./node_module ...

If you want to use the decorators plugin, make sure to include the 'decoratorsBeforeExport' option in your

Currently, I am utilizing Next.js along with TypeScript and attempting to integrate TypeORM into my project, like demonstrated below: @Entity() export class UserModel extends BaseEntity { @PrimaryGeneratedColumn('uuid') id: number } Unfortun ...

Mastering advanced horizontal list animations in Angular: A comprehensive guide

Need help with creating an advanced animation involving rows of items that can be removed by the user. The animation should shift all subsequent items to the left when one is removed, and if a spot is vacated in the leftmost position, the item should move ...

"What is the most effective way to utilize and integrate the `setValue` function from react-hook-form within a custom react hook

Struggling to pass setValue to a react hook? In my custom react hook, I need to set values in a form using react-hook-form's setValue. Yet, after migrating from v6 to v7, I'm having trouble figuring out the proper typing for this. This is how t ...

I am inputting an array in which I am certain about the type of the initial value, however, I am uncertain about the length of the array and the types of the remaining values

Within this function, the first argument must be a function while the remaining arguments could be of any type and their length is unknown. How should I define the array type to have a known first element but unknown length and types for the rest? // I a ...

Error: Attempting to access 'userService' property of undefined object without any value

I encountered an issue with the error message: TypeError: Cannot read properties of undefined (reading 'userService') This occurred while attempting to access a method from my UserService class in another class named ServicecenterController. In ...

VSCode encounters difficulty in locating the tsconfig within the Next.js application developed with NX

For the creation of a new NX workspace, I executed the following command: npx create-nx-workspace@latest --preset=next --packageManager=yarn --skipGit To further expand on this, I also generated a shared library using the command below: npx nx g @nrwl/nex ...

Why doesn't TypeScript perform type checking on a two-dimensional array?

Here's a simple code snippet I've been using to create a 2D array: type cell = { id: string; }; const board: cell[][]; board = Array(10) .fill("") .map((x) => Array(10).fill(ANY TYPE CAN GO HERE WHY?)); Oddly enough, when I popu ...

During the development phase, Angular 17 will encounter a 404 error when attempting to process paths containing dots (.) in the URL

export const routes: Routes = [ { path: 'foo.bar', component: ListComponent }, { path: 'foobar', component: ListComponent }, example.com/foobar is functioning correctly, but example.com/foo.bar leads to a 404 error. The issue ...