简体   繁体   English

如何在谷歌地图的标记旁边显示标签?

[英]How to display a label next to a Marker for Google Maps?

I would like to display a text label next to the markers on google maps.我想在谷歌地图上的标记旁边显示一个文本标签。 I've used Virtual Earth before and I'm just starting to use Google Maps.我以前用过虚拟地球,现在才开始用谷歌地图。 I tried setting the Title property but that only changes the roll over text.我尝试设置 Title 属性,但这只会更改翻转文本。

Is there a way to display a small line of text underneath a marker that will stay there as the user zooms, pans and uses the map?有没有办法在标记下方显示一小行文本,当用户缩放、平移和使用地图时,该文本会保留在那里?

For Google Maps JavaScript API v3, check out the examples here . 对于Google Maps JavaScript API v3,请查看此处的示例

Source code available in normal and minimised forms . 源代码以正常和最小化形式提供

有一个很好的标签类在这里 ,但你必须添加它旁边的标记。

If you just want to show label below the marker, then you can extend google maps Marker to add a setter method for label and you can define the label object by extending google maps overlayView like this..如果您只想在标记下方显示标签,那么您可以扩展 google maps Marker 为标签添加一个 setter 方法,您可以通过像这样扩展 google maps overlayView 来定义标签对象。

Demo: jsFiddle演示: jsFiddle

<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>

This will work.这将起作用。

Here is some sample code which would solve the problem: https://github.com/printercu/google-maps-utility-library-v3-read-only/tree/master/markerwithlabel/examples 以下是一些可以解决问题的示例代码: https//github.com/printercu/google-maps-utility-library-v3-read-only/tree/master/markerwithlabel/examples

This is a custom class called InfoBox. 这是一个名为InfoBox的自定义类。 You can see the API ref on the link above 您可以在上面的链接中看到API参考

The js file can be included from here: https://github.com/printercu/google-maps-utility-library-v3-read-only/blob/master/infobox/src/infobox.js js文件可以在这里包含: https//github.com/printercu/google-maps-utility-library-v3-read-only/blob/master/infobox/src/infobox.js

Cheers! 干杯!

Even this question has been answered I have found this resource , which I think is a good solution for this issue and could be helpful. 即使这个问题已经得到解答我已经找到了这个资源 ,我认为这是一个很好的解决方案,可能会有所帮助。 Also, a similar solution could be found here . 此外,可以在此处找到类似的解决方案。

An alternative to using js library is to simply create a .png icon that will include your text. 使用js库的另一种方法是简单地创建一个包含文本的.png图标。 It sounds silly, but you can use some external service for this (like this one ). 听起来很傻,但你可以使用一些外部服务(就像这个 )。

You upload your icon, and get a link in return. 您上传您的图标,并获得一个链接作为回报。 If you add some text to links url - it will return your icon with that text rendered into it. 如果您向链接网址添加一些文字 - 它会返回您的图标,并将该文字呈现在其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM