绘制完测地线后不能从谷歌地图上删除
Can't remove geodesic lines from google map after drawing them
我试图在绘制10后删除行,并且在each
循环的每次重复期间将每个标记和行推入数组,然后调用for
循环通过数组并使用setMap(null)
,但标记和行仍然留在那里。如何去除它们?我尝试在for
循环中嵌套each
,然后用dataArray.places.whatever
替换所有place.whatever
,并用var j
代替运行第二个for
循环,但仍然不起作用。
var dataArray = {
'places': [{
'destinationLng': '-74',
'sourceLng': '-91',
'destinationLat': '40',
'sourceLat': '38'
}, {
'destinationLng': '-104',
'sourceLng': '-99',
'destinationLat': '39',
'sourceLat': '19'
}, {
'destinationLng': '18',
'sourceLng': '-112',
'destinationLat': '59',
'sourceLat': '49'
}, {
'destinationLng': '-122',
'sourceLng': '-91',
'destinationLat': '37',
'sourceLat': '38'
}, {
'destinationLng': '-74',
'sourceLng': '-80',
'destinationLat': '40',
'sourceLat': '41'
}, {
'destinationLng': '-121',
'sourceLng': '123',
'destinationLat': '37',
'sourceLat': '10'
}]
};
function initialize() {
mapOptions = {
zoom: 2,
center: new google.maps.LatLng(40.6700, -73.9400),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
el = document.getElementById('map');
map = new google.maps.Map(el, mapOptions);
console.log(dataArray.places.length);
_.each(dataArray.places, function(place, index) {
var markers = [];
var geoLines = [];
window.setTimeout(function() {
start = new google.maps.LatLng(place.sourceLat, place.sourceLng);
endpt = new google.maps.LatLng(place.destinationLat, place.destinationLng);
coords = [start, endpt];
var color = '#393';
var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: color
};
source = new google.maps.Marker({
position: start,
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: color
},
});
markers.push(source);
geoLine = new google.maps.Polyline({
path: coords,
strokeColor: color,
strokeOpacity: 2,
strokeWeight: 2,
geodesic: true,
map: map,
icons: [{
icon: lineSymbol,
offset: '100%'
}]
});
geoLines.push(geoLine);
animateCircle();
animatePoly();
}, index * 2500);
for (var i = 0; i < place.length; i++) {
markers[i].setMap(null);
geoLines[i].setMap(null);
}
});
}
function animatePoly() {
var step = 0;
var numSteps = 250;
var timePerStep = 1;
var interval = setInterval(function() {
step += 1;
if (step > numSteps) {
clearInterval(interval);
} else {
var theMotion = google.maps.geometry.spherical.interpolate(start,endpt,step/numSteps);
geodesicPoly.setPath([start, theMotion]);
}
}, timePerStep);
}
google.maps.event.addDomListener(window, 'load', initialize);
删除行的循环在设置添加行的setTimeout
调用的循环之后立即运行。这些setTimeout
调用延迟然后运行包含的函数。所以时间轴是:
- 循环设置在各种延迟下创建折线
- 循环删除折线运行
-
setTimeout
函数运行,将折线添加到地图中。
在添加后删除它们,使用setTimeout
删除延迟大于添加折线(dataArray.places)的最大延迟的折线。长度* 2500应可)。
删除在之后添加的折线的代码片段:
var dataArray = {
'places': [{
'destinationLng': '-74',
'sourceLng': '-91',
'destinationLat': '40',
'sourceLat': '38'
}, {
'destinationLng': '-104',
'sourceLng': '-99',
'destinationLat': '39',
'sourceLat': '19'
}, {
'destinationLng': '18',
'sourceLng': '-112',
'destinationLat': '59',
'sourceLat': '49'
}, {
'destinationLng': '-122',
'sourceLng': '-91',
'destinationLat': '37',
'sourceLat': '38'
}, {
'destinationLng': '-74',
'sourceLng': '-80',
'destinationLat': '40',
'sourceLat': '41'
}, {
'destinationLng': '-121',
'sourceLng': '123',
'destinationLat': '37',
'sourceLat': '10'
}]
};
function initialize() {
mapOptions = {
zoom: 2,
center: new google.maps.LatLng(40.6700, -73.9400),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
el = document.getElementById('map');
map = new google.maps.Map(el, mapOptions);
console.log(dataArray.places.length);
var markers = [];
var geoLines = [];
dataArray.places.forEach(/* dataArray.places, */ function(place, index) {
window.setTimeout(function() {
start = new google.maps.LatLng(place.sourceLat, place.sourceLng);
endpt = new google.maps.LatLng(place.destinationLat, place.destinationLng);
document.getElementById('status').innerHTML += "line " + index + " start=" + start.toUrlValue(6) + " end=" + endpt.toUrlValue(6) + "<br>";
coords = [start, endpt];
var color = '#393';
var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: color
};
source = new google.maps.Marker({
position: start,
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: color
},
});
markers.push(source);
geoLine = new google.maps.Polyline({
path: coords,
strokeColor: color,
strokeOpacity: 2,
strokeWeight: 2,
geodesic: true,
map: map,
icons: [{
icon: lineSymbol,
offset: '100%'
}]
});
geoLines.push(geoLine);
// animateCircle();
// animatePoly();
}, index * 2500);
});
setTimeout(function() {
document.getElementById('status').innerHTML += "remove lines<br>";
for (var i = 0; i < dataArray.places.length; i++) {
markers[i].setMap(null);
geoLines[i].setMap(null);
}
}, dataArray.places.length * 2500);
}
function animateCircle() {
var count = 0;
window.setInterval(function() {
count = (count + 1) % 200;
var icons = line.get('icons');
icons[0].offset = (count / 2) + '%';
line.set('icons', icons);
}, 20);
}
function animatePoly() {
var step = 0;
var numSteps = 250;
var timePerStep = 1;
var interval = setInterval(function() {
step += 1;
if (step > numSteps) {
clearInterval(interval);
} else {
var theMotion = google.maps.geometry.spherical.interpolate(start, endpt, step / numSteps);
geodesicPoly.setPath([start, theMotion]);
}
}, timePerStep);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<div id="status"></div>
<div id="map" style="border: 2px solid #3872ac;"></div>
相关文章:
- 删除特定的谷歌地图标记
- 谷歌地图/Angular JS:地图更新并不总是反映数据,并且可以't删除标记
- 从谷歌地图中删除固定标记
- 从谷歌地图中删除 KML 叠加层
- 只需要谷歌地图上的 2 个标记.删除之前的任何内容
- 上一个标记删除在谷歌地图
- 从谷歌地图信息窗口中删除关闭图标
- 从谷歌地图drawingManager V3中删除多边形
- 删除/移动谷歌地图标记(jquery ui地图)
- 谷歌地图V3删除多段线覆盖
- 从谷歌地图xml中删除标记
- 如何从谷歌地图中删除一个标记
- 谷歌地图自动完成删除空格并在地址字符串中插入加号(+)
- 根据 id 从谷歌地图 API 中删除多个标记
- 谷歌地图删除控件
- 谷歌地图,从控件数组中删除控件
- 谷歌地图:删除所有圆圈
- 从谷歌地图删除一个以上的HeatmapLayer与一键
- Javascript谷歌地图删除图标
- 从谷歌地图删除自定义覆盖