Skip to content

How to capture a mouse over event on a <aol-feature> with OpenLayer  #261

@pascal101

Description

@pascal101

Is there a way to capture mouse over event on a aol-feature on a OSM map ? I have a device list (points) which are dysplayed on the map and my goal is to be able to show a pop-up once the mouse is over a device (aol-feature).
I'm beginner on OpenLayer librairie and on OSM map.

First I tried to follow this explaination with onClick event : #84 But it does not work for me.

I'm using Angular 7 with : bootstrap: 4.1.3, ngx-bootstrap: 5.6.1, ngx-openlayers : 0.8.22, ol: 6.4.3.

Here is my template code :

<aol-map [logo]="true" [width]="width" [height]="height" (onClick)="onClick($event)">
    <aol-interaction-default></aol-interaction-default>
    <aol-view [zoom]="zoom">
        <aol-coordinate [x]="longitude" [y]="latitude" [srid]="'EPSG:4326'"></aol-coordinate>
    </aol-view>
    <aol-layer-tile [opacity]="opacity">
        <aol-source-osm></aol-source-osm>
    </aol-layer-tile>
    <aol-layer-vector [opacity]="opacity">
        <aol-source-vector>    
            <aol-feature *ngFor="let device of devices; let i = index" (click)="setPopupContent(device)" [id]="i">                  
                <aol-geometry-point>
                    <aol-coordinate [x]="device.gpsLongitude" [y]="device.gpsLatitude" [srid]="'EPSG:4326'">
                    </aol-coordinate>
                </aol-geometry-point>
                <aol-style>
                    <aol-style-circle [radius]="10">
                        <aol-style-stroke [color]="'black'" [width]="width" ></aol-style-stroke>
                        <aol-style-fill [color]="'green'"></aol-style-fill>
                    </aol-style-circle>
                </aol-style>
            </aol-feature>
            <aol-overlay #popup *ngIf="popupContent.shown"> 
                   <aol-coordinate [x]="popupContent.gpsLongitude" [y]="popupContent.gpsLatitude" [srid]="'EPSG:4326'">      
                   </aol-coordinate>
                   <aol-content>
                        <div><p>hi</p></div>
                   </aol-content>
            </aol-overlay>
        </aol-source-vector>
    </aol-layer-vector>

Here is the typescript code :

import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core'
import { GeoLocationService } from 'src/app/common/services/geo-location.service'
import { marker } from './marker.image'
import * as olProj from 'ol/proj'
import { View } from 'ol'
import { HttpClient } from '@angular/common/http'
import { Device } from '../../common/model/device'
import { TooltipModule } from 'ngx-bootstrap/tooltip';
import { MapBrowserEvent, Feature } from 'ol';

@Component({
  selector: 'app-osm-view',
  templateUrl: './osm-view.component.html',
  styleUrls: ['./osm-view.component.css'],
  providers: [HttpClient, GeoLocationService]
})
export class OsmViewComponent implements OnInit {

    public features: Device[];
    public popupContent: { shown: boolean; feature: Device};

    constructor(private httpClient: HttpClient, 
            private geoLocationService: GeoLocationService,
            private reportManagementService: ReportManagementService
            ) {
        this.features = [];
        this.popupContent = {
          shown: false,
          feature: undefined
        };
        
    }
    
    public onClick(event: MapBrowserEvent) {
        console.log("onClick : this.event : ", event);
        console.log("onClick : this.devices : ", this.devices);
        event.map.forEachFeatureAtPixel(event.pixel, (feature: Feature) => {
          let featureId: number;
          featureId = feature.deviceId;
          console.log("onClick : feature.getId() : ", feature.deviceId);
          console.log("onClick : this.features[featureId] : ", this.features[featureId]);
          this.setPopupContent(this.features[featureId]);
        });
      }
    
      public setPopupContent(feature: Device) {
        this.popupContent.shown = true;
        this.popupContent.feature = feature;
        console.log("In setPopupContent method : this.popupContent.shown : ", this.popupContent.shown);
        console.log("In setPopupContent method : this.popupContent.feature : ", this.popupContent.feature);
      }

You can see in the browser console the result of my console.log trace

onClick : this.event :  Object { type: "click", target: {…}, map: {…}, frameState: {…}, originalEvent: pointerdown, pixel: (2) […], coordinate: (2) […], dragging: false, b: {…} }
onClick : this.devices :  Array [ {…}, {…} ]
onClick : feature.getId() :  undefined
onClick : this.features[featureId] :  undefined
In setPopupContent method : this.popupContent.shown :  true
In setPopupContent method : this.popupContent.feature :  undefined

So when I click on a device, the event is captured by no popup is displayed.
What is the problem ?

How to modify the code if I would like to do this on mouse over event ?
I tried this (mouseover)="mouseover($event)" but it does not work.

Please help. Thank you in advance...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions