acha-framework
Version:
is a modular framework on both client (angular.js) and server (node.js) side, it provides security, orm, ioc, obfuscation and ...
217 lines • 7.42 kB
JavaScript
(function ($, angular, underscore, window, document, undefined) {
'use strict';
angular.module('frontend.directives').factory('googleMapService', [
'$q',
function ($q) {
return function () {
return $q(function (resolve, reject) {
if (window.google && window.google.maps) {
resolve();
return;
}
var mapHref = '//maps.googleapis.com/maps/api/js?key=' + window.achasoft.configuration.google.map.key;
$script(mapHref, function () {
resolve();
});
});
};
}
]).directive('googleMap', [
'googleMapService',
function (googleMapService) {
return {
restrict: 'E',
replace: true,
scope: {
tag: '<?',
disabled: '=?',
visible: '=?',
cssClass: '=?',
zoom: '=?',
center: '=?',
mouseWheel: '=?',
markers: '=?',
polylines: '=?',
polygons: '=?',
markCenter: '=?',
model: '=?'
},
templateUrl: '/templates/framework/directives/google-map/template.html',
link: function (scope, element, attr) {
scope.vm = {
waiting: true,
markers: [],
polylines: [],
polygons: [],
canvas: element.find('.vw-google-map')[0]
};
scope.vm.init = function () {
if (angular.isUndefined(scope.disabled)) {
scope.disabled = false;
}
if (angular.isUndefined(scope.visible)) {
scope.visible = true;
}
if (angular.isUndefined(scope.cssClass)) {
scope.cssClass = '';
}
if (angular.isUndefined(scope.zoom)) {
scope.zoom = 16;
}
if (angular.isUndefined(scope.mouseWheel)) {
scope.mouseWheel = false;
}
if (angular.isUndefined(scope.markCenter)) {
scope.markCenter = false;
}
if (angular.isUndefined(scope.markers)) {
scope.markers = [];
}
if (angular.isUndefined(scope.center)) {
scope.center = {
lat: 51.5,
lng: -0.2
};
}
if (angular.isUndefined(scope.polylines)) {
scope.polylines = [];
}
if (angular.isUndefined(scope.polygons)) {
scope.polygons = [];
}
if (angular.isUndefined(scope.model)) {
scope.model = [];
}
if (scope.markCenter) {
scope.markers.push(scope.center);
}
scope.vm.bind();
googleMapService().then(function () {
scope.vm.waiting = false;
scope.vm.create();
});
};
scope.vm.createPolyline = function (data) {
var path = new google.maps.Polyline({
path: data.path,
geodesic: data.geodesic,
strokeColor: data.strokeColor || '#FF0000',
strokeOpacity: data.strokeOpacity || 1,
strokeWeight: data.strokeWeight || 2
});
path.setMap(scope.vm.instance);
return path;
};
scope.vm.createPolygon = function (data) {
var polygon = new google.maps.Polygon({
paths: data.path,
strokeColor: data.strokeColor || '#0000FF',
strokeOpacity: data.strokeOpacity || 1,
strokeWeight: data.strokeWeight || 2,
fillColor: data.fillColor || '#FF0000',
fillOpacity: data.fillOpacity || 0.35
});
polygon.setMap(scope.vm.instance);
return polygon;
};
scope.vm.createMarker = function (location) {
return new google.maps.Marker({
draggable: false,
position: location ? {
lat: location.lat,
lng: location.lng
} : undefined,
map: scope.vm.instance,
title: location ? location.title || ' ' : ' '
});
};
scope.vm.pushMarker = function (location, created) {
var marker;
if (created) {
marker = location;
} else {
marker = scope.vm.createMarker(location);
}
scope.vm.markers.push(marker);
};
scope.vm.pushPolyline = function (data, created) {
var item;
if (created) {
item = data;
} else {
item = scope.vm.createPolyline(data);
}
scope.vm.polylines.push(item);
};
scope.vm.pushPolygon = function (data, created) {
var item;
if (created) {
item = data;
} else {
item = scope.vm.createPolygon(data);
}
scope.vm.polygons.push(item);
};
scope.vm.clearMarkers = function () {
(scope.vm.markers || []).forEach(function (marker) {
marker.setMap(null);
});
(scope.vm.markers || []).length = 0;
};
scope.vm.clearPolylines = function () {
(scope.vm.polylines || []).forEach(function (polyline) {
polyline.setMap(null);
});
(scope.vm.polylines || []).length = 0;
};
scope.vm.clearPolygons = function () {
(scope.vm.polygons || []).forEach(function (polygon) {
polygon.setMap(null);
});
(scope.vm.polygons || []).length = 0;
};
scope.vm.create = function () {
var mapOptions = {
center: new google.maps.LatLng(scope.center.lat, scope.center.lng),
zoom: scope.zoom,
scrollwheel: scope.mouseWheel
};
scope.vm.instance = new google.maps.Map(scope.vm.canvas, mapOptions);
google.maps.event.addListener(scope.vm.instance, 'click', function (event) {
var marker = scope.vm.createMarker(null);
marker.setPosition(event.latLng);
scope.vm.pushMarker(marker, true);
});
};
scope.vm.bind = function () {
scope.$watch('markers', function (val) {
if (!scope.vm.instance)
return;
scope.vm.clearMarkers();
(val || []).forEach(function (item) {
scope.vm.pushMarker(item);
});
});
scope.$watch('polylines', function (val) {
if (!scope.vm.instance)
return;
scope.vm.clearPolylines();
(val || []).forEach(function (item) {
scope.vm.pushPolyline(item);
});
});
scope.$watch('polygons', function (val) {
if (!scope.vm.instance)
return;
scope.vm.clearPolygons();
(val || []).forEach(function (item) {
scope.vm.pushPolygon(item);
});
});
};
scope.vm.init();
}
};
}
]);
}(jQuery, angular, _, window, document));