File

packages/core/src/lib/services/managers/rectangle-manager.ts

Index

Properties
Methods

Constructor

constructor(_apiWrapper: GoogleMapsAPIWrapper, _zone: NgZone)
Parameters :
Name Type Optional
_apiWrapper GoogleMapsAPIWrapper No
_zone NgZone No

Methods

addRectangle
addRectangle(rectangle: AgmRectangle)
Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : void
createEventObservable
createEventObservable(eventName: string, rectangle: AgmRectangle)
Type parameters :
  • T
Parameters :
Name Type Optional
eventName string No
rectangle AgmRectangle No
Returns : Observable<T>
getBounds
getBounds(rectangle: AgmRectangle)
Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : Promise<google.maps.LatLngBounds>
removeRectangle
removeRectangle(rectangle: AgmRectangle)

Removes the given rectangle from the map.

Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : Promise<void>
setBounds
setBounds(rectangle: AgmRectangle)
Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : Promise<void>
setDraggable
setDraggable(rectangle: AgmRectangle)
Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : Promise<void>
setEditable
setEditable(rectangle: AgmRectangle)
Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : Promise<void>
setOptions
setOptions(rectangle: AgmRectangle, options: google.maps.RectangleOptions)
Parameters :
Name Type Optional
rectangle AgmRectangle No
options google.maps.RectangleOptions No
Returns : Promise<void>
setVisible
setVisible(rectangle: AgmRectangle)
Parameters :
Name Type Optional
rectangle AgmRectangle No
Returns : Promise<void>

Properties

Private _rectangles
Type : Map<AgmRectangle | Promise<google.maps.Rectangle>>
Default value : new Map<AgmRectangle, Promise<google.maps.Rectangle>>()
import { Injectable, NgZone } from '@angular/core';

import { Observable, Subscriber } from 'rxjs';

import { AgmRectangle } from '../../directives/rectangle';
import { GoogleMapsAPIWrapper } from '../google-maps-api-wrapper';

@Injectable()
export class RectangleManager {
  private _rectangles: Map<AgmRectangle, Promise<google.maps.Rectangle>> =
      new Map<AgmRectangle, Promise<google.maps.Rectangle>>();

  constructor(private _apiWrapper: GoogleMapsAPIWrapper, private _zone: NgZone) {}

  addRectangle(rectangle: AgmRectangle) {
    this._rectangles.set(rectangle, this._apiWrapper.getNativeMap().then( () =>
      this._apiWrapper.createRectangle({
        bounds: {
          north: rectangle.north,
          east: rectangle.east,
          south: rectangle.south,
          west: rectangle.west,
        },
        clickable: rectangle.clickable,
        draggable: rectangle.draggable,
        editable: rectangle.editable,
        fillColor: rectangle.fillColor,
        fillOpacity: rectangle.fillOpacity,
        strokeColor: rectangle.strokeColor,
        strokeOpacity: rectangle.strokeOpacity,
        strokePosition: google.maps.StrokePosition[rectangle.strokePosition],
        strokeWeight: rectangle.strokeWeight,
        visible: rectangle.visible,
        zIndex: rectangle.zIndex,
      }))
    );
  }

  /**
   * Removes the given rectangle from the map.
   */
  removeRectangle(rectangle: AgmRectangle): Promise<void> {
    return this._rectangles.get(rectangle).then((r) => {
      r.setMap(null);
      this._rectangles.delete(rectangle);
    });
  }

  setOptions(rectangle: AgmRectangle, options: google.maps.RectangleOptions): Promise<void> {
    return this._rectangles.get(rectangle).then((r) => {
      const actualStrokePosition = options.strokePosition as any as keyof typeof google.maps.StrokePosition;
      options.strokePosition = google.maps.StrokePosition[actualStrokePosition];
      r.setOptions(options);
    });
  }

  getBounds(rectangle: AgmRectangle): Promise<google.maps.LatLngBounds> {
    return this._rectangles.get(rectangle).then((r) => r.getBounds());
  }

  setBounds(rectangle: AgmRectangle): Promise<void> {
    return this._rectangles.get(rectangle).then((r) => {
      return r.setBounds({
        north: rectangle.north,
        east: rectangle.east,
        south: rectangle.south,
        west: rectangle.west,
      });
    });
  }

  setEditable(rectangle: AgmRectangle): Promise<void> {
    return this._rectangles.get(rectangle).then((r) => {
      return r.setEditable(rectangle.editable);
    });
  }

  setDraggable(rectangle: AgmRectangle): Promise<void> {
    return this._rectangles.get(rectangle).then((r) => {
      return r.setDraggable(rectangle.draggable);
    });
  }

  setVisible(rectangle: AgmRectangle): Promise<void> {
    return this._rectangles.get(rectangle).then((r) => {
      return r.setVisible(rectangle.visible);
    });
  }

  createEventObservable<T>(eventName: string, rectangle: AgmRectangle): Observable<T> {
    return new Observable((subsrciber: Subscriber<T>) => {
      let listener: google.maps.MapsEventListener = null;
      this._rectangles.get(rectangle).then((r) => {
        listener = r.addListener(eventName, (e: T) => this._zone.run(() => subsrciber.next(e)));
      });

      return () => {
        if (listener !== null) {
          listener.remove();
        }
      };
    });
  }
}

result-matching ""

    No results matching ""