HERE Maps クラスタリングデータプロバイダおよびレイヤーを初期化します。 次に、データポイントをクラスタ化されたレイヤーとしてマップに追加します。 たとえば、次のコード スニペット を参照してください。
// Create a data provider:var clusteredDataProvider =newH.clustering.Provider(dataPoints);// Create a layer that includes the data provider and its data points: var layer =newH.map.layer.ObjectLayer(clusteredDataProvider);// Add the layer to the map:
map.addLayer(layer);
// Create an icon to represent the noise points// Note that same icon will be used for all noise pointsvar noiseIcon =newH.map.Icon(noiseSvg,{size:{w:20,h:20},anchor:{x:10,y:10},});
// Create a clustered data provider and a theme implementationvar clusteredDataProvider =newH.clustering.Provider(dataPoints,{theme:{getClusterPresentation:function(cluster){// Use cluster weight to change the icon sizevar weight = cluster.getWeight(),// Calculate circle size
radius = weight *5,
diameter = radius *2,// Replace variables in the icon template
svgString = clusterSvgTemplate.replace(/\{radius\}/g, radius).replace(/\{diameter\}/g, diameter);// Create an icon// Note that we create a different icon depending from the weight of the cluster
clusterIcon =newH.map.Icon(svgString,{size:{w: diameter,h: diameter},anchor:{x: radius,y: radius}}),// Create a marker for the cluster
clusterMarker =newH.map.Marker(cluster.getPosition(),{icon: clusterIcon,// Set min/max zoom with values from the cluster, otherwise// clusters will be shown at all zoom levelsmin: cluster.getMinZoom(),max: cluster.getMaxZoom()});// Bind cluster data to the marker
clusterMarker.setData(cluster);return clusterMarker;},getNoisePresentation:function(noisePoint){// Create a marker for noise points:var noiseMarker =newH.map.Marker(noisePoint.getPosition(),{icon: noiseIcon,// Use min zoom from a noise point to show it correctly at certain zoom levelsmin: noisePoint.getMinZoom()});// Bind noise point data to the marker:
noiseMarker.setData(noisePoint);return noiseMarker;}}});
// Add an event listener to the Provider - this listener is called when a marker has been tapped:
clusteredDataProvider.addEventListener('tap',function(event){// Get the clicked marker - either a cluster or a noise point:var marker = event.target;// Get the marker implementing IResult interface:var point = marker.getData();// Is the marker a cluster?var isCluster = point.isCluster();if(isCluster){// Log all the noise points inside the cluster along with their geographical position:
console.group("Cluster was tapped");
point.forEachDataPoint((dataPoint)=>{
console.log("Noise point at "+ dataPoint.getPosition().lat +", "+ dataPoint.getPosition().lng);});
console.groupEnd();}else{// We tapped a noise point
console.log("Noise point at "+ point.getPosition().lat +", "+ point.getPosition().lng +" was tapped.");}},false);
const platform =newH.service.Platform({'apikey':'YOUR API KEY'});// Obtain the default map types from the platform object:const defaultLayers = platform.createDefaultLayers();// Instantiate (and display) a map:var map =newH.Map(
document.getElementById("map"),
defaultLayers.vector.normal.map,{zoom:7,center:{lat:51.509865,lng:-0.118092}});var dataPoints =[];
dataPoints.push(newH.clustering.DataPoint(51.4748,-0.368));//London City Airport (LCY)
dataPoints.push(newH.clustering.DataPoint(51.8897,0.2629));//London Biggin Hill Airport (BQH)
dataPoints.push(newH.clustering.DataPoint(51.4508,-0.1546));//London Heathrow Airport (LHR)
dataPoints.push(newH.clustering.DataPoint(51.3483,-0.5037));//London Gatwick Airport (LGW)
dataPoints.push(newH.clustering.DataPoint(51.1537,-0.1821));//London Luton Airport (LTN)
dataPoints.push(newH.clustering.DataPoint(51.7588,-0.262));//London Stansted Airport (STN)// SVG template to use for noise iconsvar noiseSvg ='<svg xmlns="http://www.w3.org/2000/svg" height="20px" width="20px"><circle cx="5px" cy="5px" r="5px" fill="green" /></svg>';// Create an icon to represent the noise points// Note that same icon will be used for all noise pointsvar noiseIcon =newH.map.Icon(noiseSvg,{size:{w:20,h:20},anchor:{x:10,y:10},});// SVG template to use for cluster iconsvar clusterSvgTemplate ='<svg xmlns="http://www.w3.org/2000/svg" height="{diameter}" width="{diameter}">'+'<circle cx="{radius}px" cy="{radius}px" r="{radius}px" fill="red" />'+'</svg>';// Create a clustered data provider and a theme implementationvar clusteredDataProvider =newH.clustering.Provider(dataPoints,{theme:{getClusterPresentation:function(cluster){// Use cluster weight to change the icon sizevar weight = cluster.getWeight(),// Calculate circle size
radius = weight *5,
diameter = radius *2,// Replace variables in the icon template
svgString = clusterSvgTemplate.replace(/\{radius\}/g, radius).replace(/\{diameter\}/g, diameter);// Create an icon// Note that we create a different icon depending from the weight of the cluster
clusterIcon =newH.map.Icon(svgString,{size:{w: diameter,h: diameter},anchor:{x: radius,y: radius}}),// Create a marker for the cluster
clusterMarker =newH.map.Marker(cluster.getPosition(),{icon: clusterIcon,// Set min/max zoom with values from the cluster, otherwise// clusters will be shown at all zoom levelsmin: cluster.getMinZoom(),max: cluster.getMaxZoom()});// Bind cluster data to the marker
clusterMarker.setData(cluster);return clusterMarker;},getNoisePresentation:function(noisePoint){// Create a marker for noise points:var noiseMarker =newH.map.Marker(noisePoint.getPosition(),{icon: noiseIcon,// Use min zoom from a noise point to show it correctly at certain zoom levelsmin: noisePoint.getMinZoom()});// Bind noise point data to the marker:
noiseMarker.setData(noisePoint);return noiseMarker;}}});// Create a layer that includes the data provider and its data points: var layer =newH.map.layer.ObjectLayer(clusteredDataProvider);// Add the layer to the map:
map.addLayer(layer);// Add an event listener to the Provider - this listener is called when a marker has been tapped:
clusteredDataProvider.addEventListener('tap',function(event){// Get the clicked marker - either a cluster or a noise point:var marker = event.target;// Get the marker implementing IResult interface:var point = marker.getData();// Is the marker a cluster?var isCluster = point.isCluster();if(isCluster){// Log all the noise points inside the cluster along with their geographical position:
console.group("Cluster was tapped");
point.forEachDataPoint((dataPoint)=>{
console.log("Noise point at "+ dataPoint.getPosition().lat +", "+ dataPoint.getPosition().lng);});
console.groupEnd();}else{// We tapped a noise point
console.log("Noise point at "+ point.getPosition().lat +", "+ point.getPosition().lng +" was tapped.");}},false);// MapEvents enables the event system// Behavior implements default interactions for pan/zoom (also on mobile touch environments)const behavior =newH.mapevents.Behavior(newH.mapevents.MapEvents(map));
次のステップ
他の Maps API for JavaScript の使用例の詳細について は、例を参照してください。