2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
|
|
|
var assert = require('assert');
|
|
|
|
var loopback = require('../');
|
|
|
|
var GeoPoint = loopback.GeoPoint;
|
|
|
|
|
2013-06-24 23:30:09 +00:00
|
|
|
describe('GeoPoint', function() {
|
|
|
|
describe('geoPoint.distanceTo(geoPoint, options)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Get the distance to another `GeoPoint`', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var here = new GeoPoint({lat: 10, lng: 10});
|
|
|
|
var there = new GeoPoint({lat: 5, lng: 5});
|
|
|
|
var distance = here.distanceTo(there, {type: 'meters'});
|
2014-05-16 14:20:34 +00:00
|
|
|
|
|
|
|
assert.equal(Math.floor(distance), 782777);
|
2013-06-24 23:30:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GeoPoint.distanceBetween(a, b, options)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Get the distance between two points', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var here = new GeoPoint({lat: 10, lng: 10});
|
|
|
|
var there = new GeoPoint({lat: 5, lng: 5});
|
|
|
|
var distance = GeoPoint.distanceBetween(here, there, {type: 'feet'});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-16 14:20:34 +00:00
|
|
|
assert.equal(Math.floor(distance), 2568169);
|
2013-06-24 23:30:09 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('GeoPoint()', function() {
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Create from string', function() {
|
2013-06-26 03:29:58 +00:00
|
|
|
var point = new GeoPoint('1.234,5.678');
|
2015-01-07 00:02:24 +00:00
|
|
|
assert.equal(point.lat, 1.234);
|
|
|
|
assert.equal(point.lng, 5.678);
|
2013-06-26 03:29:58 +00:00
|
|
|
var point2 = new GeoPoint('1.222, 5.333');
|
2015-01-07 00:02:24 +00:00
|
|
|
assert.equal(point2.lat, 1.222);
|
|
|
|
assert.equal(point2.lng, 5.333);
|
2013-06-26 03:29:58 +00:00
|
|
|
var point3 = new GeoPoint('1.333, 5.111');
|
2015-01-07 00:02:24 +00:00
|
|
|
assert.equal(point3.lat, 1.333);
|
|
|
|
assert.equal(point3.lng, 5.111);
|
2013-06-26 03:29:58 +00:00
|
|
|
});
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Serialize as string', function() {
|
2013-06-26 03:29:58 +00:00
|
|
|
var str = '1.234,5.678';
|
|
|
|
var point = new GeoPoint(str);
|
|
|
|
assert.equal(point.toString(), str);
|
|
|
|
});
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Create from array', function() {
|
2013-06-26 03:29:58 +00:00
|
|
|
var point = new GeoPoint([5.555, 6.777]);
|
2015-01-07 00:02:24 +00:00
|
|
|
assert.equal(point.lat, 5.555);
|
|
|
|
assert.equal(point.lng, 6.777);
|
2013-06-26 03:29:58 +00:00
|
|
|
});
|
|
|
|
it('Create as Model property', function() {
|
2013-07-16 17:49:25 +00:00
|
|
|
var Model = loopback.createModel('geo-model', {
|
2016-11-15 21:46:23 +00:00
|
|
|
geo: {type: 'GeoPoint'},
|
2013-06-26 03:29:58 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-06-26 03:29:58 +00:00
|
|
|
var m = new Model({
|
2016-04-01 09:14:26 +00:00
|
|
|
geo: '1.222,3.444',
|
2013-06-26 03:29:58 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-06-26 03:29:58 +00:00
|
|
|
assert(m.geo instanceof GeoPoint);
|
2015-01-07 00:02:24 +00:00
|
|
|
assert.equal(m.geo.lat, 1.222);
|
|
|
|
assert.equal(m.geo.lng, 3.444);
|
2013-06-26 03:29:58 +00:00
|
|
|
});
|
|
|
|
});
|
2014-02-12 00:01:51 +00:00
|
|
|
});
|