简体   繁体   English

Google Maps JS API - 根据缩放级别调整所有自定义标记的大小

[英]Google Maps JS API - Resize ALL custom markers based on zoom level

This is a fairly common question but all answers I have found only work with a single marker, I have one example on jsfiddle in witch I place 4 markers on the map (this is just for example porpuses, in my real scenario the position for the markers comes from an API source).这是一个相当常见的问题,但我发现的所有答案都只适用于一个标记,我在女巫中的 jsfiddle 上有一个示例我在地图上放置了 4 个标记(这只是例如 porpuses,在我的真实场景中的位置标记来自 API 源)。 I can "only" change the size for one of the markers and I do a Zoom in, and can't quite find a way to replicate it to all markers, so it there's another way around it I would appreciate some clues.我可以“只”更改一个标记的大小,然后放大,并且无法找到将其复制到所有标记的方法,因此还有另一种解决方法,我希望能提供一些线索。

 /** declare map as a global variable */
var map;
      

  /** create map   */
  
  var map = new google.maps.Map(document.getElementById("map_div"), {
    center: new google.maps.LatLng(33.808678, -117.918921),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  
  /*  add markers to map   */

   var icon = {
          url: "https://www.dropbox.com/s/nx8bz4yobukzb9v/sonangol_marker.png?dl=1",
          size: new google.maps.Size(50, 58),
          scaledSize: new google.maps.Size(20, 28),
          origin: new google.maps.Point(0,0), // origin
          anchor: new google.maps.Point(10, 28) // anchor
        }
   var places = ['33.808678', '33.808978', '33.809278', '33.809578'] 
    
    for (var i = 0; i < 4; i++) {
        marker = new google.maps.Marker({
        position: new google.maps.LatLng(places[i], -117.918921),
        map: map,
        icon: icon
       });
    }
    
    map.addListener('zoom_changed', () => {
    
            //set the icon with the new size to the marker
      if (map.zoom>=20) {
             marker.icon.scaledSize = new google.maps.Size(50,58 );
             marker.icon.anchor = new google.maps.Point(25,58);
             }
      else if (map.zoom>=16) {
             marker.icon.scaledSize = new google.maps.Size(40,48 );
             marker.icon.anchor = new google.maps.Point(20,48);
             }
      else if (map.zoom>=13) {
             marker.icon.scaledSize = new google.maps.Size(30,38 );
             marker.icon.anchor = new google.maps.Point(15,38);
             }
      else if (map.zoom>=7) {
             marker.icon.scaledSize = new google.maps.Size(20,28 );
             marker.icon.anchor = new google.maps.Point(10,28);
             }
      else if (map.zoom>=5) {
             marker.icon.scaledSize = new google.maps.Size(10,18);
             marker.icon.anchor = new google.maps.Point(5,18);
            }
      else {
             marker.icon.scaledSize = new google.maps.Size(5,9);
             marker.icon.anchor = new google.maps.Point(2.5,9);
            }
      marker.setMap(map);
      //console.log(marker.icon);
    

      });

The demo is here .演示在这里 To check it just run the jsfiddle demo and do a Zoom in on the map.要检查它,只需运行 jsfiddle 演示并放大地图。

You need to keep references to all the markers, process through that array updating the size.您需要保留对所有标记的引用,通过该数组更新大小。 If you have lots of markers, this will take a while.如果您有很多标记,这将需要一段时间。

map.addListener('zoom_changed', () => {
  let scaledSize;
  let anchor;
  //set the icon with the new size to the marker
  if (map.zoom >= 20) {
    scaledSize = new google.maps.Size(50, 58);
    anchor = new google.maps.Point(25, 58);
  } else if (map.zoom >= 16) {
    scaledSize = new google.maps.Size(40, 48);
    anchor = new google.maps.Point(20, 48);
  } else if (map.zoom >= 13) {
    scaledSize = new google.maps.Size(30, 38);
    anchor = new google.maps.Point(15, 38);
  } else if (map.zoom >= 7) {
    scaledSize = new google.maps.Size(20, 28);
    anchor = new google.maps.Point(10, 28);
  } else if (map.zoom >= 5) {
    scaledSize = new google.maps.Size(10, 18);
    anchor = new google.maps.Point(5, 18);
  } else {
    scaledSize = new google.maps.Size(5, 9);
    anchor = new google.maps.Point(2.5, 9);
  }
  for (var i = 0; i < markers.length; i++) {
    var icon = markers[i].getIcon();
    icon.scaledSize = scaledSize;
    icon.anchor = anchor;
    markers[i].setIcon(icon);
  }
});

updated fiddle更新的小提琴

code snippet:代码片段:

 /** declare map as a global variable */ var map; /** create map */ var map = new google.maps.Map(document.getElementById("map_div"), { center: new google.maps.LatLng(33.808678, -117.918921), zoom: 14, mapTypeId: google.maps.MapTypeId.ROADMAP }); /* add markers to map */ var icon = { url: "https://irdp.bmc-hosting.com/assets/img/sonangol_marker.png", size: new google.maps.Size(50, 58), scaledSize: new google.maps.Size(20, 28), origin: new google.maps.Point(0, 0), // origin anchor: new google.maps.Point(10, 28) // anchor } var places = [{lat: 33.808678,lng: -117.918921},{lat: 33.808978, lng: -117.918921},{lat: 33.809278, lng: -117.918921},{lat: 33.809578, lng: -117.918921},{lat: 33.809578, lng: -117.908921},] var markers = []; for (var i = 0; i < places.length; i++) { var marker = new google.maps.Marker({ position: new google.maps.LatLng(places[i], ), map: map, icon: icon }); markers.push(marker); } map.addListener('zoom_changed', () => { let scaledSize; let anchor; //set the icon with the new size to the marker if (map.zoom >= 20) { scaledSize = new google.maps.Size(50, 58); anchor = new google.maps.Point(25, 58); } else if (map.zoom >= 16) { scaledSize = new google.maps.Size(40, 48); anchor = new google.maps.Point(20, 48); } else if (map.zoom >= 13) { scaledSize = new google.maps.Size(30, 38); anchor = new google.maps.Point(15, 38); } else if (map.zoom >= 7) { scaledSize = new google.maps.Size(20, 28); anchor = new google.maps.Point(10, 28); } else if (map.zoom >= 5) { scaledSize = new google.maps.Size(10, 18); anchor = new google.maps.Point(5, 18); } else { scaledSize = new google.maps.Size(5, 9); anchor = new google.maps.Point(2.5, 9); } for (var i = 0; i < markers.length; i++) { var icon = markers[i].getIcon(); icon.scaledSize = scaledSize; icon.anchor = anchor; markers[i].setIcon(icon); } });
 html, body { height: 100%; width: 100%; margin: 0; padding: 0; font: 12px sans-serif; } h1, p { margin: 0; padding: 0; } #map_div { height: 100%; width: 100%; }
 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <div id="map_div"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM