zoukankan      html  css  js  c++  java
  • google map使用自定义Marker在地图上添加文字标示

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

    Code highlighting produced by Actipro CodeHighlighter (freeware)
    http://www.CodeHighlighter.com/
    
    --> 1 google.maps.LabelMarker = function(latlng, options)
     {
         this.latlng = latlng;
         this.labelText = options.labelText || '';
         this.labelClass = options.labelClass || 'writeb';
         this.labelOffset = options.labelOffset || new google.maps.Size(8, -33);
         options.icon = options.icon || getTextIcon();
         google.maps.Marker.apply(this, arguments);
     }
     
     google.maps.LabelMarker.prototype = new google.maps.Marker(new google.maps.LatLng(0, 0));
     
     google.maps.LabelMarker.prototype.initialize = function(map){
         google.maps.Marker.prototype.initialize.call(this, map);
         
         var label = document.createElement('div');
         label.className = this.labelClass;
         label.innerHTML = this.labelText;
         label.style.position = 'absolute';
         label.style.width = '48px';
         map.getPane(G_MAP_MARKER_PANE).appendChild(label);
         
         this.map = map;
         this.label = label;
     }
     
     google.maps.LabelMarker.prototype.redraw = function(force){
         google.maps.Marker.prototype.redraw.call(this, map);
         
         if(!force)
         {
             return;
         }
         
         var point = this.map.fromLatLngToDivPixel(this.latlng);
         var z = google.maps.Overlay.getZIndex(this.latlng.lat());
         
         this.label.style.left = (point.x + this.labelOffset.width) + 'px';
         this.label.style.top = (point.y + this.labelOffset.height) + 'px';
         this.label.style.zIndex = z + 1;
     }
     
     google.maps.LabelMarker.prototype.remove = function(){
         this.label.parentNode.removeChild(this.label);
         this.label = null;
         google.maps.Marker.prototype.remove.call(this);
     }
     
     function getTextIcon()
     {
         var icon = new google.maps.Icon();
         icon.image = "/js/map/img/mapts.gif";
         icon.iconSize = new GSize(48, 40);
         icon.iconAnchor = new GPoint(0, 40);
         icon.infoWindowAnchor = new GPoint(5, 1);
         return icon;
     }
    

      在页面上调用的代码:

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

      

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

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

     

  • 相关阅读:
    编写高质量Python代码的59个有效方法
    排序NB三人组(快速排序/堆排序/归并排序)
    排序之插入排序
    编写高质量代码 改善Python程序的91个建议——笔记(三)
    ImportHelper 导出帮助类
    使用Layer弹出一个页面
    MongoDB使用手册
    MVC4设置@Html.BeginForm的ID
    LayerManage
    Layer弹窗返回数据回调
  • 原文地址:https://www.cnblogs.com/greywolf/p/2621945.html
Copyright © 2011-2022 走看看