最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS Maps v3: custom marker with user profile picture - Stack Overflow

programmeradmin2浏览0评论

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.

I am wondering how I can have a result similar to this one? What I tried was really hackish.

  • I put the FB picture as the marker icon
  • I put a CSS class on the label of the marker
  • I find the sibling to add this border and this arrow to decorate the user picture

but it doesn't work when there is more than one marker on the map.

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

global question:

  • how can I get an icon like that (.jpg for instance) with a custom user picture inside? is it possible?

  • otherwise how is it possible to customize the icon (if it is the profile picture) to have a result similar to the screenshot

thanks for your help

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.

I am wondering how I can have a result similar to this one? What I tried was really hackish.

  • I put the FB picture as the marker icon
  • I put a CSS class on the label of the marker
  • I find the sibling to add this border and this arrow to decorate the user picture

but it doesn't work when there is more than one marker on the map.

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

global question:

  • how can I get an icon like that (http://mt-st.rfclipart.com/image/thumbnail/24-1d-5f/blue-glossy-square-map-pin-or-speech-bubble-Download-Royalty-free-Vector-File-EPS-29153.jpg for instance) with a custom user picture inside? is it possible?

  • otherwise how is it possible to customize the icon (if it is the profile picture) to have a result similar to the screenshot

thanks for your help

Share Improve this question asked May 31, 2014 at 0:20 dbaqdbaq 1,3571 gold badge14 silver badges26 bronze badges 3
  • Something like this jsfiddle? From this similar question: How to crop a circle shaped image and then set it inside another image?. – geocodezip Commented May 31, 2014 at 5:56
  • Yes but this kind of solutions are for static maps – dbaq Commented Jun 2, 2014 at 16:17
  • To whom downvote that question, at least, you should say why. If the question is unclear I can try to explain again. – dbaq Commented Jun 3, 2014 at 5:16
Add a comment  | 

1 Answer 1

Reset to default 18 +50

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)

  1. 暂无评论