File

marker/marker.ts

Extends

CoordinateHandler

Metadata

moduleId module.id.toString()
providers PopupService
selector marker-element
styleUrls marker.css
templateUrl marker.html

Inputs

iconUrl

Type: string

lat

Type: number

Default value: 52.6

lon

Type: number

mouseover

Type: string

Default value: undefined

onclick

Type: string

Default value: undefined

Constructor

constructor(mapService: MapService, groupService: GroupService, popupService: PopupService, http: Http, elementText: ElementRef, LeafletElement: LeafletElement, LeafletGroup: LeafletGroup)

Methods

ngOnInit
ngOnInit()
Returns: void
createMarkerlayer
createMarkerlayer(marker: any, map: any)
Returns: void
imageExists
imageExists(url: any, callback: any)
Returns: void
getImage
getImage()
Returns: any

Properties

marker
marker: any
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>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""