如何在使用谷歌地图api时显示多边形标题

时间:2016-01-24 23:12:53

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

我为美国地图上的每个州绘制了多边形。 我希望在它上面显示一个值(text / int)。我找不到任何能够在多边形顶部显示文本的多边形选项。有什么方法可以做到这一点?

$(document).ready(function() {
  $('.button').on('click dblclick', function(e) {

    $('.slide-in').toggleClass('active');
    e.stopPropagation();

  });
  $(document).on('click', function() {
    $(".slide-in").removeClass("active");
  });
});

4 个答案:

答案 0 :(得分:1)

您可以使用MapLabel Utility执行此操作。只需设置标签的位置并通过对象应用选项。

label: new MapLabel({
    text: result[index].name,
    position: 
       new google.maps.LatLng(object.lat, object.lng), // the lat/lng of location of the label.
    fontSize: 10,
    fontColor: #000,
    labelInBackground: true,
    strokeColor: #000,
    map: map   // The map object to place the label on
})

答案 1 :(得分:0)

Polygon不支持开箱即用。您可以做的是通过从google.maps.OverlayView派生自定义标签类型。

然后创建一个新Label,设置其属性,并将其位置设置为多边形的中心。执行此操作后,如果您碰巧编辑多边形,Google Maps api将更新标签位置。

//for var polygon
var _label = new Label({ map: _options.map, text: _options.name, position: polygon.GetCenter(), color: _options.colour });
_label.bindTo("zoom", _options.map, "zoom");

Label类。 From a library I created a while back to manage spatial objects in google maps

define(
    ['gmaps'],
    function(gmaps) {

        // Define the overlay, derived from gmaps.OverlayView
        var label = function(options) {
            // Initialization
            this.setValues(options);

            var div = this.div_ = document.createElement('div');
            div.style.cssText = 'position: absolute; display: none; -webkit-transform: translateZ(0px); z-index:1000;';

            // Label specific
            var span = this.span_ = document.createElement('span');

            span.style.cssText = 'position: relative; left: -50%; top: -8px; display:block; filter:progid:DXImageTransform.Microsoft.Glow(Color=#333333, Strength=2); ' +
                'white-space: nowrap; border: none; color:' + this.get('color') + '; ' +
                'padding: 2px; z-index:1000; font-size:12px; ';

            if (this.get('shadowstyle') == undefined)
                span.style.cssText += 'text-shadow: -1px -1px 0px #111, 1px -1px 0px #111, -1px 1px 0px #111, 1px 1px 0px #111;';
            else
                span.style.cssText += this.get('shadowstyle');

            div.appendChild(span);
        };

        if (gmaps.OverlayView) {
            label.prototype = new gmaps.OverlayView();
        }

        // Implement onAdd
        label.prototype.onAdd = function() {
            var pane = this.getPanes().overlayLayer;
            pane.appendChild(this.div_);

            // Ensures the label is redrawn if the text or position is changed.
            var me = this;
            this.listeners_ = [
                gmaps.event.addListener(this, 'position_changed', function() { me.draw(); }),
                gmaps.event.addListener(this, 'text_changed', function() { me.draw(); })
            ];
        };

        // Implement onRemove
        label.prototype.onRemove = function() {
            this.div_.parentNode.removeChild(this.div_);

            // Label is removed from the map, stop updating its position/text.
            for (var i = 0, I = this.listeners_.length; i < I; ++i) {
                gmaps.event.removeListener(this.listeners_[i]);
            }
        };

        // Implement draw
        label.prototype.draw = function() {
            var projection = this.getProjection();
            var position = projection.fromLatLngToDivPixel(this.get('position'));
            var mapzoom = this.get('zoom');
            if (mapzoom == null) mapzoom = 16;

            var div = this.div_;

            if (mapzoom > 11) {
                div.style.left = position.x + 'px';
                div.style.top = position.y + 'px';
                div.style.display = 'block';
                this.span_.innerHTML = this.get('text').toString();
            } else {
                div.style.display = 'none';
            }
        };

        return label;
    }
);

答案 2 :(得分:0)

我一直在寻找一种简单的实现方法,因此找到了解决方案。希望可以帮助某人:

  1. 在您想要显示标签的位置(例如,在多边形的中间)创建一个标记
  2. 在此标记中,创建标签
  3. 在此标记中,将图标的不透明度设置为0
  4. 您将在多边形上方放置标签。

代码如下:

addMarkerAsLabel = async (position, map, title) => {
var marker = new this.props.google.Marker({
  position,
  map,
  draggable: false,
  label: {
    text: title,
    color: "#FFFFFF",
    textShadow: "2px 2px 2px #000000"
  },
  icon: {
    path: "your icon path here",
    fillOpacity: 0,
    strokeWeight: 0
  }
});

答案 3 :(得分:-1)

尝试使用mouseOver。但不太确定。

google.maps.event.addListener(polygon,"mouseover",function(){
this.setOptions({name: "title"});
});