angular-heremaps
Version:
Angular directive for working with Nokia Here Maps
145 lines (117 loc) • 3.9 kB
JavaScript
module.exports = HereMapsUtilsService;
HereMapsUtilsService.$inject = [
'$rootScope',
'$timeout',
'HereMapsCONSTS'
];
function HereMapsUtilsService($rootScope, $timeout, HereMapsCONSTS) {
return {
throttle: throttle,
createScriptTag: createScriptTag,
createLinkTag: createLinkTag,
runScopeDigestIfNeed: runScopeDigestIfNeed,
isValidCoords: isValidCoords,
addEventListener: addEventListener,
zoom: zoom,
getBoundsRectFromPoints: getBoundsRectFromPoints,
generateId: generateId,
getMapFactory: getMapFactory
};
//#region PUBLIC
function throttle(fn, period) {
var timeout = null;
return function () {
if ($timeout)
$timeout.cancel(timeout);
timeout = $timeout(fn, period);
}
}
function addEventListener(obj, eventName, listener, useCapture) {
obj.addEventListener(eventName, listener, !!useCapture);
}
function runScopeDigestIfNeed(scope, cb) {
if (scope.$root && scope.$root.$$phase !== '$apply' && scope.$root.$$phase !== '$digest') {
scope.$digest(cb || angular.noop);
return true;
}
return false;
}
function createScriptTag(attrs) {
var script = document.getElementById(attrs.src);
if (script)
return false;
script = document.createElement('script');
script.type = 'text/javascript';
script.id = attrs.src;
_setAttrs(script, attrs);
return script;
}
function createLinkTag(attrs) {
var link = document.getElementById(attrs.href);
if (link)
return false;
link = document.createElement('link');
link.id = attrs.href;
_setAttrs(link, attrs);
return link;
}
function isValidCoords(coords) {
return coords &&
(typeof coords.latitude === 'string' || typeof coords.latitude === 'number') &&
(typeof coords.longitude === 'string' || typeof coords.longitude === 'number')
}
function zoom(map, value, step) {
var currentZoom = map.getZoom(),
_step = step || HereMapsCONSTS.ANIMATION_ZOOM_STEP,
factor = currentZoom >= value ? -1 : 1,
increment = step * factor;
return (function zoom() {
if (!step || Math.floor(currentZoom) === Math.floor(value)) {
map.setZoom(value);
return;
}
currentZoom += increment;
map.setZoom(currentZoom);
requestAnimationFrame(zoom);
})();
}
function getMapFactory(){
return H;
}
/**
* @method getBoundsRectFromPoints
*
* @param {Object} topLeft
* @property {Number|String} lat
* @property {Number|String} lng
* @param {Object} bottomRight
* @property {Number|String} lat
* @property {Number|String} lng
*
* @return {H.geo.Rect}
*/
function getBoundsRectFromPoints(topLeft, bottomRight) {
return H.geo.Rect.fromPoints(topLeft, bottomRight, true);
}
function generateId() {
var mask = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
regexp = /[xy]/g,
d = new Date().getTime(),
uuid = mask.replace(regexp, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
}
//#endregion PUBLIC
function _setAttrs(el, attrs) {
if (!el || !attrs)
throw new Error('Missed attributes');
for (var key in attrs) {
if (!attrs.hasOwnProperty(key))
continue;
el[key] = attrs[key];
}
}
};