/**
* BeNomad BeMap JavaScript API - NearPoint
*
* One POI in the array returned by `bemap.NearPoiSearch.nearPoi()`.
*
* Wire shape from the BeMap server (cross-checked against a real
* `POST service/nearpoi/1.0` response):
*
* {
* coordinate: { lon, lat }, // POI location
* distance: long, // straight-line distance from the search centre, in metres
* duration: long, // estimated travel duration for the active transport mode, in seconds
* type: string, // POI category code, e.g. "RESTAURANT" / "HOTEL_MOTEL" / "RENT_A_CAR" / "PHARMACY" / ...
* name: string, // POI display name (may be empty for unnamed POIs like parking areas)
* telephone: string // POI phone number when known (may be empty)
* }
*
* @since 1.5.0
* @public
* @constructor
* @param {Object} [options]
* @param {bemap.Coordinate} [options.coordinate]
* @param {Number} [options.distance] Metres.
* @param {Number} [options.duration] Seconds.
* @param {String} [options.type] Server category code.
* @param {String} [options.name]
* @param {String} [options.telephone]
* @param {String} [options.id] Convenience; not emitted by the server.
* @param {bemap.PostalAddress} [options.postalAddress] Reserved for future server fields.
* @param {Object} [options.extra]
*/
bemap.NearPoint = function(options) {
var opts = options || {};
this.coordinate = opts.coordinate || null;
this.distance = (typeof opts.distance === 'number') ? opts.distance : null;
this.duration = (typeof opts.duration === 'number') ? opts.duration : null;
this.type = opts.type || null;
// name / telephone use a type-check rather than `||` so the server's
// empty-string values (parking areas, partial records) survive.
this.name = (typeof opts.name === 'string') ? opts.name : null;
this.telephone = (typeof opts.telephone === 'string') ? opts.telephone : null;
this.id = opts.id || null;
this.postalAddress = opts.postalAddress || null;
this.extra = opts.extra || null;
};
bemap.NearPoint.prototype.getCoordinate = function() { return this.coordinate; };
/** @public — metres. */
bemap.NearPoint.prototype.getDistance = function() { return this.distance; };
/** @public — seconds for the active transport mode. */
bemap.NearPoint.prototype.getDuration = function() { return this.duration; };
/** @public — server category code, e.g. "RESTAURANT", "HOTEL_MOTEL". */
bemap.NearPoint.prototype.getType = function() { return this.type; };
bemap.NearPoint.prototype.getName = function() { return this.name; };
bemap.NearPoint.prototype.getTelephone = function() { return this.telephone; };
bemap.NearPoint.prototype.getId = function() { return this.id; };
bemap.NearPoint.prototype.getPostalAddress = function() { return this.postalAddress; };
bemap.NearPoint.prototype.getExtra = function() { return this.extra; };
/* Compatibility aliases — earlier drafts used `category` / `poiClass`. */
bemap.NearPoint.prototype.getCategory = function() { return this.type; };
bemap.NearPoint.prototype.getPoiClass = function() { return this.type; };
bemap.NearPoint.fromJson = function(raw) {
if (!raw) return new bemap.NearPoint();
var coord = null;
if (raw.coordinate) {
var lon = (raw.coordinate.lon !== undefined)
? raw.coordinate.lon
: (raw.coordinate.longitude !== undefined) ? raw.coordinate.longitude : null;
var lat = (raw.coordinate.lat !== undefined)
? raw.coordinate.lat
: (raw.coordinate.latitude !== undefined) ? raw.coordinate.latitude : null;
if (lon !== null && lat !== null) coord = new bemap.Coordinate(lon, lat);
}
var postal = null;
if (raw.postalAddress) {
postal = new bemap.PostalAddress({
countryCode: raw.postalAddress.countryCode,
country: raw.postalAddress.country,
state: raw.postalAddress.state,
county: raw.postalAddress.county,
city: raw.postalAddress.city,
district: raw.postalAddress.district,
postalCode: raw.postalAddress.postalCode,
street: raw.postalAddress.street,
streetNumber: raw.postalAddress.streetNumber
});
}
return new bemap.NearPoint({
coordinate: coord,
distance: (typeof raw.distance === 'number') ? raw.distance : null,
duration: (typeof raw.duration === 'number') ? raw.duration : null,
type: raw.type || raw.poiClass || raw['class'] || raw.category || null,
name: (typeof raw.name === 'string') ? raw.name : null,
telephone: (typeof raw.telephone === 'string') ? raw.telephone : null,
id: raw.id || null,
postalAddress: postal,
extra: raw.extra || null
});
};