JS Maps v3:带有用户个人资料图片的自定义标记

dba*_*baq 8 javascript google-maps google-maps-api-3 marker google-maps-markers

我在2天的时间里一直在努力思考,在地图上,我必须为每个用户显示一个标记,其中包含用户FB个人资料图片.

在此输入图像描述

我想知道我怎么能得到与此相似的结果?我试过的真是个hackish.

  • 我把FB图片作为标记图标
  • 我在标记的标签上放了一个CSS类
  • 我找到兄弟添加这个边框和这个箭头来装饰用户图片

但是当地图上有多个标记时,它不起作用.

.marker-labels {
    display: none !important;

    + div  { 
        background-color: $dark-gray; 
        border: 2px solid $dark-gray;
        @include radius(0.2em);
        height: 54px !important;
        width: 54px !important;
        overflow: inherit !important;

       > img {
            height: 50px;
            width: 50px;
        } 

        &:after {
            content: ' ';
            height: 0;
            width: 0;
            border: 6px solid transparent; 
            border-top-color: $dark-gray;
            position: absolute;
            top: 52px;
            left: 19px;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

全球问题:

谢谢你的帮助

mfi*_*aus 17

此答案假设您已经拥有facebook个人资料图片的URI.老实说,它感觉有一种更简单的方法,但我发现一些代码显示了如何使用自定义HTML元素创建自定义标记,我从那里开始.从那里开始,创建一个接受图像URI作为参数的自定义标记非常容易.从原始版本开始,我只添加了一个imageSrc参数,通过将类名附加到新div来将样式移到代码之外.就html和css而言,我只是将带有传递的图像URI的图像附加到div中,并添加了一些CSS以使其看起来像你拥有的那样.

演示

所以javascript代码看起来像这样:

function CustomMarker(latlng, map, imageSrc) { 
    this.latlng_ = latlng;
    this.imageSrc = imageSrc; //added imageSrc
    this.setMap(map);
}

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

CustomMarker.prototype.draw = function () {
    // Check if the div has been created.
    var div = this.div_;
    if (!div) {
        // Create a overlay text DIV
        div = this.div_ = document.createElement('div');
        // Create the DIV representing our CustomMarker
        div.className = "customMarker" //replaced styles with className

        var img = document.createElement("img");
        img.src = this.imageSrc; //attach passed image uri
        div.appendChild(img);
        google.maps.event.addDomListener(div, "click", function (event) {
            google.maps.event.trigger(me, "click");
        });

        // Then add the overlay to the DOM
        var panes = this.getPanes();
        panes.overlayImage.appendChild(div);
    }

    // Position the overlay 
    var point = this.getProjection().fromLatLngToDivPixel(this.latlng_);
    if (point) {
        div.style.left = point.x + 'px';
        div.style.top = point.y + 'px';
    }
};

CustomMarker.prototype.remove = function () {
    // Check if the overlay was on the map and needs to be removed.
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }
};

CustomMarker.prototype.getPosition = function () {
    return this.latlng_;
};
Run Code Online (Sandbox Code Playgroud)

我想我在这里只添加了一两行.您可以将此添加到我认为的页面中.有了这个,您可以像往常一样设置容器样式,它应该适用于所有自定义标记.您可以根据需要添加元素和类,以实现您正在寻找的外观.但为了完成起见,我在这里添加了我用于演示的样式.

.customMarker {   /* the marker div */
    position:absolute;
    cursor:pointer;
    background:#424242;
    width:100px;
    height:100px;

    /* we'll offset the div so that
       the point passed doesn't end up at
       the upper left corner but at the bottom
       middle. so we'll move it left by width/2 and
       up by height+arrow-height */
    margin-left:-50px;  
    margin-top:-110px;
    border-radius:10px;
    padding:0px;
}
.customMarker:after { //triangle
    content:"";
    position: absolute;
    bottom: -10px;
    left: 40px;
    border-width: 10px 10px 0;
    border-style: solid;
    border-color: #424242 transparent;
    display: block;
    width: 0;
}
.customMarker img { //profile image
    width:90px;
    height:90px;
    margin:5px;
    border-radius:2px;
}
Run Code Online (Sandbox Code Playgroud)

对于演示,我在数组中有一些示例数据,并使用for循环将它们放在地图上.

var data = [{
    profileImage: "http://domain.com/image1.jpg",
    pos: [37.77, -122.41],
}, {
    profileImage: "http://domain.com/image2.jpg",
    pos: [37.77, -122.41],
}]

for(var i=0;i<data.length;i++){
   new CustomMarker(
      new google.maps.LatLng(data[i].pos[0],data[i].pos[1]),
      map,
      data[i].profileImage
   )
}
Run Code Online (Sandbox Code Playgroud)

我希望有所帮助.