Google Map Api 自定义maker样式和InfoWindow样式(叠加层)

      在做googlemap的时候,一般会要求有标记(maker),点击标记或者鼠标移上去会显示信息框(infowindow),但google自带的maker只能自己定义图片

不能改变图片里的内容,如下图,显示钱数,infowindow虽然能够自定义内部的html代码,但外部边框,还是不能改变,这样就不能达到自己想要的效果.

      这样我们就要用到谷歌地图的自定义叠加层(OverlayView)

参考地址:https://developers.google.com/maps/documentation/javascript/overlays?hl=zh-cn#CustomOverlays

代码如下..

  var map;
     var lastpointer="";      //记录那个大的类似infowindow的div的id
     var lastpointer1="";    //记录类似maker的div的id
      var categoryid='<%=CategoryID %>';
        jq(document).ready(function () {
            initialize("<%=midx %>", "<%=midy %>");//<%=midx %>,<%=midy %>这两个是从后台取的值
        });
        function initialize(lat,lng) {
         var latlng = new google.maps.LatLng(lat, lng);
     
            var mapOptions = {
                zoom: 3,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
          map = new google.maps.Map(document.getElementById('map-canvas1'), mapOptions);

//添加监听,当中心坐标,缩放比例变化时,类似的infowindow隐藏
         google.maps.event.addListener(map, 'zoom_changed', function() {
                if(lastpointer!="")
                {
                        jq(lastpointer).hide();
                }
            });
         google.maps.event.addListener(map, 'center_changed', function() {
                if(lastpointer!="")
                {
                        jq(lastpointer).hide();
                }
            });

 

//下面是循环添加叠加层
           <% for (int j = 0; j <_mapList.Count ; j++) { %>
           if (<%=_mapList[j].xcoord%>!='null'&&<%=_mapList[j].xcoord%>!=''&&<%=_mapList[j].ycoord%>!='null'&&<%=_mapList[j].ycoord%>!=''&&<%=_mapList[j].ycoord%>!=null&&<%=_mapList[j].xcoord%>!=null){

addSite(map,<%=j %>,'<% =_mapList[j].simpleprice%>',<%=_mapList[j].xcoord %>,<%=_mapList[j].ycoord %>,"<%=_mapList[j].comerce %>","<%=_mapList[j].detailurl %>","<%=_mapList[j].projname %>","<%=_mapList[j].titleimg %>","<%=_mapList[j].livearea %>","<%=_mapList[j].cretetime %>","<%=_mapList[j].pricermb %>");
}
        <% }%>
        }

 

//添加叠加层的方法

function addSite(map, siteCode, siteDesc, lat, lng, address,url,projectname,picurl,livearea,createtime,price) {
    var pt = new google.maps.LatLng(lat,lng);

//这实例化一个叠加层
    var label = new ELabel(map,
        pt,
        '<div class="SearchMapCityIco" οnmοuseοut="showinfo2('+siteCode+',this)"  id="m'+siteCode+'" οnmοuseοver="showinfo('+siteCode+',this)" ><a class="CityIco">'+siteDesc+'</a>'+
        '<div id="houseinfo'+siteCode+'" style="position: absolute; display:none;" class="SearchMapBox">'+
         '<div class="SearchMapBoxTit">'+
          '<a href="'+url+'" target="_blank">'+projectname+'</a></div>'+
           '<div class="SearchMapBoxTxt clearfix">'+
            '<div class="SearchMapBoxTxtImg">'+
             '<a  href="'+url+'" target="_blank">'+
              '<img width="70" height="47" alt="" src="'+picurl+'"></a></div>'+
               '<div class="SearchMapBoxTxtImgR">'+
                '<p class="Color999">'+
                '<span>'+livearea+' </span><span class="PL20">'+createtime+'</span></p>'+
                 '<p class="Color999">'+address+
                 '</p>'+
                  '<p class="FwB ColorBlack">'+price+
                   '</p>'+
                   '</div>'+
                   '</div>'+
                '<div class="SearchMapBoxJianTou">'+
                  '<span>◆</span> <b>◆</b>'+
                  '</div>'+
                    '</div>'
        +'<\/div>',
        '', // null
                new google.maps.Size(-15,9)
        );
    label.setMap(map);//在地图上添加叠加层
}

//鼠标的mouseover方法,显示类似infowindow信息

function showinfo(code,obj)
{
    if(lastpointer!="")
    {
        jq(lastpointer).hide();
        jq(lastpointer1).removeClass("ColorYellowBg");
          
     }
    jq("#houseinfo"+code).show();
    lastpointer="#houseinfo"+code;
    lastpointer1="#m"+code;
    jq("#m"+code).addClass("ColorYellowBg");
     jq("#m"+code).parent().parent().addClass("ColorYellowBg1").siblings().removeClass("ColorYellowBg1");
}

//鼠标mouseout方法,隐藏

function showinfo2(code,obj)
{
 if(lastpointer!="")
    {
        jq(lastpointer).hide();
        jq("#m"+code).removeClass("ColorYellowBg");       
     }
jq("#m"+code).removeClass("ColorYellowBg");  
}

 

下面是Elable的js内容,从网上找的,忘了下载地址了.coding by Mike Williams

function ELabel(map, point, html, classname, pixelOffset, percentOpacity, overlap) {
    this.div_ = null;
    this.map_ = map;
    this.point = point;
    this.html = html;                                                                                             
    this.classname = classname || "";
    this.pixelOffset = pixelOffset || new google.maps.Size(0, 0);
    if (percentOpacity) {
        if (percentOpacity < 0) percentOpacity = 0;
        if (percentOpacity > 100) percentOpacity = 100;
    }
    this.percentOpacity = percentOpacity;
    this.overlap = overlap || false;
    this.hidden = false;
}

ELabel.prototype = new google.maps.OverlayView();

ELabel.prototype.onAdd = function () {
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.innerHTML = '<div class=" + this.classname + ">' + this.html + '</div>';
    this.div_ = div;
    if (this.percentOpacity) {
        if (typeof (div.style.filter) == 'string') { div.style.filter = 'alpha(opacity:' + this.percentOpacity + ')'; }
        if (typeof (div.style.KHTMLOpacity) == 'string') { div.style.KHTMLOpacity = this.percentOpacity / 100; }
        if (typeof (div.style.MozOpacity) == 'string') { div.style.MozOpacity = this.percentOpacity / 100; }
        if (typeof (div.style.opacity) == 'string') { div.style.opacity = this.percentOpacity / 100; }
    }
    if (this.overlap) {
        // you may need to work on this "hack" to replace V2 getZindex
        // GOverlay.getZIndex(this.point.lat());
        var z = 1000 * (90 - this.point.lat());
        this.div_.style.zIndex = parseInt(z);
    }
    if (this.hidden) {
        this.hide();
    }

    // add ourselves to the shadow overlay layer

    var panes = this.getPanes();
    panes.floatShadow.appendChild(div);
}


ELabel.prototype.onRemove = function () {
    this.div_.parentNode.removeChild(this.div_);
}

ELabel.prototype.draw = function (force) {
    var proj = this.getProjection();
    var p = proj.fromLatLngToDivPixel(this.point);
    var h = parseInt(this.div_.clientHeight);
    this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
    this.div_.style.top = (p.y + this.pixelOffset.height - h) + "px";

}

ELabel.prototype.show = function () {
    if (this.div_) {
        this.div_.style.display = "";
        this.redraw();
    }
    this.hidden = false;
}

ELabel.prototype.hide = function () {
    if (this.div_) {
        this.div_.style.display = "none";
    }
    this.hidden = true;
}



ELabel.prototype.copy = function () {
    return new ELabel(this.point, this.html, this.classname, this.pixelOffset, this.percentOpacity, this.overlap);
}

ELabel.prototype.isHidden = function () {
    return this.hidden;
}

ELabel.prototype.supportsHide = function () {
    return true;
}

ELabel.prototype.setContents = function (html) {
    this.html = html;
    this.div_.innerHTML = '<div class=" + this.classname + ">' + this.html + '</div>';
    this.redraw(true);
}

ELabel.prototype.setPoint = function (point) {
    this.point = point;
    if (this.overlap) {
        var z = GOverlay.getZIndex(this.point.lat());
        this.div_.style.zIndex = z;
    }
    this.redraw(true);
}

ELabel.prototype.setOpacity = function (percentOpacity) {
    if (percentOpacity) {
        if (percentOpacity < 0) { percentOpacity = 0; }
        if (percentOpacity > 100) { percentOpacity = 100; }
    }
    this.percentOpacity = percentOpacity;
    if (this.percentOpacity) {
        if (typeof (this.div_.style.filter) == 'string') { this.div_.style.filter = 'alpha(opacity:' + this.percentOpacity + ')'; }
        if (typeof (this.div_.style.KHTMLOpacity) == 'string') { this.div_.style.KHTMLOpacity = this.percentOpacity / 100; }
        if (typeof (this.div_.style.MozOpacity) == 'string') { this.div_.style.MozOpacity = this.percentOpacity / 100; }
        if (typeof (this.div_.style.opacity) == 'string') { this.div_.style.opacity = this.percentOpacity / 100; }
    }
}

ELabel.prototype.getPoint = function () {
    return this.point;
}

ELabel.prototype.redraw = function (force) {
    var proj = this.getProjection();
    var p = proj.fromLatLngToDivPixel(this.point);
    var h = parseInt(this.div_.clientHeight);
    this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
    this.div_.style.top = (p.y + this.pixelOffset.height - h) + "px";
}

 

转载于:https://www.cnblogs.com/dingsong/p/3368531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用高德地图API和Vue3实现自定义弹窗样式时,可以参考以下步骤: 1. 首先,初始化地图弹窗实例,并设置isCustom为true,表示使用自定义窗体。可以使用AMap.InfoWindow类来创建地图弹窗实例,并设置偏移量等属性。\[1\] 2. 在处理地图标记物点击事件时,可以通过获取点击的标记物对象,设置动画效果,并获取相应的内容。可以使用AMap.InfoWindow的setContent方法设置弹窗内容,然后使用open方法打开弹窗。\[1\] 3. 可以参考高德地图官方提供的demo样式,该demo展示了自定义弹窗的样式。可以在官方demo中查看代码和样式,以便参考和使用。\[2\] 4. 高德地图官网还提供了自定义弹窗内容的例子,使用了Dom操作的方式来实现。可以使用Vue的全局API中的Vue.extend方法来创建一个Vue子类构造器,然后通过实例化该构造器来创建一个Vue对象,类似于Dom中的DocumentFragment接口。这样可以使用Vue的方式来实现自定义弹窗的Dom操作。\[3\] 综上所述,你可以使用高德地图API和Vue3来实现自定义弹窗样式。可以参考高德地图官方提供的demo和文档,根据自己的需求进行相应的代码编写和样式设计。 #### 引用[.reference_title] - *1* *3* [高德地图自定义弹窗内容](https://blog.csdn.net/fredricen/article/details/106172766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [VUE 高德自定义弹窗样式不生效?(AMap.InfoWindow弹窗样式问题)](https://blog.csdn.net/weixin_39921970/article/details/120744647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值