Source: bemap-model/bemap-polyline.js

/**
 * BeNomad BeMap JavaScript API - Polyline class
 */

/**
 * @classdesc
 * Base class for Polyline.
 * @public
 * @constructor
 * @param {bemap.Coordinate} coordinates Array of bemap.Coordinate.
 * @param {Object} options See below the available values.
 * @param {bemap.LineStyle} options.style Style of poyline used by the renderer.
 * @param {String} options.name Name of polyline.
 * @param {Object} options.properties custom object.
 */
bemap.Polyline = function(coordinates, options) {
    var opts = options || {};

    /**
     * @type {Object}
     * @protected
     */
    this.native = null;

    /**
     * @type {Object}
     * @protected
     */
    this.map = null;

    /**
     * @type {Object}
     * @protected
     */
    this.layer = null;

    /**
     * @type {bemap.Coordinate[]}
     * @protected
     */
    this.coords = coordinates ? coordinates : [];

    /**
     * @type {
     bemap.LineStyle
 }
     * @protected
     */
    this.style = opts.style ? opts.style : null;


    /**
     * @type {String}
     * @protected
     */
    this.id = opts.id ? opts.id : null;

    /**
     * @type {String}
     * @protected
     */
    this.name = opts.name ? opts.name : null;

    /**
     * @type {Array}
     * @protected
     */
    this.events = [];

    /**
     * @type {Function}
     * @protected
     */
    this.callback = [];

    /**
     * @type {Object}
     * @protected
     */
    this.properties = opts.properties ? opts.properties : null;
};

/**
 * Add a coordinate.
 * @public
 * @param {bemap.Coordinate} coordinate bemap.Coordinate.
 * @return {bemap.Polyline} this
 */
bemap.Polyline.prototype.addCoordinate = function(coordinate) {
    if (coordinate === null || !bemap.inheritsof(coordinate, bemap.Coordinate)) {
        throw new bemap.Exception("coordinate is not an bemap.Coordinate object");
    }
    this.coords.push(coordinate);
    return this;
};

/**
 * Add a coordinate.
 * @public
 * @param {double} lon Longitude in degres deciaml (WGS84).
 * @param {double} lat Latitude in degres deciaml (WGS84).
 * @return {bemap.Polyline} this
 */
bemap.Polyline.prototype.addLonLat = function(lon, lat) {
    this.coords.push(new bemap.Coordinate(lon, lat));
    return this;
};

/**
 * Return an array with longitude and latitude in degres decimal (WGS94).
 * @public
 * @param {int} index Index of coordinate in polyline.
 * @return {array} Return an array with longitude and latitude in degres decimal (WGS94).
 */
bemap.Polyline.prototype.getLonLatArray = function(index) {
    return this.coords[index].getLonLatArray();
};

/**
 * Return an array with latitude and longitude in degres decimal (WGS94).
 * @public
 * @param {int} index Index of coordinate in polyline.
 * @return {array} Return an array with latitude and longitude in degres decimal (WGS94).
 */
bemap.Polyline.prototype.getLatLonArray = function(index) {
    return this.coords[index].getLatLonArray();
};

/**
 * Return an array with longitude and latitude in degres decimal (WGS94).
 * Example: [ [lon, lat], [lon, lat], ... ]
 * @public
 * @return {array} Return an array with longitude and latitude in degres decimal (WGS94).
 */
bemap.Polyline.prototype.getLonLatArrays = function() {
    var i = 0,
        iLen = this.coords.length,
        rets = [],
        c;
    for (; i < iLen; i++) {
        c = this.coords[i];
        rets.push([c.lon, c.lat]);
    }
    return rets;
};

/**
 * Return an array with latitude and longitude in degres decimal (WGS94).
 * Example: [ [lat, lon], [lat, lon], ... ]
 * @public
 * @return {array} Return an array with latitude and longitude in degres decimal (WGS94).
 */
bemap.Polyline.prototype.getLatLonArrays = function() {
    var i = 0,
        iLen = this.coords.length,
        rets = [],
        c;
    for (; i < iLen; i++) {
        c = this.coords[i];
        rets.push([c.lat, c.lon]);
    }
    return rets;
};

/**
 * Return the id of the polyline.
 * @public
 * @return {String} Return the id of the polyline.
 */
bemap.Polyline.prototype.getId = function() {
    return this.id;
};

/**
 * Define the id of polyline
 * @public
 * @param {String} id the new id to set.
 * @return {bemap.Polyline} this
 */
bemap.Polyline.prototype.setId = function(id) {
    this.id = id;
    return this;
};

/**
 * Return the name of the polyline.
 * @public
 * @return {String} Return the name of the polyline.
 */
bemap.Polyline.prototype.getName = function() {
    return this.name;
};

/**
 * Define the name of polyline
 * @public
 * @param {String} name the new name to set.
 * @return {bemap.Polyline} this
 */
bemap.Polyline.prototype.setName = function(name) {
    this.name = name;
    return this;
};

/**
 * Return the coordinate. See bemap.Coordinate.
 * @public
 * @param {int} index Index of coordinate in polyline.
 * @return {bemap.Coordinate} Return the coordinate. See bemap.Coordinate.
 */
bemap.Polyline.prototype.getCoordinate = function(index) {
    return this.coords[index];
};

/**
 * Return an array of coordinate. See bemap.Coordinate.
 * @public
 * @return {bemap.Coordinate} Return an array of the coordinate. See bemap.Coordinate.
 */
bemap.Polyline.prototype.getCoordinates = function() {
    return this.coords;
};

/**
 * Return the style of the polyline.
 * @public
 * @return {bemap.LineStyle} Return the style of the polyline.
 */
bemap.Polyline.prototype.getStyle = function() {
    return this.style;
};

/**
 * Remove the Polyline from the layer.
 * @public
 * @return {bemap.Polyline} this
 */
bemap.Polyline.prototype.remove = function() {
    if (this.map !== null && bemap.inheritsof(this.map, bemap.Map)) {
        this.map.removePolyline(this);
    }
    return this;
};

/**
 * Set the listner when an specified eventType occur on bemap.Polyline.
 * @public
 * @param {bemap.Map.EventType} eventType Event type.
 * @param {function} callback Function will be called when the specified eventType is occur.
 * @param {object} options options.
 * @return {bemap.Polyline} this.
 */
bemap.Polyline.prototype.on = function(eventType, callback, options) {
    if (this.map !== null && bemap.inheritsof(this.map, bemap.Map)) {
        this.map.onPolyline(this, eventType, callback, options);
    }
    return this;
};

/**
 * Define the draggable capability for bemap.Polyline.
 * @protected
 * @param {function} callback Function will be called when the specified eventType is occur.
 * @param {object} options Options.
 * @param {bemap.Layer} options.layerFilter set the bemap layer used as filter.
 * @return {bemap.Listener} bemap.listener.
 */
bemap.Polyline.prototype.draggable = function(callback, options) {
    if (this.map !== null && bemap.inheritsof(this.map, bemap.Map)) {
        return this.map.draggablePolyline(this, callback, options);
    }
    return new bemap.listener();
};