如何使用来自DOM元素

时间:2016-08-06 18:36:56

标签: jquery html google-maps dom overlay

我正在使用谷歌地图:

  1. 我想在此处运行类似于“重新加载标记”的操作:http://jsfiddle.net/upsidown/p646xmcr/ (注意:此特定部分与此问题并不完全相关,我只是想提供一些上下文,因为代码确实使用了上述链接中的一些元素)

  2. 我想从某些DOM元素的数据属性填充我的地图

  3. 我想使用HTML标记,而不是使用默认地图标记:Google Maps: Multiple Custom HTML Markers

  4. 我有什么:

    1. 小提琴:https://jsfiddle.net/yh2ucyq7/
    2. 标记正确加载,lat / lng正常工作
    3. 我无法弄明白

      1. 如何让每个HTML Marker的内部HTML正确填充,目前它只显示用于填充地图的最后一个DOM元素的文本
      2. 我的JavaScript:

            // Make Plot Points From Results DOM Elements
        function makeMapPlotPoints() {
        
            // Set marker from results list and create empty plot point array
            var mapPlotPointDOM = $(".listing-item");
            var mapPlotPointArr = [];
        
            $(mapPlotPointDOM).each(function() {
                if($(this).data("marker-lat") !== ''){
                    mapPlotPointArr.push([
                        $(this).data("marker-id"),
                        $(this).data("marker-lat"),
                        $(this).data("marker-lng"),
                    ]);
                }
            });
            setMarkers(mapPlotPointArr);
        };
        
        var map;
        var markers = []; // Create a marker array to hold markers
        
        //create empty LatLngBounds object
        var bounds = new google.maps.LatLngBounds();
        var infowindow = new google.maps.InfoWindow();
        var center = {
            lat: 0,
            lng: 0
        };
        
        var overlay;
        
        function setMarkers(locations) {
        
            for (var i = 0; i < locations.length; i++) {
        
                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 () {}
        
                var mapMarkerItem = locations[i];
                var myLatLng = new google.maps.LatLng(mapMarkerItem[1], mapMarkerItem[2]);
        
                //init your html element here
                HTMLMarker.prototype.onAdd = function () {
                    div = document.createElement('DIV');
                    div.style.position='absolute';
                    div.className = "htmlMarker";
                    div.innerHTML = mapMarkerItem[0]; // ### NOTE: This is returning the same value for all html markers
                    var panes = this.getPanes();
                    panes.overlayImage.appendChild(div);
                    this.div=div;
                }
        
                HTMLMarker.prototype.draw = function () {
                    var overlayProjection = this.getProjection();
                    var position = overlayProjection.fromLatLngToDivPixel(this.pos);
                    var panes = this.getPanes();
                    this.div.style.left = position.x + 'px';
                    this.div.style.top = position.y - 10 + 'px';
                }
        
                //to use it
                var htmlMarker = new HTMLMarker(mapMarkerItem[1], mapMarkerItem[2]);
                htmlMarker.setMap(map);
        
        
                // Set Map Bounds to Auto-center
                bounds.extend(myLatLng);
                map.fitBounds(bounds);
        
                // Push marker to markers array
                markers.push(htmlMarker);
        
                // Marker Info Window / Tooltip (not working)
                google.maps.event.addListener(htmlMarker, 'click', (function(htmlMarker, i) {
                    return function() {
                        infowindow.setContent(locations[i][4]);
                        infowindow.open(map, htmlMarker);
                    }
                })(htmlMarker, i));
        
            }
        }
        
        
        
        function reloadMarkers() {
        
            // Loop through markers and set map to null for each
            for (var i = 0; i < markers.length; i++) {
        
                markers[i].setMap(null);
            }
        
            // Reset the markers array
            markers = [];
        
            // Call set markers to re-add markers
            makeMapPlotPoints();
        }
        
        function initializeMap() {
        
            var mapOptions = {
                zoom: 2,
                center: new google.maps.LatLng(0, -30),
                mapTypeId: google.maps.MapTypeId.ROADMAP,
            }
        
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        
            makeMapPlotPoints();
        
        }
        
        initializeMap();
        

1 个答案:

答案 0 :(得分:2)

mapMarkerItem[0]始终相同(如您所发现的)。

div.innerHTML = mapMarkerItem[0];

您需要将唯一值传递给HtmlMarker的构造函数。

function HTMLMarker(lat, lng, text) {
  this.lat = lat;
  this.lng = lng;
  this.pos = new google.maps.LatLng(lat, lng);
  this.text = text;
}
HTMLMarker.prototype.onAdd = function() {
  var div = document.createElement('DIV');
  div.style.position = 'absolute';
  div.className = "htmlMarker";
  div.innerHTML = this.text;
  var panes = this.getPanes();
  panes.overlayImage.appendChild(div);
  this.div = div;
}

proof of concept fiddle

代码段

&#13;
&#13;
function HTMLMarker(lat, lng, text) {
  this.lat = lat;
  this.lng = lng;
  this.pos = new google.maps.LatLng(lat, lng);
  this.text = text;
}
HTMLMarker.prototype = new google.maps.OverlayView();
HTMLMarker.prototype.onRemove = function() {}

//init your html element here
HTMLMarker.prototype.onAdd = function() {
  var div = document.createElement('DIV');
  div.style.position = 'absolute';
  div.className = "htmlMarker";
  div.innerHTML = this.text;
  var panes = this.getPanes();
  panes.overlayImage.appendChild(div);
  this.div = div;
}

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

// Make Plot Points From Results DOM Elements
function makeMapPlotPoints() {

  // Set marker from results list and create empty plot point array
  var mapPlotPointDOM = $(".listing-item");
  var mapPlotPointArr = [];

  $(mapPlotPointDOM).each(function() {
    if ($(this).data("marker-lat") !== '') {
      mapPlotPointArr.push([
        $(this).data("marker-id"),
        $(this).data("marker-lat"),
        $(this).data("marker-lng"),
      ]);
    }
  });
  setMarkers(mapPlotPointArr);
};

var map;
var markers = []; // Create a marker array to hold markers

//create empty LatLngBounds object
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
var center = {
  lat: 0,
  lng: 0
};

var overlay;

function setMarkers(locations) {

  for (var i = 0; i < locations.length; i++) {

    var mapMarkerItem = locations[i];
    var myLatLng = new google.maps.LatLng(mapMarkerItem[1], mapMarkerItem[2]);

    //to use it
    var htmlMarker = new HTMLMarker(mapMarkerItem[1], mapMarkerItem[2], mapMarkerItem[0]);
    htmlMarker.setMap(map);


    // Set Map Bounds to Auto-center
    bounds.extend(myLatLng);
    map.fitBounds(bounds);

    // Push marker to markers array
    //markers.push(marker);
    markers.push(htmlMarker);
  }
}

function initializeMap() {

  var mapOptions = {
    zoom: 2,
    maxZoom: 18,
    minZoom: 2,
    center: new google.maps.LatLng(0, -30),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
  }

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  makeMapPlotPoints();

}

initializeMap();
&#13;
#listings,
.results-map-wrapper {
  width: 50%;
  float: left;
}
#map-canvas {
  width: 100%;
  height: 400px;
}
.htmlMarker {
  background: #f00000;
  color: #ffffff;
  height: 20px;
  width: 20px;
}
&#13;
<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"></script>
<div id="listings">
  <div class="listing-item" data-marker-id="01" data-marker-lng="0" data-marker-lat="0">
    Marker 01
  </div>
  <div class="listing-item" data-marker-id="02" data-marker-lng="0" data-marker-lat="-2">
    Marker 02
  </div>
  <div class="listing-item" data-marker-id="03" data-marker-lng="0" data-marker-lat="-4">
    Marker 03
  </div>
  <div class="listing-item" data-marker-id="04" data-marker-lng="0" data-marker-lat="2">
    Marker 04
  </div>
  <div class="listing-item" data-marker-id="05" data-marker-lng="0" data-marker-lat="4">
    Marker 05
  </div>
</div>

<div class="results-map-wrapper">
  <div id="map-canvas"></div>
</div>
&#13;
&#13;
&#13;