HERE Java SDK多标记添加问题:二次标记不显示且多次点击崩溃
Hey there, let's tackle your HERE Maps problems head-on—second markers not appearing and crashes after repeated clicks are super common pitfalls, so let's break down the likely causes and fixes.
Common Causes & Solutions
1. You're Reusing the Same Marker Instance (Not Creating New Ones)
This is the #1 reason a second marker won't show up. If you're updating the coordinates of a single global marker instead of creating a new one each time, you'll just move the first marker instead of adding a new one.
Bad Example (What You Might Be Doing):
// Global marker variable being reused let singleMarker; function addMarkerToMap(lat, lng) { if (!singleMarker) { singleMarker = new H.map.Marker({ lat, lng }); map.addObject(singleMarker); } else { // This only moves the existing marker, not adds a new one singleMarker.setGeometry({ lat, lng }); } }
Fix: Create a New Marker Every Time
Make sure to instantiate a new H.map.Marker for each click, and store markers in an array if you need to manage them later:
// Array to track all markers (for easy cleanup later) const allMarkers = []; function addMarkerToMap(lat, lng) { // Create a brand new marker instance const newMarker = new H.map.Marker({ lat, lng }); // Add it to the map map.addObject(newMarker); // Store it in the array for future reference allMarkers.push(newMarker); }
2. Duplicate Event Listeners Causing Crashes
If you're binding the map's tap/click event inside your button's click handler, you'll add a new listener every time you press the button. After a few clicks, a single map tap will trigger addMarkerToMap dozens of times, flooding the map with markers and causing memory leaks that crash your app.
Bad Example:
document.getElementById("addMarkerBtn").addEventListener("click", () => { // This binds a NEW tap listener every time you click the button map.addEventListener("tap", (evt) => { const coords = map.screenToGeo(evt.currentPointer.viewportX, evt.currentPointer.viewportY); addMarkerToMap(coords.lat, coords.lng); }); });
Fix: Bind the Map Event Only Once
Bind the map's tap event once during initialization, or use a flag to prevent duplicate bindings:
let isMapTapBound = false; document.getElementById("addMarkerBtn").addEventListener("click", () => { if (!isMapTapBound) { // Bind the tap listener ONLY once map.addEventListener("tap", (evt) => { const coords = map.screenToGeo(evt.currentPointer.viewportX, evt.currentPointer.viewportY); addMarkerToMap(coords.lat, coords.lng); }); isMapTapBound = true; // Optional: Give feedback to the user, like "Tap map to add marker!" } });
3. Missing Map Instance or Garbage Collection Issues
If your map variable is scoped too tightly (e.g., inside an initialization function that finishes running), it might get garbage collected, leading to unexpected behavior or crashes.
Fix: Ensure Map is Accessible
Declare your map variable in a global scope (or a module-level scope if using ES modules) so it persists while your app is running:
// Global or module-level map variable let map; function initMap() { // Initialize the map as usual const platform = new H.service.Platform({ apikey: "YOUR_API_KEY" }); const defaultLayers = platform.createDefaultLayers(); map = new H.Map( document.getElementById("mapContainer"), defaultLayers.vector.normal.map, { center: { lat: 52.5159, lng: 13.3777 }, zoom: 10, } ); // Add map interactions, etc. }
4. Optional: Clean Up Markers to Prevent Memory Bloat
If you're adding lots of markers, periodically cleaning up unused ones can prevent crashes from memory overload:
function clearAllMarkers() { allMarkers.forEach((marker) => map.removeObject(marker)); // Reset the array to empty allMarkers.length = 0; }
Quick Checklist to Debug
- Am I creating a new
H.map.Markerfor each addition? - Is my map tap event bound only once?
- Is the
mapvariable accessible in the scope where I'm adding markers? - Am I tracking markers in an array to avoid memory leaks?
内容的提问来源于stack exchange,提问作者Loo Vincent

