Use an SVG for a marker in Google Maps?

前端 未结 5 676
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 18:43

i know it\'s possible to add svg overlays to google maps. i\'m wondering if you can use svg files as markers. i tried setting it just like you would a png or jpg file, but nothi

相关标签:
5条回答
  • 2021-02-05 19:00
    var marker = new google.maps.Marker({
            icon: {
                size: new google.maps.Size(21, 45),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(21, 45),
                url: `data:image/svg+xml;utf-8,
                            <svg width="21" height="45" viewBox="0 0 21 45" xmlns="http://www.w3.org/2000/svg"><title>POINT</title><desc>Created with Sketch.</desc><g transform="translate(-651 -1250) translate(651 1250)" fill="#F76A4C"><circle cx="10.5" cy="10.5" r="10.5"/><path d="M9 19h3v26h-3z"/></g></svg>`
            }
        });
    

    Live example on codepen.

    0 讨论(0)
  • 2021-02-05 19:01

    I had a problem with this but I looked at the svg in a code editor and it was missing a defined width and height.

    Did not work

    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" 
        x="0px" y="0px" 
        viewBox="0 0 419.5 595.3" 
        enable-background="new 0 0 419.5 595.3"
        xml:space="preserve">
    

    Did work

    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" 
        x="0px" y="0px" 
        width="33px" height="50px" 
        viewBox="0 0 419.5 595.3" 
        enable-background="new 0 0 419.5 595.3" 
        xml:space="preserve">
    
    0 讨论(0)
  • 2021-02-05 19:09

    On Google Maps API v3 it's working just fine for me with this code (which also handles resizing):

    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat, lng),
      icon: new google.maps.MarkerImage('/path/to/icon.svg',
        null, null, null, new google.maps.Size(64,64)),
      draggable: false,
      map: map
    });
    
    0 讨论(0)
  • 2021-02-05 19:14

    When referencing an external SVG you need to use scaledSize instead of size for the icon. See code snippet below...

    function initMap() {
      var springfield = {
        lat: 39.9354165,
        lng: -83.8215624
      };
    
      var homer = {
        url: 'http://thenewcode.com/assets/images/thumbnails/homer-simpson.svg',
        scaledSize: new google.maps.Size(64, 64),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(0, 32)
      }
    
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: springfield
      });
    
      var marker = new google.maps.Marker({
        position: springfield,
        map: map,
        icon: homer,
        draggable: true
      });
    }
    #map {
      height: 400px;
      width: 100%;
    }
    <script async defer src="//maps.googleapis.com/maps/api/js?callback=initMap"></script>
    <div id="map"></div>

    0 讨论(0)
  • 2021-02-05 19:20

    As some mentioned above, scaledSize is the property that makes the magic happens:

      window.userimage = {
        url: '/img/user.svg',
        scaledSize: new google.maps.Size(64, 64),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(0, 0)
      };
    
    
      window.usermarker = new google.maps.Marker({
        map: minimap,
        animation: google.maps.Animation.DROP,
        anchorPoint: new google.maps.Point(0, -29),
        icon: userimage,
      });
    

    Result:

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