Specify the z-index of Google Map Markers

前端 未结 4 557
离开以前
离开以前 2020-12-25 11:28

I am having a Google Map with lots of markers added using websockets. I am using custom marker images based on data availability. I want to make sure the newest marker stays

相关标签:
4条回答
  • 2020-12-25 12:10

    By default map will make z-index higher for markers lower on the map so they visibly stack top to bottom

    You can set zIndex option for a marker. You would just need to create a increment zIndex counter as you add markers and add that to the marker options object:

    marker=new google.maps.Marker({
        position:pin,
        icon:markerIcon,
        zIndex: counterValue
    });
    

    I'm not quite sure what the base start value needs to be

    0 讨论(0)
  • 2020-12-25 12:17

    You have to set the marker option "optimized" to false in combination with the zIndex option.

    var marker=new google.maps.Marker({
          position:pin2,
          optimized: false,
          zIndex:99999999
    });
    

    This should solve your problem.

    0 讨论(0)
  • 2020-12-25 12:25

    Problem solved. Just set the zValue to 999 (I'm guessing higher will bring it more to the top) and it should come to the top. I guess the default is below 999:

    var zValue;
    
    if (someCondition) {
        zValue = 999;
    }
    
    var marker = new google.maps.Marker({
        map: map,
        position: {lat: lat, lng: lng},
        title: titleString,
        icon: image,
        zIndex: zValue
    });
    
    0 讨论(0)
  • 2020-12-25 12:28

    Take care that as it says in the google map documentation the z-index of a circle will be compared to the z-index of other polygons and not to the z-index of the markers.

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