angular-heremaps
Version:
Angular directive for working with Nokia Here Maps
101 lines (77 loc) • 3.69 kB
JavaScript
module.exports = HereMapsUiFactory;
HereMapsUiFactory.$inject = [
'HereMapsAPIService',
'HereMapsMarkerService',
'HereMapsUtilsService',
'HereMapsCONSTS'
];
function HereMapsUiFactory(HereMapsAPIService, HereMapsMarkerService, HereMapsUtilsService, HereMapsCONSTS) {
function UI(platform, alignment) {
this.map = platform.map;
this.layers = platform.layers;
this.alignment = alignment;
this.ui = platform.ui = H.ui.UI.createDefault(this.map, this.layers);
this.setupControls();
}
UI.isValidAlignment = isValidAlignment;
var proto = UI.prototype;
proto.setupControls = setupControls;
proto.createUserControl = createUserControl;
proto.setControlsAlignment = setControlsAlignment;
return {
start: function(args) {
if (!(args.platform.map instanceof H.Map) && !(args.platform.layers))
return console.error('Missed ui module dependencies');
var ui = new UI(args.platform, args.alignment);
}
}
function setupControls() {
var NAMES = HereMapsCONSTS.CONTROLS.NAMES,
userControl = this.createUserControl();
this.ui.getControl(NAMES.SETTINGS).setIncidentsLayer(false);
this.ui.addControl(NAMES.USER, userControl);
this.setControlsAlignment(NAMES);
}
function createUserControl() {
var self = this,
userControl = new H.ui.Control(),
markup = '<svg class="H_icon" fill="#fff" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path class="middle_location_stroke" d="M8 12c-2.206 0-4-1.795-4-4 0-2.206 1.794-4 4-4s4 1.794 4 4c0 2.205-1.794 4-4 4M8 1.25a6.75 6.75 0 1 0 0 13.5 6.75 6.75 0 0 0 0-13.5"></path><path class="inner_location_stroke" d="M8 5a3 3 0 1 1 .001 6A3 3 0 0 1 8 5m0-1C5.794 4 4 5.794 4 8c0 2.205 1.794 4 4 4s4-1.795 4-4c0-2.206-1.794-4-4-4"></path><path class="outer_location_stroke" d="M8 1.25a6.75 6.75 0 1 1 0 13.5 6.75 6.75 0 0 1 0-13.5M8 0C3.59 0 0 3.59 0 8c0 4.411 3.59 8 8 8s8-3.589 8-8c0-4.41-3.59-8-8-8"></path></svg>';
var userControlButton = new H.ui.base.Button({
label: markup,
onStateChange: function(evt) {
if (userControlButton.getState() === H.ui.base.Button.State.DOWN)
return;
HereMapsAPIService.getPosition().then(function(response) {
var position = {
lng: response.coords.longitude,
lat: response.coords.latitude
};
self.map.setCenter(position);
HereMapsUtilsService.zoom(self.map, 17, .08);
if (self.userMarker) {
self.userMarker.setPosition(position);
return;
}
self.userMarker = HereMapsMarkerService.addUserMarker(self.map, {
pos: position
});
});
}
});
userControl.addChild(userControlButton);
return userControl;
}
function setControlsAlignment(NAMES) {
if (!UI.isValidAlignment(this.alignment))
return;
for (var id in NAMES) {
var control = this.ui.getControl(NAMES[id]);
if (!NAMES.hasOwnProperty(id) || !control)
continue;
control.setAlignment(this.alignment);
}
}
function isValidAlignment(alignment) {
return !!(HereMapsCONSTS.CONTROLS.POSITIONS.indexOf(alignment) + 1);
}
};