文本叠加 - 显示在错误的位置

时间:2013-02-05 14:52:29

标签: css google-maps google-maps-markers

我已经对此Question实施了已接受的答案。当我运行html文件时,我在Google Map标记上写的文本显示在错误的位置(参见图片error.png)。任何人都可以帮我在标记上正确显示文字吗?

enter image description here

enter image description here

Marker.html

    <html><head>
<title> Hi </title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

            function TxtOverlay(pos, txt, cls, map){

                this.pos = pos;
                this.txt_ = txt;
                this.cls_ = cls;
                this.map_ = map;
                this.div_ = null;
                this.setMap(map);
            }

            TxtOverlay.prototype = new google.maps.OverlayView();
            TxtOverlay.prototype.onAdd = function(){
                var div = document.createElement('DIV');
                div.className = this.cls_;
                div.innerHTML = this.txt_;                
                var overlayProjection = this.getProjection();
                var position = overlayProjection.fromLatLngToDivPixel(this.pos);
                div.style.left = ( position.x) + 'px';
                div.style.top = ( position.y)+ 'px';                
                var panes = this.getPanes();
                panes.floatPane.appendChild(div);
            }
            TxtOverlay.prototype.draw = function(){
                var overlayProjection = this.getProjection();
                var position = overlayProjection.fromLatLngToDivPixel(this.pos);
                var div = this.div_;
                div.style.left = ( position.x) + 'px';
                div.style.top = ( position.y)+ 'px';
            }

            TxtOverlay.prototype.onRemove = function(){
                this.div_.parentNode.removeChild(this.div_);
                this.div_ = null;
            }
            TxtOverlay.prototype.hide = function(){
                if (this.div_) {
                    this.div_.style.visibility = "hidden";
                }
            }
            TxtOverlay.prototype.show = function(){
                if (this.div_) {
                    this.div_.style.visibility = "visible";
                }
            }
            TxtOverlay.prototype.toggle = function(){
                if (this.div_) {
                    if (this.div_.style.visibility == "hidden") {
                        this.show();
                    }
                    else {
                        this.hide();
                    }
                }
            }

            TxtOverlay.prototype.toggleDOM = function(){
                if (this.getMap()) {
                    this.setMap(null);
                }
                else {
                    this.setMap(this.map_);
                }
            }

            var map;
            function init(){
                var latlng = new google.maps.LatLng(37.90695894, -122.07920128);
                var myOptions = {
                    zoom: 4,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map"), myOptions);
                var marker = new google.maps.Marker({
                    position: latlng,
                    map: map,
                    title: "Hello World!",
                    icon:"./bluepin.PNG"
                });
                customTxt = "<div>1</div>"
                txt = new TxtOverlay(latlng,customTxt,"customBox",map )
            }
        </script>
        <style>
            .customBox {                                
                background: AARRGGBB;
                border: 1px;
                color:#ffffff;
                position: relative;
            }
        </style>
    </head>
    <body onload="init()">
        <div id="map" style="width: 600px; height: 600px;">
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要偏移div,使其显示在标记上方。偏移量(-5,-30)适用于此标记和数字“1”。

        function TxtOverlay(pos, txt, cls, map, offset){

            this.pos = pos;
            this.txt_ = txt;
            this.cls_ = cls;
            this.map_ = map;
            this.offset_ = offset;
            this.div_ = null;
            this.setMap(map);
        }

        TxtOverlay.prototype.onAdd = function(){
            var div = document.createElement('DIV');
            div.className = this.cls_;
            div.innerHTML = this.txt_;                
            this.div_ = div;
            var overlayProjection = this.getProjection();
            var position = overlayProjection.fromLatLngToDivPixel(this.pos);
            div.style.left = ( position.x + this.offset_.x) + 'px';
            div.style.top = ( position.y + this.offset_.y)+ 'px';                
            var panes = this.getPanes();
            panes.floatPane.appendChild(div);
        }
        TxtOverlay.prototype.draw = function(){
            var overlayProjection = this.getProjection();
            var position = overlayProjection.fromLatLngToDivPixel(this.pos);
            var div = this.div_;
            div.style.left = ( position.x + this.offset_.x) + 'px';
            div.style.top = ( position.y + this.offset_.y)+ 'px';                
        }

        txt = new TxtOverlay(latlng,customTxt,"customBox",map,new google.maps.Point(-5,-30) )

Example

您也可以查看the MarkerWithLabel utility library

Example