JS Maps v3: custom marker with user profile picture

前端 未结 1 1191
旧巷少年郎
旧巷少年郎 2020-11-30 07:27

I am struggling since 2 days with something I was thinking easy, on a map, I have to display a marker for each user with the user FB profile picture inside.

相关标签:
1条回答
  • 2020-11-30 07:47

    This answer assumes you already have the URIs for the facebook profile images. Honestly, it feels there is an easier way, but I found some code that shows how to create a custom marker with custom HTML elements and I went from there. From there's it's pretty easy to create a custom marker that accepts a image URI as a parameter. From the original, I just added an imageSrc parameter, moved the styling outside the code by attaching a class name to the new div. In terms of html and css, I just appended an image with the passed image URI into the div, and just added some CSS to make it look like what you have.

    Demo

    So the javascript code looks something like this:

    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_;
    };
    

    I think I added only one or two lines here. You can just add this to your page I think. With this in place you can just style the container as normal, and it should apply to all the custom markers. You can add elements and classes as you see fit to achieve the look you are looking for. But for completion's sake I added the styles I used for the demo here.

    .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;
    }
    

    And for the demo I have some sample data in array and placed them on the map using a for loop.

    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
       )
    }
    

    I hope that helps.

    0 讨论(0)
提交回复
热议问题