How could I rotate an image (marker image) on a Google map V3?
Remark: There are similar questions, but all for V2 and not V3 (as far I can tell). I need it for V3.
Check the auto-rotate settings If you're using Google Maps to navigate while you're driving, make sure the Android Auto is left out of battery settings as well. ⇒ Note: If you're driving, you can only change from north up to car direction after you set the destination.
Use the SHIFT key + the left and right arrow keys to rotate your view. Use the SHIFT key + the up and down arrow keys to move you forward or backwards.
My js class for solving this problem is:
var RotateIcon = function(options){ this.options = options || {}; this.rImg = options.img || new Image(); this.rImg.src = this.rImg.src || this.options.url || ''; this.options.width = this.options.width || this.rImg.width || 52; this.options.height = this.options.height || this.rImg.height || 60; var canvas = document.createElement("canvas"); canvas.width = this.options.width; canvas.height = this.options.height; this.context = canvas.getContext("2d"); this.canvas = canvas; }; RotateIcon.makeIcon = function(url) { return new RotateIcon({url: url}); }; RotateIcon.prototype.setRotation = function(options){ var canvas = this.context, angle = options.deg ? options.deg * Math.PI / 180: options.rad, centerX = this.options.width/2, centerY = this.options.height/2; canvas.clearRect(0, 0, this.options.width, this.options.height); canvas.save(); canvas.translate(centerX, centerY); canvas.rotate(angle); canvas.translate(-centerX, -centerY); canvas.drawImage(this.rImg, 0, 0); canvas.restore(); return this; }; RotateIcon.prototype.getUrl = function(){ return this.canvas.toDataURL('image/png'); };
Call it like this:
var marker = new google.maps.Marker({ icon: { url: RotateIcon .makeIcon( 'https://ru.gravatar.com/userimage/54712272/b8eb5f2d540a606f4a6c07c238a0bf40.png') .setRotation({deg: 92}) .getUrl() }})
See live example here http://jsfiddle.net/fe9grwdf/39/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With