Git Product home page Git Product logo

Comments (1)

mludlum avatar mludlum commented on May 24, 2024 1

I got this to work by rotating the canvas using CSS. Search for "//rotate" in code below.

  1. I'm using @rcknr's pull request: #9
  2. set default property in constructor.
  3. added rotation update to redraw in changed
  4. Updated draw to set transform CSS property on canvas.
MapLabel = function (opt_options) {
    if (!MapLabel.prototype.setValues) {
        for (var property in google.maps.OverlayView.prototype) {
            if (!MapLabel.prototype.hasOwnProperty(property)) {
                MapLabel.prototype[property] = google.maps.OverlayView.prototype[property];
            }
        }
    }

    this.set('align', 'center');
    this.set('fontColor', '#000000');
    this.set('fontFamily', 'Roboto,Arial,sans-serif');
    this.set('fontSize', 12);
    this.set('rotation', 0);    // rotate
    this.set('strokeColor', '#ffffff');
    this.set('strokeWeight', 1);
    this.set('zIndex', 1e3);

    this.setValues(opt_options);
}

window['MapLabel'] = MapLabel;


/** @inheritDoc */
MapLabel.prototype.changed = function (prop) {
    switch (prop) {
        case 'fontFamily':
        case 'fontSize':
        case 'fontColor':
        case 'rotation': // rotate
        case 'strokeWeight':
        case 'strokeColor':
        case 'text':
            this.drawCanvas_();
        case 'align':
        case 'maxZoom':
        case 'minZoom':
        case 'position':
            return this.draw();
    }
};

/**
 * @inheritDoc
 */
MapLabel.prototype.draw = function () {
    var projection = this.getProjection();

    if (!projection) {
        // The map projection is not ready yet so do nothing
        return;
    }

    if (!this.canvas_) {
        // onAdd has not been called yet.
        return;
    }

    var latLng = /** @type {google.maps.LatLng} */ (this.get('position'));
    if (!latLng) {
        return;
    }
    var pos = projection.fromLatLngToDivPixel(latLng);

    var style = this.canvas_.style;

    style['top'] = pos.y + 'px';
    style['transform'] = 'scale(' + this.map.getZoom() / 17 + ') rotate(' + this.get('rotation') + 'rad)'; //rotate

    switch (this.get('align')) {
        case 'left':
            style['left'] = pos.x - (this.canvas_.width / (window.devicePixelRatio ? window.devicePixelRatio : 1)) + 'px';
            style['margin-left'] = '-1em';
            style['margin-top'] = '-0.4em';
            break;
        case 'right':
            style['left'] = pos.x + 'px';
            style['margin-left'] = '1em';
            style['margin-top'] = '-0.4em';
            break;
        default:
            style['left'] = (pos.x - (this.canvas_.width / (window.devicePixelRatio ? window.devicePixelRatio : 1)) / 2) + 'px';
            style['margin-left'] = 0;
            style['margin-top'] = '1em';
    }

    style['visibility'] = this.getVisible_();
};

`

from js-map-label.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.