我已经在 div 标签中创建了一个 map 。稍后在某些用户操作中我想创建另一个车站的 map 并在其上绘制新标记。我应该如何清除同一 div 标签中的 map 对象?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>Google Maps Markers</title> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"
type="text/javascript"></script>
</head>
<body>
<div id="map-canvas" style="width: 650px; height: 250px;"></div>
<script type="text/javascript">
var map;
var DFWCenter = new google.maps.LatLng(32.9017,-97.0405770);
function showMap()
{
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 15,
center: DFWCenter,
mapTypeId: google.maps.MapTypeId.HYBRID
});
}
如果我执行map=null,它仍然不会从div标签中清除 map 。
最佳答案
不要破坏现有的 map ,重新使用它。使用新位置调用 map.setCenter()
和 map.setZoom()
(或使用新的所需边界调用 map.fitBounds()
) 。删除所有现有标记并创建新标记。
(注意:尝试销毁 map 实例并重新创建它时有内存泄漏的报告,请参阅 issue 3803 on the issues list ,可能还有其他)
关于javascript - 从 div 中删除 google map 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30014425/