vee
vee

Reputation: 141

Use an SVG for a marker in Google Maps?

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 nothing shows up. let me know if i should post my code, but i think i am probably approaching it in the wrong way.

thanks.

Upvotes: 14

Views: 24161

Answers (5)

moji
moji

Reputation: 2739

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: enter image description here

Upvotes: 1

Chris Aelbrecht
Chris Aelbrecht

Reputation: 2091

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>

Upvotes: 6

Eugene Godun
Eugene Godun

Reputation: 376

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.

Upvotes: 0

Thomas
Thomas

Reputation: 153

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">

Upvotes: 10

Yuval Adam
Yuval Adam

Reputation: 165340

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

Upvotes: 22

Related Questions