Primefaces GMap标记与标签

时间:2016-12-14 22:49:52

标签: google-maps primefaces jsf-2.2

我有一个gmap,我在.xhtml中设置并在我的操作中添加标记:

<p:gmap id="topologyMap" fitBounds="true" type="MAP"
                            mapTypeControl="false" draggable="true" disableDoubleClickZoom="true"
                            navigationControl="false" streetView="false"
                            style="width:100%;height:500px"
                            model="#{Action.simpleModel}" disableDefaultUI="true">

在我的行动中我有

MapModel simpleModel = new DefaultMapModel();;
Marker newMarker = new Marker(new LatLng(latitude, longitude));
newMarker.setIcon("resources/media/marker-blue-dot.png");
simpleModel.addOverlay(newMarker);

我无法弄清楚如何在标记上添加标签。通过标签我的意思是我想在我的标记下面显示一些文字,例如:标记下面的城市名称。我正在使用primefaces 6.0。

感谢。

2 个答案:

答案 0 :(得分:0)

您必须为此使用一些Java

  <script>

        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 + 'px';
                this.span.style.top = position.y + 'px';
            }
        });

        //This is the base function
        function initialize() {

            //Take the map from Primefaces Gmap
            var gmap = PF('geoMapV').getMap();

            //Take All Markers in from the map
            var myMarkers = gmap.markers;

            //Add Label to the marker
            var i = 0;
            for (i = 0; myMarkers.length > i; i++) {
                myMarkers[i].setLabel("Marker Label!!");
            }

        }

    </script>

然后添加一些样式以在地图上显示标记

<style>
        .map-marker-label {
            position: absolute;
            color: red;
            font-size: 16px;
            font-weight: bold;
            /* Use margin to position the text */
            /* top, left position is the place of marker position */
            margin-top: -30px;
            margin-left: 15px;
        }
    </style>

然后我从ManageBean调用initialize()

     //Call Javascript from the labels  markers
     RequestContext.getCurrentInstance().execute("initialize()");

结果

The result

答案 1 :(得分:-1)

Marker的构造函数带有标题字符串:

a = (a+b).Substring((b=a).Length);