每次我调用这个函数时,旧标记都会退缩而新标记会出现,有什么办法可以解决这个问题吗?
Every time i call this function the old markers are staying back and new markers are appearing is there any way to solve this?
为之前的多段线创建了新的线串,因此每次都有新的路线出现,但标记却在后面! for 循环在操作点创建标记。
https://i.stack.imgur.com/MeQ4h.jpg
function addManueversToMap(route){
var svgMarkup = '<svg width="18" height="18" ' +
'xmlns="http://www.w3.org/2000/svg">' +
'<circle cx="8" cy="8" r="8" ' +
'fill="#1b468d" stroke="white" stroke-width="1" />' +
'</svg>',
dotIcon = new H.map.Icon(svgMarkup, {anchor: {x:8, y:8}}),
i,
j;
// Add a marker for each maneuver
for (i = 0; i < route.leg.length; i += 1) {
for (j = 0; j < route.leg[i].maneuver.length; j += 1) {
// Get the next maneuver.
var maneuver = route.leg[i].maneuver[j];
// Add a marker to the maneuvers group
var marker = new H.map.Marker({
lat: maneuver.position.latitude,
lng: maneuver.position.longitude} ,
{icon: dotIcon});
marker.instruction = maneuver.instruction;
group.addObject(marker);
}
}
group.addEventListener('tap', function (evt) {
map.setCenter(evt.target.getGeometry());
openBubble(
evt.target.getGeometry(), evt.target.instruction);
}, false);
// Add the maneuvers group to the map
map.addObject(group);
}
您应该从组中删除之前添加的对象。
只需在方法addManueversToMap
的开头添加group.removeAll()
即可。
此外,为了获得更好的性能,您应该将创建 svgMarkup
和 dotIcon
的代码移出此方法。
为之前的多段线创建了新的线串,因此每次都有新的路线出现,但标记却在后面! for 循环在操作点创建标记。
https://i.stack.imgur.com/MeQ4h.jpg
function addManueversToMap(route){
var svgMarkup = '<svg width="18" height="18" ' +
'xmlns="http://www.w3.org/2000/svg">' +
'<circle cx="8" cy="8" r="8" ' +
'fill="#1b468d" stroke="white" stroke-width="1" />' +
'</svg>',
dotIcon = new H.map.Icon(svgMarkup, {anchor: {x:8, y:8}}),
i,
j;
// Add a marker for each maneuver
for (i = 0; i < route.leg.length; i += 1) {
for (j = 0; j < route.leg[i].maneuver.length; j += 1) {
// Get the next maneuver.
var maneuver = route.leg[i].maneuver[j];
// Add a marker to the maneuvers group
var marker = new H.map.Marker({
lat: maneuver.position.latitude,
lng: maneuver.position.longitude} ,
{icon: dotIcon});
marker.instruction = maneuver.instruction;
group.addObject(marker);
}
}
group.addEventListener('tap', function (evt) {
map.setCenter(evt.target.getGeometry());
openBubble(
evt.target.getGeometry(), evt.target.instruction);
}, false);
// Add the maneuvers group to the map
map.addObject(group);
}
您应该从组中删除之前添加的对象。
只需在方法addManueversToMap
的开头添加group.removeAll()
即可。
此外,为了获得更好的性能,您应该将创建 svgMarkup
和 dotIcon
的代码移出此方法。