如何获得标记点击的标记选项?

时间:2019-04-01 17:21:25

标签: angular ionic3 google-maps-markers ionic4

我正在使用Ionic 4,并且正在使用Google Maps API。我有几个这样建立的标记:

setRestaurantMarkers() {
    const markers = [];
    this.singletonService.restaurants.results.map(restaurant => {
        const restaurantPosition = {lat: restaurant['Restaurant'].Lat, lng: restaurant['Restaurant'].Long};

        this.markerOptions = new Marker({
            position: restaurantPosition,
            title: restaurant['Restaurant'].Name,
            category: restaurant['RestaurantCategory'].Name,
            map: this.map
        });

        this.marker = new google.maps.Marker(this.markerOptions);
        this.marker.addListener('click', (marker) => {
            // Set destination for navigate button
            this.destination = [marker.latLng.lat(), marker.latLng.lng()];
            console.log(marker.latLng.lat(), marker.markerOptions);
            this.markerClicked = true;
        });
    });
}

但是,当我单击标记时,我无法获得markerOptions的详细信息。我想在点击标记时使用这个markerOptions,因为我可能找不到关于它的任何东西。

1 个答案:

答案 0 :(得分:1)

在var中分配标记选项,而不是类属性

setRestaurantMarkers() {
    const markers = [];
    this.singletonService.restaurants.results.map(restaurant => {
        const restaurantPosition = {lat: restaurant['Restaurant'].Lat, lng: restaurant['Restaurant'].Long};

        const markerOptions = new Marker({ // make it as const
            position: restaurantPosition,
            title: restaurant['Restaurant'].Name,
            category: restaurant['RestaurantCategory'].Name,
            map: this.map
        });

        this.marker = new google.maps.Marker(markerOptions);
        this.marker.addListener('click', (marker) => {
            // Set destination for navigate button
            this.destination = [marker.latLng.lat(), marker.latLng.lng()];
            console.log(marker.latLng.lat(), markerOptions); // get directly
            this.markerClicked = true;
        });
    });
}