【问题标题】:Google Maps API Custom Marker SVG Size and Position [duplicate]Google Maps API 自定义标记 SVG 大小和位置 [重复]
【发布时间】:2020-05-10 21:29:11
【问题描述】:

从 div 数据属性创建自定义 Google 地图标记 SVG 时,我将如何控制 SVG 的大小?默认情况下,谷歌正在让它变得巨大。如果我使用 CSS,则定位完全关闭。 Google API 文档与我在下面的文档有些不同,我不知道如何使它工作。任何信息表示赞赏!

这是标记 HTML:

<div class="marker" data-lat="<?php echo $loc_address_lat; ?>" data-lng="<?php echo $loc_address_lng; ?>" data-marker="/custom-pin-<?php echo $map_marker_color; ?>.svg">

这是创建标记的脚本部分。我的尝试(例如“scaledSize”)失败了。

// create marker
var marker = new google.maps.Marker({
    position : latlng,
    map : map,
    //icon : image // Custom image (comment out to disable),
    icon: $marker.attr('data-marker'),
    scaledSize: new google.maps.Size(10, 10),
});

这是一个小提琴: https://jsfiddle.net/inhouse/3xnz8yg9/2/

【问题讨论】:

标签: google-maps svg google-maps-api-3 google-maps-markers


【解决方案1】:

scaledSizeicon 的属性,而不是marker

对于超过 256 个标记,需要 marker 对象上的 optimized: false 以防止剪切。

fiddle with 2500 markers

像这样使用它:

var marker = new google.maps.Marker({
  position: latlng,
  map: map,
  icon: {
    url: $marker.attr('data-marker'),
    scaledSize: new google.maps.Size(30, 30),
  }
});

updated fiddle

代码 sn-p:

/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */

.large-acf-map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="large-acf-map">
  <div class="marker" data-lat="-25.363" data-lng="131.044" data-marker="https://s3-us-west-2.amazonaws.com/s.cdpn.io/134893/pin-red.svg"></div>
  <div class="marker" data-lat="-25.393" data-lng="131.044" data-marker="http://maps.google.com/mapfiles/ms/micons/blue.png"></div>

</div>

<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk">
</script>
<script type="text/javascript">
  (function($) {
    function render_map($el) {

      // var
      var $markers = $el.find('.marker');

      // vars
      var args = {
        zoom: 16,
        center: new google.maps.LatLng(0, 0),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        //mapTypeId : google.maps.MapTypeId.HYBRID
      };

      // create map               
      var map = new google.maps.Map($el[0], args);

      // add a markers reference
      map.markers = [];

      // add markers
      $markers.each(function() {

        add_marker($(this), map);

      });

      // center map
      center_map(map);

    }

    // create info window outside of each - then tell that singular infowindow to swap     content based on click
    var infowindow = new google.maps.InfoWindow({
      content: ''
    });

    /*
     *  add_marker
     *
     *  This function will add a marker to the selected Google Map
     *
     *  @type    function
     *  @date    8/11/2013
     *  @since   4.3.0
     *
     *  @param   $marker (jQuery element)
     *  @param   map (Google Map object)
     *  @return  n/a
     */

    function add_marker($marker, map) {
      // var
      var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng'));

      // create marker
      var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: {
          url: $marker.attr('data-marker'),
          scaledSize: new google.maps.Size(30, 30),
        }
      });
      // add to array
      map.markers.push(marker);

      // if marker contains HTML, add it to an infoWindow
      if ($marker.html()) {

        // show info window when marker is clicked & close other markers
        google.maps.event.addListener(marker, 'click', function() {
          //swap content of that singular infowindow
          infowindow.setContent($marker.html());
          infowindow.open(map, marker);
        });

        // close info window when map is clicked
        google.maps.event.addListener(map, 'click', function(event) {
          if (infowindow) {
            infowindow.close();
          }
        });
      }

    }

    /*
     *  center_map
     *
     *  This function will center the map, showing all markers attached to this map
     *
     *  @type    function
     *  @date    8/11/2013
     *  @since   4.3.0
     *
     *  @param   map (Google Map object)
     *  @return  n/a
     */

    function center_map(map) {

      // vars
      var bounds = new google.maps.LatLngBounds();

      // loop through all markers and create bounds
      $.each(map.markers, function(i, marker) {

        var latlng = new google.maps.LatLng(marker.position.lat(), marker.position.lng());

        bounds.extend(latlng);

      });

      // only 1 marker?
      if (map.markers.length == 1) {
        // set center of map
        map.setCenter(bounds.getCenter());
        map.setZoom(16);
      } else {
        // fit to bounds
        map.fitBounds(bounds);
      }

    }

    /*
     *  document ready
     *
     *  This function will render each map when the document is ready (page has loaded)
     *
     *  @type    function
     *  @date    8/11/2013
     *  @since   5.0.0
     *
     *  @param   n/a
     *  @return  n/a
     */

    $(document).ready(function() {

      $('.large-acf-map').each(function() {

        render_map($(this));

      });

    });

  })(jQuery);
</script>

【讨论】:

  • 太棒了!这样可行;但是,我注意到如果有超过 255 个标记,SVG 就会被剪裁。我一直在玩“size”、“origin”、“anchor”和“scaledSize”几个小时。我尝试了不同的 SVG 图像,但已排除此变量。这是一个更新的小提琴,包含超过 254 个测试位置:jsfiddle.net/inhouse/7x9Lq43r/24
  • 这是一个不同的问题。
  • 是吗?似乎答案只能部分解决问题。 SVG 在某些情况下会调整大小,但在其他情况下不会。我非常感谢这个答案,但我不能在我的开发中使用它,因为缩放时图标都被剪掉了。
  • 此问题已结束。
  • 将优化设置为 false(在标记上),它可以不受限制地工作(性能可能会受到影响,标记越多,加载和缩放越慢)。 proof of concept fiddle
猜你喜欢
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 2017-04-10
  • 2014-02-25
  • 2012-02-08
  • 2016-11-06
相关资源
最近更新 更多