Hi,
I have a map with a SymbolLayer on it with the ability to click on the map to add a symbol and also click on a symbol to see it's ID in a popup. I am encountering an issue where if I change the map style and add a symbol and click on that symbol, the symbol is a different type of object (i.e., doesn't have a data property in the code example below and therefore the popup doesn't not appear) versus the symbols added prior to changing the map style. You can change the map style again and the symbol will be of the expected type when clicked on.
I've tried to create a simple example of the issue below:
<!DOCTYPE html>
<html>
<head>
<title>Azure Maps - Changing Map Style and Adding Symbols to Existing Layer Issue</title>
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
</head>
<body>
<div id="myMap" style="width: 100%; height: 500px;"></div>
<div style="width: 100%;">
Steps:
<ol>
<li>Click map to add a few symbols</li>
<li>Click symbol to view ID in a popup</li>
<li>Change map style</li>
<li>Add another symbol</li>
<li>Click new symbol - popup won't appear (popups on other symbols still work)</li>
<li>Change map style and click symbol again and the popup displays</li>
<li>Repeat steps 4-6 with same results</li>
</div>
<script>
let id = 0;
let popup;
// Initialize the map
const map = new atlas.Map('myMap', {
center: [-118.2437, 34.0522], // Los Angeles
zoom: 10,
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: 'YOUR_AZURE_MAPS_SUBSCRIPTION_KEY'
}
});
map.events.add('ready', () => {
map.controls.add([new atlas.control.StyleControl()], {
position: atlas.ControlPosition.TopRight
});
const dataSource = new atlas.source.DataSource();
map.sources.add(dataSource);
const symbolLayer = new atlas.layer.SymbolLayer(dataSource, null, {
iconOptions: {
image: 'pin-round-darkblue', // Default Azure Maps icon
anchor: 'center'
}
});
map.layers.add(symbolLayer);
// Add a click event to the map
map.events.add('click', (e) => {
if (e.shapes && e.shapes.length > 0 && Object.hasOwn(e.shapes[0], 'data')) {
const shape = e.shapes[0];
const properties = shape.getProperties();
popup.setOptions({
content: `<div style='padding:16px 4px 4px 4px'><strong>ID:</strong> ${properties.id}</div>`,
position: shape.getCoordinates()
});
popup.open(map); // Opens the popup at the symbol's location
return;
}
if (e.position) {
const newPoint = new atlas.data.Feature(new atlas.data.Point(e.position), {
id: id++
});
dataSource.add(newPoint); // Add the new point to the data source
}
});
popup = new atlas.Popup({
position: [0, 0], // Placeholder position
pixelOffset: [0, -18] // Adjusts the position of the popup
});
});
</script>
</body>
</html>