【问题标题】:Google Maps v3 custom marker icon is in center and does not keep its position on mapGoogle Maps v3 自定义标记图标位于中心,并且不会保持其在地图上的位置
【发布时间】:2020-05-22 13:44:44
【问题描述】:

我正在尝试实现 FontAwesome 图标作为 Google 地图上的标记。到目前为止,我已经成功了,但目前图标位于屏幕中间,无论如何它们都会呆在那里(他们拒绝在专用位置上)。

JsFiddle处的代码示例

JS代码:

function FontAwesomeMarker(latlng, map, args) {
  this.latlng = latlng;
  this.args = args;
  this.setMap(map);
}

FontAwesomeMarker.prototype = new google.maps.OverlayView();

FontAwesomeMarker.prototype.draw = function() {
  var self = this,
    panes = this.getPanes(),
    marker = this.marker;

  if (!marker) {
    marker = this.marker = document.createElement('div');
    marker.className = 'marker';

    var icon = document.createElement('i');
    icon.className = 'fa fa-' + this.args.icon;
    icon.style.fontSize = this.args.fontSize;
    icon.style.color = this.args.color;
    icon.anchorPoint = new google.maps.Point(0, 49)
    marker.appendChild(icon);

    var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
    if (point) {
      marker.style.left = (point.x - 25) + 'px';
      marker.style.top = (point.y - 25) + 'px';
    }

    google.maps.event.addDomListener(marker, "click", function(event) {
      alert('You clicked on a custom marker!');
      google.maps.event.trigger(self, "click");
    });

    panes.overlayImage.appendChild(marker);
  }
};

FontAwesomeMarker.prototype.remove = function() {
  if (this.marker) {
    this.marker.parentNode.removeChild(this.marker);
    this.marker = null;
  }
};

FontAwesomeMarker.prototype.getPosition = function() {
  return this.latlng;
};

function initialize() {
  var myLatlng = new google.maps.LatLng(-33.9, 151.2),
    mapOptions = {
      zoom: 15,
      center: myLatlng,
      disableDefaultUI: true
    };

  var map = new google.maps.Map(document.getElementById('map'), mapOptions);
  var markers = [{
      latLan: new google.maps.LatLng(-33.9, 151.2),
      icon: 'cutlery',
      color: '#346698',
      fontSize: '35px'
    },
    {
      latLan: new google.maps.LatLng(-33.8939, 151.207333),
      icon: 'anchor',
      color: '#B90C13',
      fontSize: '35px'
    },
    {
      latLan: new google.maps.LatLng(-33.895, 151.195),
      icon: 'automobile',
      color: '#39A00F',
      fontSize: '35px'
    },
    {
      latLan: new google.maps.LatLng(-33.905, 151.195),
      icon: 'headphones',
      color: '#000',
      fontSize: '35px'
    },
    {
      latLan: new google.maps.LatLng(-33.9039, 151.207333),
      icon: 'child',
      color: '#26C2C3',
      fontSize: '35px'
    },
  ]

  markers.forEach(function(item) {
    new FontAwesomeMarker(
      item.latLan,
      map, {
        icon: item.icon,
        color: item.color,
        fontSize: item.fontSize
      }
    );
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

我在这里做错了什么?

【问题讨论】:

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


    【解决方案1】:

    你不见了marker.style.position = 'absolute';

    if (!marker) {
      marker = this.marker = document.createElement('div');
      marker.className = 'marker';
      marker.style.position = 'absolute';
      // <snip>
    

    要让标记随地图一起拖动,您还需要将draw 方法分开,将其中的一部分放入onAdd 方法中(请参阅相关问题):

    FontAwesomeMarker.prototype.onAdd = function() {
          marker = this.marker = document.createElement('div');
          marker.className = 'marker';
        marker.style.position = 'absolute';
    
          var icon = document.createElement('i');
          icon.className = 'fa fa-' + this.args.icon;
          icon.style.fontSize = this.args.fontSize;
          icon.style.color = this.args.color;
        icon.anchor = new google.maps.Point(0, 49)
          marker.appendChild(icon);
        var panes = this.getPanes();
          panes.overlayImage.appendChild(marker);
    }
    
    FontAwesomeMarker.prototype.draw = function() {
        var self = this,
        marker = this.marker;
    
        var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
            if (point) {
              marker.style.left = (point.x - 25) + 'px';
              marker.style.top = (point.y - 25) + 'px';
            }
    
          google.maps.event.addDomListener(marker, "click", function(event) {
            alert('You clicked on a custom marker!');           
            google.maps.event.trigger(self, "click");
          });
    
    };
    

    来自 HTMLMarker 上的相关问题:Adding Custom Markers (HTMLMarkers) to Clustering

    proof of concept fiddle

    function FontAwesomeMarker(latlng, map, args) {
      this.latlng = latlng;
      this.args = args;
      this.setMap(map);
    }
    
    FontAwesomeMarker.prototype = new google.maps.OverlayView();
    
    FontAwesomeMarker.prototype.onAdd = function() {
      marker = this.marker = document.createElement('div');
      marker.className = 'marker';
      marker.style.position = 'absolute';
    
      var icon = document.createElement('i');
      icon.className = 'fa fa-' + this.args.icon;
      icon.style.fontSize = this.args.fontSize;
      icon.style.color = this.args.color;
      icon.anchor = new google.maps.Point(0, 49)
      marker.appendChild(icon);
      var panes = this.getPanes();
      panes.overlayImage.appendChild(marker);
    
      google.maps.event.addDomListener(marker, "click", function(event) {
        alert('You clicked on a custom marker!');
        google.maps.event.trigger(self, "click");
      });
    }
    FontAwesomeMarker.prototype.draw = function() {
      var self = this,
        marker = this.marker;
    
      var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
      if (point) {
        marker.style.left = (point.x - 25) + 'px';
        marker.style.top = (point.y - 25) + 'px';
      }
    };
    
    FontAwesomeMarker.prototype.remove = function() {
      if (this.marker) {
        this.marker.parentNode.removeChild(this.marker);
        this.marker = null;
      }
    };
    
    FontAwesomeMarker.prototype.getPosition = function() {
      return this.latlng;
    };
    
    function initialize() {
      var myLatlng = new google.maps.LatLng(-33.9, 151.2),
        mapOptions = {
          zoom: 15,
          center: myLatlng,
          disableDefaultUI: true
        };
    
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);
      var markers = [{
          latLan: new google.maps.LatLng(-33.9, 151.2),
          icon: 'cutlery',
          color: '#346698',
          fontSize: '35px'
        },
        {
          latLan: new google.maps.LatLng(-33.8939, 151.207333),
          icon: 'anchor',
          color: '#B90C13',
          fontSize: '35px'
        },
        {
          latLan: new google.maps.LatLng(-33.895, 151.195),
          icon: 'automobile',
          color: '#39A00F',
          fontSize: '35px'
        },
        {
          latLan: new google.maps.LatLng(-33.905, 151.195),
          icon: 'headphones',
          color: '#000',
          fontSize: '35px'
        },
        {
          latLan: new google.maps.LatLng(-33.9039, 151.207333),
          icon: 'child',
          color: '#26C2C3',
          fontSize: '35px'
        },
      ]
    
      markers.forEach(function(item) {
        new FontAwesomeMarker(
          item.latLan,
          map, {
            icon: item.icon,
            color: item.color,
            fontSize: item.fontSize
          }
        );
      });
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map {
      height: 100%;
    }
    
    body {
      margin: 0;
    }
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
    <div id="map"></div>

    【讨论】:

    • 确实如此。根据文档,remove 也应该是 onRemove
    • 这也在我的回答中引用的相关问题中得到解决。可能只有在将它们从地图中删除时才需要。
    • remove 似乎按原样工作:jsfiddle.net/geocodezip/j62swmcf/3
    猜你喜欢
    • 2012-03-08
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2016-11-06
    • 2012-02-23
    • 2016-09-03
    • 2012-01-28
    相关资源
    最近更新 更多