带有标签的Google Maps V3标记

Adr*_*scu 20 google-maps-api-3

如果我的标记在ajax成功填充每个结果,我如何添加标记到我的标记.

map.gmap('addMarker', { 'position': new google.maps.LatLng(result.latitude, result.longitude) });
Run Code Online (Sandbox Code Playgroud)

我试过这样,但没有成功:

map.gmap('addMarker', { 
    'position': new google.maps.LatLng(result.latitude, result.longitude), 
    'bounds': true,
    'icon': markerIcon,
    'labelContent': 'A',
    'labelAnchor': new google.maps.Point(result.latitude, result.longitude),
    'labelClass': 'labels', // the CSS class for the label
    'labelInBackground': false
});
Run Code Online (Sandbox Code Playgroud)

Ram*_*ani 56

如果你只想在标记下方显示标签,那么你可以扩展谷歌地图标记为标签添加一个setter方法,你可以通过像这样扩展谷歌地图overlayView来定义标签对象.

<script type="text/javascript">
    var point = { lat: 22.5667, lng: 88.3667 };
    var markerSize = { x: 22, y: 40 };


    google.maps.Marker.prototype.setLabel = function(label){
        this.label = new MarkerLabel({
          map: this.map,
          marker: this,
          text: label
        });
        this.label.bindTo('position', this, 'position');
    };

    var MarkerLabel = function(options) {
        this.setValues(options);
        this.span = document.createElement('span');
        this.span.className = 'map-marker-label';
    };

    MarkerLabel.prototype = $.extend(new google.maps.OverlayView(), {
        onAdd: function() {
            this.getPanes().overlayImage.appendChild(this.span);
            var self = this;
            this.listeners = [
            google.maps.event.addListener(this, 'position_changed', function() { self.draw();    })];
        },
        draw: function() {
            var text = String(this.get('text'));
            var position = this.getProjection().fromLatLngToDivPixel(this.get('position'));
            this.span.innerHTML = text;
            this.span.style.left = (position.x - (markerSize.x / 2)) - (text.length * 3) + 10 + 'px';
            this.span.style.top = (position.y - markerSize.y + 40) + 'px';
        }
    });
    function initialize(){
        var myLatLng = new google.maps.LatLng(point.lat, point.lng);
        var gmap = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 5,
            center: myLatLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        var myMarker = new google.maps.Marker({
            map: gmap,
            position: myLatLng,
            label: 'Hello World!',
            draggable: true
        });
    }
</script>
<style>
    .map-marker-label{
        position: absolute;
    color: blue;
    font-size: 16px;
    font-weight: bold;
    }
</style>
Run Code Online (Sandbox Code Playgroud)

这会奏效.

  • 工作了一个魅力.谢谢你,你太棒了!我希望你能得到更多的回报,你的回答比蒂姆的要快100%. (3认同)

mar*_*-hi 20

我怀疑标准库是否支持此功能.

但您可以使用谷歌地图实用程序库:

http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries#MarkerWithLabel

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);

var myOptions = {
    zoom: 8,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

var marker = new MarkerWithLabel({
   position: myLatlng,
   map: map,
   draggable: true,
   raiseOnDrag: true,
   labelContent: "A",
   labelAnchor: new google.maps.Point(3, 30),
   labelClass: "labels", // the CSS class for the label
   labelInBackground: false
 });
Run Code Online (Sandbox Code Playgroud)

有关标记的基础知识,请访问:https://developers.google.com/maps/documentation/javascript/overlays#Markers


rob*_*obd 9

在3.21版(2015年8月)的Google地图添加了对单字符标记标签的支持.查看新的标记标签API.

您现在可以像这样创建标签标记:

var marker = new google.maps.Marker({
  position: new google.maps.LatLng(result.latitude, result.longitude), 
  icon: markerIcon,
  label: {
    text: 'A'
  }
});
Run Code Online (Sandbox Code Playgroud)

如果您希望删除1个字符的限制,请投票支持此问题.

2016年10月更新:

此问题已修复,从版本3.26.10开始,Google地图本身支持多个字符标签以及使用MarkerLabels的自定义图标.


tim*_*tim 7

不使用插件的情况下执行此操作的方法是创建google的OverlayView()方法的子类.

https://developers.google.com/maps/documentation/javascript/reference?hl=en#OverlayView

您可以创建自定义函数并将其应用于地图.

function Label() { 
    this.setMap(g.map);
};
Run Code Online (Sandbox Code Playgroud)

现在,您构建子类的原型并添加HTML节点:

Label.prototype = new google.maps.OverlayView; //subclassing google's overlayView
Label.prototype.onAdd = function() {
        this.MySpecialDiv               = document.createElement('div');
        this.MySpecialDiv.className     = 'MyLabel';
        this.getPanes().overlayImage.appendChild(this.MySpecialDiv); //attach it to overlay panes so it behaves like markers
Run Code Online (Sandbox Code Playgroud)

}

您还必须实现API文档中所述的删除和绘制功能,否则这将无效.

Label.prototype.onRemove = function() {
... // remove your stuff and its events if any
}
Label.prototype.draw = function() {
      var position = this.getProjection().fromLatLngToDivPixel(this.get('position')); // translate map latLng coords into DOM px coords for css positioning
var pos = this.get('position');
            $('.myLabel')
            .css({
                'top'   : position.y + 'px',
                'left'  : position.x + 'px'
            })
        ;
}
Run Code Online (Sandbox Code Playgroud)

这就是它的要点,你必须在你的具体实现中做更多的工作.