Google Maps API与MarkerClusterer:如何更改标记集群选项?如何自定义集群图标?

huangapple go评论54阅读模式
英文:

Google Maps API with MarkerClusterer: How do I change marker cluster options? How do I customize the cluster icon?

问题

这是使用Google Maps API创建Web视图页面的代码。我已完成实现,但关于未更新的标记聚类选项,我有一个问题。

这是CDN:

<script src="https://unpkg.com/@googlemaps/markerclusterer@latest"></script>

这是我的代码:

markers.push(marker);

const clusterStyles = [
  {
    url: 'https://developers.google.com/maps/documentation/javascript/examples/clusterer/m3.png',
    textColor: 'white',
    textSize: 14,
    width: 50,
    height: 50,
  },
];

const clusterOptions = {
  styles: clusterStyles,
  gridSize: 50,
  maxZoom: 15,
  minimumClusterSize: 3,
};

const markerCluster = new markerClusterer.MarkerClusterer({ map, markers, clusterOptions });

google.maps.event.addListener(markers, 'clusterclick', onClusterClick);

我期望标记聚类的颜色实际上是由控制聚类选项来更改的。

英文:

I'm creating a web view page using the Google Maps API. I've completed the implementation, but I have a question regarding the marker cluster options that are not being updated.

this is CDN

&lt;script src=&quot;https://unpkg.com/@googlemaps/markerclusterer@latest&quot;&gt;&lt;/script&gt;

this is my code

markers.push(marker);
			            
			            const clusterStyles = [
			            	  {
			            	    url: &#39;https://developers.google.com/maps/documentation/javascript/examples/clusterer/m3.png&#39;,
			            	    textColor: &#39;white&#39;,
			            	    textSize: 14,
			            	    width: 50,
			            	    height: 50,
			            	  },
			            	];

			            	const clusterOptions = {
			            	  styles: clusterStyles,
			            	  gridSize: 50,
			            	  maxZoom: 15,
			            	  minimumClusterSize: 3,
			            	};

			            	const markerCluster = new markerClusterer.MarkerClusterer({ map, markers, clusterOptions });
			            google.maps.event.addListener(markers, &#39;clusterclick&#39;, onClusterClick);

i expected color change to marker cluster actually cotrolling cluster option

答案1

得分: 1

我成功地定制了标记群集的标记,如下所示:

// 标记群集器
const yourCluster = new MarkerClusterer({
    map: yourMap,
    markers: yourMarkers, // 如果你想在同一地图上有多个不同自定义图标的群集,请使用 yourMarkers.slice(0, 3)
    renderer: {
        render: ({ markers, _position: position }) => {
            return new google.maps.Marker({
                position: {
                    lat: position.lat(),
                    lng: position.lng(),
                },
                label: {
                    text: String(markers.length),
                    color: "white",
                },
                icon: "/path/to/your/cluster-icon-1.png",
            });
        },
    },
});

在下面的完整示例中查看更多信息。

英文:

I successfully managed to customize the marker of a marker clusterer like this:

// Marker clusterer
const yourCluster = new MarkerClusterer({
    map: yourMap,
    markers: yourMarkers, // yourMarkers.slice(0, 3) if you want to have multiple clusters each with a different custom icon on the same map (e.g., set a clusterer for the first 3 markers)
    renderer: {
        render: ({ markers, _position: position }) =&gt; {
            return new google.maps.Marker({
                position: {
                    lat: position.lat(),
                    lng: position.lng(),
                },
                label: {
                    text: String(markers.length),
                    color: &quot;white&quot;,
                },
                icon: &quot;/path/to/your/cluster-icon-1.png&quot;,
            });
        },
    },
});

See the full example below.

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot; /&gt;
        &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
        &lt;title&gt;Document&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;your-google-maps-container&quot; style=&quot;width: 100%; height: 600px&quot;&gt;&lt;/div&gt;

        &lt;script type=&quot;module&quot;&gt;
            // Import MarkerClusterer
            import { MarkerClusterer } from &quot;https://cdn.skypack.dev/@googlemaps/markerclusterer@2.0.3&quot;;

            function initMap() {
                // Google Maps map
                const yourMap = new google.maps.Map(document.getElementById(&quot;your-google-maps-container&quot;), {
                    center: { lat: 46.1176208, lng: 14.8671412 },
                    zoom: 8.5,
                    disableDefaultUI: true,
                });

                // Create an array of alphabetical characters to be used to label the markers
                const labels = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;

                // Add markers to the map
                const yourMarkers = coordinates.map((position, i) =&gt; {
                    const label = labels[i % labels.length];

                    const markerOptions = {
                        position,
                        draggable: false,
                    };

                    // If you want to have different custom icons (e.g., set a custom icon for the first 3 markers)
                    /*  
                    if (i &lt; 3) {
                        markerOptions.icon = {
                            url: &quot;/path/to/your/icon-group-1.png&quot;,
                            scaledSize: new google.maps.Size(32, 32),
                        };
                    }
                    */

                    const marker = new google.maps.Marker(markerOptions);

                    return marker;
                });

                // Marker clusterer
                const yourCluster = new MarkerClusterer({
                    map: yourMap,
                    markers: yourMarkers, // yourMarkers.slice(0, 3) if you want to have multiple clusters each with a different custom icon on the same map (e.g., set a clusterer for the first 3 markers)
                    renderer: {
                        render: ({ markers, _position: position }) =&gt; {
                            return new google.maps.Marker({
                                position: {
                                    lat: position.lat(),
                                    lng: position.lng(),
                                },
                                label: {
                                    text: String(markers.length),
                                    color: &quot;black&quot;,
                                },
                                icon: &quot;/path/to/your/cluster-icon-1.png&quot;,
                            });
                        },
                    },
                });
            }

            // Coordinates
            const coordinates = [
                { lat: 45.51698897666811, lng: 13.569763482476969, info: &quot;Coordinate 1&quot; },
                { lat: 45.530799151329255, lng: 13.649157423409125, info: &quot;Coordinate 2&quot; },
                { lat: 45.52550345079001, lng: 13.597301289331448, info: &quot;Coordinate 3&quot; },
                { lat: 46.36572773115262, lng: 14.10740802891841, info: &quot;Coordinate 4&quot; },
                { lat: 46.421743820980616, lng: 15.856193372578861, info: &quot;Coordinate 5&quot; },
                { lat: 46.68333311555929, lng: 16.220405662581804, info: &quot;Coordinate 6&quot; },
                { lat: 46.64288069309906, lng: 16.0463909344671, info: &quot;Coordinate 7&quot; },
            ];

            window.initMap = initMap;
        &lt;/script&gt;

        &lt;!-- Google Maps API --&gt;
        &lt;script src=&quot;https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&amp;callback=initMap&quot; defer&gt;&lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 16:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定