• google map使用自定义Marker在地图上添加文字标示


    google map默认的标示GMarker,只能使用图片不能使用文字。但是在实际中,我们不可避免的需要在地图上标示文字信息。例如地名等。Google 地图 API 使我们可以通过扩展GMarker实现自定义的GMarker的子类LabelMarker。

     1 google.maps.LabelMarker = function(latlng, options)
     2 {
     3     this.latlng = latlng;
     4     this.labelText = options.labelText || '';
     5     this.labelClass = options.labelClass || 'writeb';
     6     this.labelOffset = options.labelOffset || new google.maps.Size(8-33);
     7     options.icon = options.icon || getTextIcon();
     8     google.maps.Marker.apply(this, arguments);
     9 }
    10 
    11 google.maps.LabelMarker.prototype = new google.maps.Marker(new google.maps.LatLng(00));
    12 
    13 google.maps.LabelMarker.prototype.initialize = function(map){
    14     google.maps.Marker.prototype.initialize.call(this, map);
    15     
    16     var label = document.createElement('div');
    17     label.className = this.labelClass;
    18     label.innerHTML = this.labelText;
    19     label.style.position = 'absolute';
    20     label.style.width = '48px';
    21     map.getPane(G_MAP_MARKER_PANE).appendChild(label);
    22     
    23     this.map = map;
    24     this.label = label;
    25 }
    26 
    27 google.maps.LabelMarker.prototype.redraw = function(force){
    28     google.maps.Marker.prototype.redraw.call(this, map);
    29     
    30     if(!force)
    31     {
    32         return;
    33     }
    34     
    35     var point = this.map.fromLatLngToDivPixel(this.latlng);
    36     var z = google.maps.Overlay.getZIndex(this.latlng.lat());
    37     
    38     this.label.style.left = (point.x + this.labelOffset.width) + 'px';
    39     this.label.style.top = (point.y + this.labelOffset.height) + 'px';
    40     this.label.style.zIndex = z + 1;
    41 }
    42 
    43 google.maps.LabelMarker.prototype.remove = function(){
    44     this.label.parentNode.removeChild(this.label);
    45     this.label = null;
    46     google.maps.Marker.prototype.remove.call(this);
    47 }
    48 
    49 function getTextIcon()
    50 {
    51     var icon = new google.maps.Icon();
    52     icon.image = "/js/map/img/mapts.gif";
    53     icon.iconSize = new GSize(4840);
    54     icon.iconAnchor = new GPoint(040);
    55     icon.infoWindowAnchor = new GPoint(51);
    56     return icon;
    57 }

     在页面上调用的代码:

    1 var marker = new google.maps.LabelMarker(map.getCenter(), { 
    2     labelText:'我在这'
    3     });
    4         
    5 map.addOverlay(marker);

    现在就会在地图上显示我们自定义的GMarker标识了。

    继承GOverlay的实现自定义GMarker的方法: google map自定义GMarker的方法二

    家庭教育

  • 相关阅读:
    TCP的流量控制
    [数组]数组元素分割
    [折半查找]排序数组中某个元素出现次数
    [队列]判断出栈序列
    [排序算法]堆排序
    [树结构]有实际用途的树的计算公式
    重写重要的库函数
    [链表]同时遍历两个链表
    [查找]二分查找
    [数组]数组元素置换方法
  • 原文地址:https://www.cnblogs.com/hyl8218/p/1632524.html
Copyright © 2020-2023  润新知