【问题标题】:How to get marker options on marker click?如何在标记点击时获得标记选项?
【发布时间】:2019-08-22 22:27:51
【问题描述】:

我正在使用 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 当点击标记时我可能找不到任何关于它的信息。

【问题讨论】:

    标签: angular ionic3 google-maps-markers ionic4


    【解决方案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;
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-08
      • 2013-08-07
      • 2011-01-21
      • 1970-01-01
      • 2019-06-07
      • 2020-03-12
      • 2016-01-01
      • 1970-01-01
      • 2020-07-15
      相关资源
      最近更新 更多