marker/marker.ts
moduleId | module.id.toString() |
providers |
PopupService
|
selector | marker-element |
styleUrls | marker.css |
templateUrl | marker.html |
iconUrl
|
Type: |
Defined in marker/marker.ts:28
|
lat
|
Type:
Default value: |
Defined in marker/marker.ts:24
|
lon
|
Type: |
Defined in marker/marker.ts:25
|
mouseover
|
Type:
Default value: |
Defined in marker/marker.ts:26
|
onclick
|
Type:
Default value: |
Defined in marker/marker.ts:27
|
constructor(mapService: MapService, groupService: GroupService, popupService: PopupService, http: Http, elementText: ElementRef, LeafletElement: LeafletElement, LeafletGroup: LeafletGroup)
|
Defined in marker/marker.ts:29
|
ngOnInit |
ngOnInit()
|
Defined in marker/marker.ts:42
|
Returns:
void
|
createMarkerlayer |
createMarkerlayer(marker: any, map: any)
|
Defined in marker/marker.ts:87
|
Returns:
void
|
imageExists |
imageExists(url: any, callback: any)
|
Defined in marker/marker.ts:105
|
Returns:
void
|
getImage |
getImage()
|
Defined in marker/marker.ts:112
|
Returns:
any
|
marker |
marker: |
Defined in marker/marker.ts:29
|
import { Component, Input, Injector, Optional, ElementRef } from '@angular/core';
import { Http, Response, Headers, RequestOptions, Request, RequestMethod, ResponseContentType } from '@angular/http';
import { MapService } from '../services/map.service';
import { GroupService } from '../services/group.service';
import { PopupService } from '../services/popup.service';
import { LeafletElement } from '../map/map';
import { LeafletGroup } from '../group/group';
import { CoordinateHandler } from '../helpers/coodinateHandler';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import * as L from 'leaflet';
@Component({
moduleId: module.id.toString(),
selector: 'marker-element',
templateUrl: 'marker.html',
styleUrls: ['marker.css'],
providers: [PopupService]
})
export class MarkerElement extends CoordinateHandler {
@Input() lat: number = 52.6;
@Input() lon: number = -1.1;
@Input() mouseover: string = undefined;
@Input() onclick: string = undefined;
@Input() iconUrl: string = "";
marker: any = null;
constructor(
private mapService: MapService,
private groupService: GroupService,
private popupService: PopupService,
private http: Http,
private elementText: ElementRef,
@Optional() private LeafletElement?: LeafletElement,
@Optional() private LeafletGroup?: LeafletGroup) {
super();
}
ngOnInit() {
super.assignCartesianPointToLeafletsLatLngSchema();
var model = this;
if (this.LeafletElement || this.LeafletGroup) {
let map = this.mapService.getMap();
super.transformPointCoordinates(this.LeafletElement.crs);
if (this.iconUrl === "") {
this.marker = L.marker([this.lat, this.lon]);
this.createMarkerlayer(this.marker, map);
} else {
this.imageExists(this.iconUrl, function (exists) {
model.getImage().subscribe(
image => {
var img = document.createElement("img");
window.URL.createObjectURL(image.blob());
var reader = new FileReader();
reader.onload = function () {
img.src = reader.result;
var myIcon = L.icon({
iconUrl: model.iconUrl,
iconSize: [img.width, img.height],
iconAnchor: [img.width / 2, img.height - 1],
popupAnchor: [0, -img.height]
});
var obj = { icon: myIcon, options: null };
model.marker = L.marker([model.lat, model.lon], obj);
model.createMarkerlayer(model.marker, map);
}
reader.readAsDataURL(image.blob());
},
err => {
console.log(err);
});
});
}
} else {
console.warn("This marker-element will not be rendered \n the expected parent node of marker-element should be either leaf-element or leaflet-group");
}
}
createMarkerlayer(marker, map) {
var textInput = undefined;
if (this.elementText.nativeElement.childNodes.length > 0) {
var textNode = this.elementText.nativeElement.childNodes[0];
textInput = textNode.nodeValue;
}
//add popup methods on element
this.popupService.enablePopup(this.mouseover, this.onclick, this.marker, textInput);
//only if the parent is map should the marker-element should be directly added to the map
if (this.LeafletGroup) {
this.groupService.addOLayersToGroup(marker, map, this.mapService, this.LeafletGroup);
} else {
marker.addTo(map);
}
}
imageExists(url, callback) {
var img = new Image();
img.onload = function () { callback(true); };
img.onerror = function () { callback(false); };
img.src = url;
}
getImage(): any {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({
responseType: ResponseContentType.Blob,
headers: headers
});
return this.http.get(this.iconUrl, options)
.map((res: Response) => res)
.catch((error: any) => Observable.throw('Server error'));
}
}
<ng-content></ng-content>