How to display a label next to a Marker for Google Maps?

后端 未结 6 447
醉酒成梦
醉酒成梦 2020-12-13 07:22

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 Tit

相关标签:
6条回答
  • 2020-12-13 07:32

    An alternative to using js library is to simply create a .png icon that will include your text. 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.

    0 讨论(0)
  • 2020-12-13 07:33

    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

    This is a custom class called InfoBox. You can see the API ref on the link above

    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

    Cheers!

    0 讨论(0)
  • 2020-12-13 07:43

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

    Demo: 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.

    0 讨论(0)
  • 2020-12-13 07:50

    For Google Maps JavaScript API v3, check out the examples here.

    Source code available in normal and minimised forms.

    0 讨论(0)
  • 2020-12-13 07:51

    There's a good label class here, though you'll have to add it alongside the markers.

    0 讨论(0)
  • 2020-12-13 07:52

    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.

    0 讨论(0)
提交回复
热议问题