如何将Map-Click事件的Map-Marker从Google-Map组件传递到功能

发布于 2025-02-13 12:00:10 字数 2319 浏览 3 评论 0 原文

我在试图在其中打开地图输入窗口时面临一些问题 Google-map()通过此操作,在一个Angular 14项目中 指南()。

这是我的html:

<google-map #GoogleMap height="500px" width="100%" [zoom]="zoom" [center]="center" [options]="options">
<map-marker-clusterer [imagePath]="'./assets/images/mappa/m'">
    <map-marker #markerElem *ngFor="let s of strutture; let i = index;" 
        (mapClick)="openInfo(markerElem, s.Nome)"
        [position]="{lat: toNumber(s.Latitudine), lng: toNumber(s.Longitudine)}"
        [label]="getLabel(s.Prezzo.toString())" [icon]="getIcon()">
    </map-marker>
    <map-info-window>{{ infoContent }}</map-info-window>
</map-marker-clusterer>

这是我获得数据和功能的TS:

export class MappaRisultatiComponent implements OnInit {
@Input() strutture: Struttura[];
toNumber = toNumber;
center!: google.maps.LatLngLiteral;
icon: google.maps.Icon;
zoom: 8;
options: google.maps.MapOptions = {
    ...
}
@ViewChild(GoogleMap, { static: false }) map: GoogleMap
@ViewChild(MapInfoWindow, { static: false }) info: MapInfoWindow
infoContent = ''
openInfo(marker: MapMarker, content) {
    this.infoContent = content
    this.info.open(marker)
}

ngOnInit() {
    this.center = {
        lat: toNumber(this.strutture[0].Latitudine),
        lng: toNumber(this.strutture[0].Longitudine),
    }

    this.icon = {
        url: './assets/images/mappa/priceLabel.png'
    }
}

public getLabel(prezzo: string): google.maps.MarkerLabel {
    let ret: google.maps.MarkerLabel = {
        fontWeight: 'bold',
        text: prezzo + '€'
    }
    return ret;
}

public getIcon(): google.maps.Icon {
    let ret: google.maps.Icon = {
        url: './assets/images/mappa/priceLabel.png'
    }
    return ret;
}

重点是我无法编译,因为它给了我这个错误: “错误ts2345:类型'htmlelement'的参数不可分配给类型'mapmarker'的参数。 “

我试图弄清楚这一点,我确实知道该功能正在获取HTML组件,而不是标记,但我不知道该怎么办...我希望这只是一个新手错误。

只是为了帮助这是我的软件包文件:

package.json

i'm facing some issues while trying to open a map-info-window within the
google-map (https://www.npmjs.com/package/@angular/google-maps) component inside an Angular 14 project by following this
guidelines (https://timdeschryver.dev/blog/google-maps-as-an-angular-component#setup).

this is my html:

<google-map #GoogleMap height="500px" width="100%" [zoom]="zoom" [center]="center" [options]="options">
<map-marker-clusterer [imagePath]="'./assets/images/mappa/m'">
    <map-marker #markerElem *ngFor="let s of strutture; let i = index;" 
        (mapClick)="openInfo(markerElem, s.Nome)"
        [position]="{lat: toNumber(s.Latitudine), lng: toNumber(s.Longitudine)}"
        [label]="getLabel(s.Prezzo.toString())" [icon]="getIcon()">
    </map-marker>
    <map-info-window>{{ infoContent }}</map-info-window>
</map-marker-clusterer>

and this is my ts from where i get my data and functions:

export class MappaRisultatiComponent implements OnInit {
@Input() strutture: Struttura[];
toNumber = toNumber;
center!: google.maps.LatLngLiteral;
icon: google.maps.Icon;
zoom: 8;
options: google.maps.MapOptions = {
    ...
}
@ViewChild(GoogleMap, { static: false }) map: GoogleMap
@ViewChild(MapInfoWindow, { static: false }) info: MapInfoWindow
infoContent = ''
openInfo(marker: MapMarker, content) {
    this.infoContent = content
    this.info.open(marker)
}

ngOnInit() {
    this.center = {
        lat: toNumber(this.strutture[0].Latitudine),
        lng: toNumber(this.strutture[0].Longitudine),
    }

    this.icon = {
        url: './assets/images/mappa/priceLabel.png'
    }
}

public getLabel(prezzo: string): google.maps.MarkerLabel {
    let ret: google.maps.MarkerLabel = {
        fontWeight: 'bold',
        text: prezzo + '€'
    }
    return ret;
}

public getIcon(): google.maps.Icon {
    let ret: google.maps.Icon = {
        url: './assets/images/mappa/priceLabel.png'
    }
    return ret;
}

the point is i can't compile because it gives me this error:
"error TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'MapMarker'." from (mapClick)="openInfo(markerElem, s.Nome)"

i'm trying to figure it out, i do understand that the function is getting the html component instead of the marker but i don't know what else should i do... i'm using Angular from just a week I hope it's just a newbie error.

just to help this is my package file:

package.json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2025-02-20 12:00:10

似乎要更改我可以

使用的.TS

import { GoogleMap, MapInfoWindow, MapMarker } from '@angular/google-maps';

从HTML中

<map-marker #markerElem="mapMarker" *ngF...

使用的类型现在起作用

it seems like that to change the type i can just use

from the .ts

import { GoogleMap, MapInfoWindow, MapMarker } from '@angular/google-maps';

in the html

<map-marker #markerElem="mapMarker" *ngF...

now it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文