How could I rotate an image (marker image) on a Google map V3?
- There is an excellent example for V2 here, exactly doing what I need. But for GMap2! They do it with a rotating canvas.
- Image rotating with JS / JQuery is frequently used, there are multiple answers about this. But how could I apply this to my maps image?
- One mentioned approach is to have different images for different angles and to switch among them - this is NOT what I want. I do not like to have so many images, I want to rotate by code.
Remark: There are similar questions, but all for V2 and not V3 (as far I can tell). I need it for V3.
How could I rotate an image (marker image) on a Google map V3?
I had the same problem and I solved it with the next code:
I suppose that you have a variable with the icon, for example:
First, we need to create our marker options:
Let's create a marker:
And we have to set the map:
Now if you want to rotate the image you need to do the next:
Let's see
Of course you can do it fancier:
And your call:
That's all.
I hope it could be helpful.
You did not state it in your question, but I am assuming that you want this rotation in relation to a line between point a and point b, which would be their path. In order to make a google svg icon that can be rotated, you will want to use the google symbol class object to define the properties of your marker symbol. This does not use a full .svg file, but only the d attribute of the path. Note that the google symbol class can only take one path per marker.
Additional attributes for color, stroke, width, opacity, etc. may be set after the marker has been created with javascript (updating the marker object properties directly), or with CSS (updating the marker properties by adding and removing classes).
As an example, the following will create an arrow marker that can be dragged, and it will be rotated around the point on the map that is the lat and long for the marker even after it is moved.
The HTML
The CSS (yes,verbose... I hate ugly)
The Javascript (in plain vanilla flavor for understanding core concepts)
And the best yet, doing it this way assures the marker is a Google MVC object, giving it all the additional methods provided by the MVC object.
If you must have multi-colored images as your marker, then creating a .png sprite sheet with a rendition of the image at all the angles you want it to be shown, and then problematically select the correct image to use based on the computed bearing between the two points you are using. However,this would not be an SVG image, but a regular marker image.
Hope this helps in making some decisions regarding your map markers.
My js class for solving this problem is:
Call it like this:
See live example here http://jsfiddle.net/fe9grwdf/39/
I was able to solve this pretty easily but using the marker.icon.rotation option pointing to a custom symbol that uses the svg path syntax.
If using angular-google-maps it is trivial to bind a ui control to change the triangle.rotation.
Like I did with this slider.
But you could use a forum too.
here is my plunker http://plnkr.co/edit/x0egXI
The easiest way may be to use the rotation property of google.maps.Symbol. Just set it as a property of your icon when creating or updating your marker:
Plunker
This is how i implemented my image rotated, I considered the marker in the form of overlay and that overlay is position to the position, Below code will be added .
Without using any additional library it is rotated,And you need to workaround to add click events and mouse events for the overlay, not similar to marker click events.
With googleMap markers customization, there will be addition memory usage in the map. This will also reduce the memory consumption of custom markers in your map.