Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing SVG markers in Google Maps

Are there good ways to optimize how Google Maps API v3 draw markers to the HTML document when using SVG symbols for the icons? Here is an example of a marker using SVG symbols:

  var star = {
    path: 'M 125,5 155,90 245,90 175,145 200,230 125,180 50,230 75,145 5,90 95,90 z',
    fillColor: 'yellow'
  };

  var marker = new google.maps.Marker({
    position: map.getCenter(),
    icon: star,
    map: map,
    optimized: true //Does not seem to do anything..
  });

Google has this property called "optimized" that can be set on markers, and when using for instance PNG icons instead of SVG drawings this works fine: Google creates one single canvas per tile instead of one img per icon, which is obviously scaling much better.

The "optimized" property does however not seem to do anything when using SVG markers: They always seems to be drawn in one single canvas per marker. When I have many markers, certain browsers start having performance issues. Chrome usually handles it fine, but IE9 for instance, does not handle many markers before showing significant performance issues. Clustering is not an option in this case.

Does anyone know any good ways of optimizing this, so that the browsers can handle more SVG markers at the same time?

like image 364
Knut Marius Avatar asked Feb 14 '14 13:02

Knut Marius


People also ask

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

Is there a SVG marker for Google Maps endevours?

This is exactly what Google does for it's own few symbols available in the SYMBOL class of the Maps API, so if that doesn't convince you... Anyway, I hope this will help you to correctly make and set up a SVG marker for your Google maps endevours.

What is an SVG marker?

An svg marker uses properties of the symbol class, which are then assigned as the icon property of the marker class. (see MrUpsidown icon object for a correct reference...) – Epiphany Mar 2, 2015 at 17:51 2 This will also break if the SVG has more than one 'd' path (usually associated with more than one layer in the svg image). – Epiphany

What is the best way to add markers to a map?

Use raster images, such as images in .PNG or .JPG format, when adding markers to identify a location on a map. Avoid using Scalable Vector Graphics (SVG) images, since rendering SVG images can introduce lag when the map is redrawn. Optimization enhances performance by rendering many markers as a single static element.

Why can't SVG be used outside of Google Maps?

@MrUpsidown But of course SVG is working fine outside of maps - never had an issue there. It has to do with a glitch in Google's support for SVG in their canvas rendering - which can be turned off so that SVGs are inserted into the DOM instead.


1 Answers

How complicated are the SVG icons you are generating? Because you could also draw the icon on a canvas object yourself and use the toDataURL method to generate a data-uri for use in Google Maps. This would actually generate a bitmap image so that might work in IE9 (at least the method is supported).

You can also draw a SVG on a canvas but there are some caveats if you want to programatically alter them. And IE9 will probably not cooperate so I am not sure if that's the way to go.

like image 105
meinaart Avatar answered Oct 13 '22 16:10

meinaart