Source: bemap-services-v2/ev/bemap-ev-reachable-area-response.js

/**
 * BeNomad BeMap JavaScript API - EvReachableAreaResponse (v1.5)
 *
 * Strongly-typed response from `POST service/evreachablearea/1.0`. Mirrors
 * the server DTO `EvReachableAreaResponse.java` — payload is
 * `{boundingBox, geometry: [{lon, lat}, ...], encodedGeometry}`.
 *
 * @public
 * @constructor
 * @param {Object} [options]
 */
bemap.EvReachableAreaResponse = function(options) {
    var opts = options || {};
    this.boundingBox = opts.boundingBox || null;
    this.polygon = Array.isArray(opts.polygon) ? opts.polygon : [];
    this.encodedPolygon = opts.encodedPolygon || null;
};

/**
 * Polygon vertices as `bemap.Coordinate` instances, ready to feed into
 * `new bemap.Polygon(coords)`.
 * @public
 * @return {Array<bemap.Coordinate>}
 */
bemap.EvReachableAreaResponse.prototype.getPolygon = function() {
    return this.polygon;
};

/**
 * `bemap.BoundingBox` covering the polygon, or `null` when absent.
 * @public
 * @return {bemap.BoundingBox|null}
 */
bemap.EvReachableAreaResponse.prototype.getBoundingBox = function() {
    return this.boundingBox || null;
};

/**
 * Google-encoded polyline string (when the request asked for
 * `encodedPolyline: true`).
 * @public
 * @return {String|null}
 */
bemap.EvReachableAreaResponse.prototype.getEncodedPolygon = function() {
    return this.encodedPolygon;
};

/**
 * Parse the raw JSON response into a strongly-typed
 * `EvReachableAreaResponse`. Lifts the polygon to `bemap.Coordinate`
 * instances and the bounding box to `bemap.BoundingBox` when possible.
 *
 * @public
 * @param {Object} json
 * @return {bemap.EvReachableAreaResponse}
 */
bemap.EvReachableAreaResponse.fromJson = function(json) {
    if (!json) return new bemap.EvReachableAreaResponse();

    var polygon = [];
    if (Array.isArray(json.geometry)) {
        polygon = json.geometry.map(function(p) {
            var lon = (typeof p.getLon === 'function') ? p.getLon() : (p.lon !== undefined ? p.lon : p.longitude);
            var lat = (typeof p.getLat === 'function') ? p.getLat() : (p.lat !== undefined ? p.lat : p.latitude);
            return new bemap.Coordinate(lon, lat);
        });
    }

    var bbox = null;
    if (json.boundingBox) {
        var b = json.boundingBox;
        if (b.topLeft && b.bottomRight) {
            try {
                bbox = new bemap.BoundingBox(
                    new bemap.Coordinate(b.topLeft.lon, b.topLeft.lat),
                    new bemap.Coordinate(b.bottomRight.lon, b.bottomRight.lat)
                );
            } catch (e) {
                if (typeof console !== 'undefined' && console.warn) {
                    console.warn('bemap.EvReachableAreaResponse: bounding-box parse failed', e);
                }
                bbox = b;
            }
        } else {
            bbox = b;
        }
    }

    return new bemap.EvReachableAreaResponse({
        boundingBox: bbox,
        polygon: polygon,
        encodedPolygon: json.encodedGeometry || null
    });
};