Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps - SVG marker cut off

Why my svg icons for google.maps.Marker are cut off? I tried many of combinations with size and scaledSize...

var marker = new google.maps.Marker({
            position: new google.maps.LatLng({lat: el.lat, lng: el.lon}),
            icon: {
                url: "/images/icons/observation_" + el.rate + ".svg",
                size: new google.maps.Size(20, 20),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(10, 10),
                scaledSize: new google.maps.Size(20, 20)
            },
            zIndex: 6,
            map: map.map
        });

Svg icons should by 20x20 px.

https://jsfiddle.net/d3v5huzr/

Here is preview: Google Maps SVG markers

like image 712
Skodsgn Avatar asked May 30 '17 10:05

Skodsgn


2 Answers

You have to set width and height attributes of the svg element like this:

<svg height="40px" width="40px" ... >

https://jsfiddle.net/zvtnohyv/

like image 131
Vyacheslav Nazarov Avatar answered Nov 14 '22 22:11

Vyacheslav Nazarov


You can use zoom_changed from events and add event Listener, for refreshing the marker icon on zoom

Fiddle Demo

Code below

var map;

map = new google.maps.Map(document.getElementById('map'), {
  center: {
    lat: -34.397,
    lng: 150.644
  },
  zoom: 8
});
var icon = {
  url: "http://svgshare.com/i/1jz.svg",
}
var marker = new google.maps.Marker({
  position: new google.maps.LatLng({
    lat: -34.397,
    lng: 150.644
  }),
  icon: icon,
  /*icon: {
    url: "http://svgshare.com/i/1jz.svg",
    // size: new google.maps.Size(20, 20),
    // origin: new google.maps.Point(5, 5),
    // anchor: new google.maps.Point(10, 10),
    // scaledSize: new google.maps.Size(20, 20)
  },*/
  zIndex: 6,
  map: map
});
google.maps.event.addListener(map, 'zoom_changed', function() {
  marker.setIcon(icon);
});
like image 24
Deep 3015 Avatar answered Nov 14 '22 22:11

Deep 3015