【问题标题】:Remove HTML markers from Google Map从 Google 地图中删除 HTML 标记
【发布时间】:2016-01-13 02:31:05
【问题描述】:

我正在使用 goole 地图叠加将 HTML 添加为标记,但我不知道如何使用 HTMLMarker.prototype.onRemove 方法删除所有添加的标记。

按照此代码http://jsfiddle.net/BCr2B/

var overlay;

function initialize() {
var myLatLng = new google.maps.LatLng(62.323907, -150.109291);
var mapOptions = {
    zoom: 11,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.SATELLITE
};

var gmap = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

function HTMLMarker(lat,lng){
    this.lat = lat;
    this.lng = lng;
    this.pos = new google.maps.LatLng(lat,lng);
}

HTMLMarker.prototype = new google.maps.OverlayView();
HTMLMarker.prototype.onRemove= function(){}

//init your html element here
HTMLMarker.prototype.onAdd= function(){
    div = document.createElement('DIV');
    div.className = "htmlMarker";
    div.innerHTML = "<i>HTML Marker</i>";
    var panes = this.getPanes();
    panes.overlayImage.appendChild(div);
}

HTMLMarker.prototype.draw = function(){
    var overlayProjection = this.getProjection();
    var position = overlayProjection.fromLatLngToDivPixel(this.pos);
    var panes = this.getPanes();
    panes.overlayImage.style.left = position.x + 'px';
    panes.overlayImage.style.top = position.y - 30 + 'px';
}

var htmlMarker = new HTMLMarker(62.323907, -150.109291);
htmlMarker.setMap(gmap);
}

谁能帮帮我?

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    您需要从 DOM 中移除 div 元素:

    HTMLMarker.prototype.onRemove = function () {
      div.parentNode.removeChild(div);
    }
    

    working fiddle

    代码 sn-p:

    var overlay;
    var htmlMarker;
    var gmap;
    
    function initialize() {
      var myLatLng = new google.maps.LatLng(62.323907, -150.109291);
      var mapOptions = {
        zoom: 11,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
      gmap = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
      htmlMarker = new HTMLMarker(62.323907, -150.109291);
      htmlMarker.setMap(gmap);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    
    function HTMLMarker(lat, lng) {
      this.lat = lat;
      this.lng = lng;
      this.pos = new google.maps.LatLng(lat, lng);
    }
    
    HTMLMarker.prototype = new google.maps.OverlayView();
    HTMLMarker.prototype.onRemove = function() {
      div.parentNode.removeChild(div);
    }
    
    //init your html element here
    HTMLMarker.prototype.onAdd = function() {
      div = document.createElement('DIV');
      div.className = "htmlMarker";
      div.innerHTML = "<i>HTML Marker</i>";
      var panes = this.getPanes();
      panes.overlayImage.appendChild(div);
    }
    
    HTMLMarker.prototype.draw = function() {
      var overlayProjection = this.getProjection();
      var position = overlayProjection.fromLatLngToDivPixel(this.pos);
      var panes = this.getPanes();
      panes.overlayImage.style.left = position.x + 'px';
      panes.overlayImage.style.top = position.y - 30 + 'px';
    }
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <input type="button" value="toggle" onclick="if(htmlMarker.getMap()!=null) {htmlMarker.setMap(null)}else{htmlMarker.setMap(gmap);}" />
    <div id="map_canvas"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 2015-03-24
      • 1970-01-01
      • 2013-04-10
      • 2015-04-09
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多