javascript - 从 div 中删除 google map 对象

标签 javascript google-maps

我已经在 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/

相关文章:

javascript - node.js promise : how to find out which iteration threw the exception in a . catch 语句?

javascript - 谷歌地图图 block 叠加

javascript - Angular.JS 接受表单输入并将该字符串转换为 HTML

java - 应用技术集所需的建议/建议

android - OnsenUI : Google Maps has 0 height when inside <ons-page> (Android 2. 3/仅限 Webkit)

javascript - 相当于 v3 Maps API 中的 GMap2.savePosition?

javascript - Google Maps API V3-子类google.maps.Marker:如何调用父方法?

javascript - 在 window.Unload 事件上将用户事件发布到服务器是个好主意吗?

javascript - javascript 错误 "Cannot read property ' foo' of null"和 "null is not an object"之间有什么区别

javascript - 第一次点击后 href 不运行,但 onclick 运行