Description

The angular-ready wrapper for the native google.maps.Data class.

Extends

GoogleMapsDrawableOverlay

Implements

IGoogleMapsData

Index

overlays/modules/data/google-maps-data.ts

Properties
Methods

Constructor

constructor(map: IGoogleMap, api: GoogleMapsApiService, native: any)
Parameters:
Name Type Optional
map IGoogleMap No
api GoogleMapsApiService No
native any No

Properties

Public Readonly features
Default value: new FeatureTracker()

The tracker of geometry features currently added to the data layer. *

Public map
Type: IGoogleMap
The map to which this overlay should be added.
Public Readonly type
Type: OverlayType
The type of overlay this wrapper holds. Used by the `OverlayTracker` to distinguish between types.
Public custom
Type: any
Public Readonly native
Type: TNative
The instantiated native object to be wrapped.

Methods

Public addFeature
addFeature(feature: IGoogleMapsFeature)

Adds a feature to the data layer.

Parameters:
Name Type Optional Description
feature IGoogleMapsFeature No

The feature wrapper to add.

The wrapper object for the new feature.

Public addFeature
addFeature(options: google.maps.Data.FeatureOptions)

Creates a wrapper object for the feature and adds it to the data layer.

Parameters:
Name Type Optional
options google.maps.Data.FeatureOptions No

The wrapper object for the new feature.

Public createMarker
createMarker(position: Coord, options?: google.maps.Data.FeatureOptions)
Decorators :
@OutsideAngular()

Creates a marker geometry feature with the specified properties and adds it to the map.

Parameters:
Name Type Optional Description
position Coord No

The position at which the marker should be added.

options google.maps.Data.FeatureOptions Yes

(Optional) Any native options to assign to the marker.

The wrapper object for the new feature.

Public createPolygon
createPolygon(path: CoordPath, options?: google.maps.Data.FeatureOptions)
Decorators :
@OutsideAngular()

Creates a polygon geometry feature with the specified properties and adds it to the map.

Parameters:
Name Type Optional Description
path CoordPath No

The path describing the polygon coordinates.

options google.maps.Data.FeatureOptions Yes

(Optional) Any native options to assign to the polygon.

The wrapper object for the new feature.

Public createPolyline
createPolyline(path: Path, options?: google.maps.Data.FeatureOptions)
Decorators :
@OutsideAngular()

Creates a polyline geometry feature with the specified properties and adds it to the map.

Parameters:
Name Type Optional Description
path Path No

The path describing the polyline coordinates.

options google.maps.Data.FeatureOptions Yes

(Optional) Any native options to assign to the polyline.

The wrapper object for the new feature.

Public findFeature
findFeature(id: string | number)

Looks for a feature in the data layer.

Parameters:
Name Type Optional Description
id string | number No

The id of the feature to look for.

Returns: google.maps.Data.Feature

The feature associated with the specified id or null when not found.

Public getBounds
getBounds()
Returns: google.maps.LatLngBounds
Public loadGeoJson
loadGeoJson(url: string, options?: google.maps.Data.GeoJsonOptions)
Decorators :
@OutsideAngular()

Downloads GeoJson data from the specified url, interprets it and creates map features for it. Will automatically take care of the callback required by Google Maps Api internally.

Parameters:
Name Type Optional Description
url string No

The url to the GeoJson data to download.

options google.maps.Data.GeoJsonOptions Yes

(Optional) Configures the process of reading the GeoJson.

Returns: Promise<google.maps.Data.Feature[]>

A promise for the features representing the geometries added from the GeoJson.

Public removeFeature
removeFeature(feature: google.maps.Data.Feature)

Removes a feature from the data layer.

Parameters:
Name Type Optional Description
feature google.maps.Data.Feature No

The native feature to remove.

The removed feature wrapper or null if not found.

Public removeFeature
removeFeature(feature: IGoogleMapsFeature)

Removes a feature from the data layer.

Parameters:
Name Type Optional Description
feature IGoogleMapsFeature No

The feature to remove.

The removed feature wrapper or null if not found.

Public removeFeature
removeFeature(featureId: string | number)

Removes a feature from the data layer.

Parameters:
Name Type Optional Description
featureId string | number No

The id of the feature to remove.

The removed feature wrapper or null if not found.

Public toGeoJson
toGeoJson()

Creates the GeoJson representation of the data and provides it as an object when the promise resolves. Will automatically take care of the callback required by Google Maps Api internally.

Returns: Promise<any>

A promise for the GeoJson object.

Public attach
attach(map: IGoogleMap)

Assigns the overlay to the specified map. If possible, prefer using the appropriate GoogleMap.createXXX() method instead. If not possible, it is the responsability of the caller to add the overlay to the OverlayTracker in the GoogleMap.overlays object. Otherwise, inconsistencies and unexpected behaviours might occur.

Parameters:
Name Type Optional Description
map IGoogleMap No

The map to dispaly the overlay on.

Returns: void
Public detach
detach()

Removes the overlay from the map it is attached to. If possible, prefer using the GoogleMap.removeOverlay() method instead. If not possible, it is the responsability of the caller to remove the overlay from the OverlayTracker in the GoogleMap.overlays object. Otherwise, inconsistencies and unexpected behaviours might occur.

Returns: void
Public clearListeners
clearListeners()

Unregisters all handlers of any previously registered native event.

Returns: void
Public listenTo
listenTo(eventName: string, handleEvent: (args: any[]) => void)

Registers a handler to a specific event of the native object and takes care of executing the handler inside angular's zone.

Parameters:
Name Type Optional Description
eventName string No

The name of the native event to register the handler for.

handleEvent function No

The function to execute when the event is triggered by the native object.

Returns: void

An function for unregistering the handler from the event.

Public stopListeningTo
stopListeningTo(eventName: string)

Unregisters all handlers previously registered to handle a specific event.

Parameters:
Name Type Optional Description
eventName string No

The name of the native event for which to unregister all handlers.

Returns: void
Public setCustom
setCustom(custom: any)
Parameters:
Name Type Optional
custom any No
Returns: void
import { GoogleMapsApiService, NativeObjectWrapper, IGoogleMap, OutsideAngular, Coord, CoordPath, Delegation, Path } from '@bespunky/angular-google-maps/core';
import { GoogleMapsDrawableOverlay             } from '../../abstraction/base/google-maps-drawable-overlay';
import { OverlayType                           } from '../../abstraction/base/overlay-type.enum';
import { isGoogleMapsFeatureOptions            } from '../../abstraction/type-guards/feature-options-type-guard';
import { IGoogleMapsData, WrappedDataFunctions } from './i-google-maps-data';
import { IGoogleMapsFeature                    } from './feature/i-google-maps-feature';
import { GoogleMapsFeature                     } from './feature/google-maps-feature';
import { FeatureTracker                        } from './services/feature-tracker';

/** Extends intellisense for `GoogleMapsData` with native data layer functions. */
export interface GoogleMapsData extends WrappedDataFunctions { }

/**
 * The angular-ready wrapper for the native `google.maps.Data` class.
 *
 * @export
 * @class GoogleMapsData
 * @extends {GoogleMapsDrawableOverlay<google.maps.Data>}
 * @implements {IGoogleMapsData}
 */
// @dynamic
@NativeObjectWrapper<GoogleMapsData>({
    getMap: Delegation.Exclude,
    setMap: Delegation.Exclude
})
export class GoogleMapsData extends GoogleMapsDrawableOverlay<google.maps.Data> implements IGoogleMapsData
{
    /**
     * The tracker of geometry features currently added to the data layer.     *
     */
    public readonly features = new FeatureTracker();

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    constructor(map: IGoogleMap, api: GoogleMapsApiService, native: any /* See super for docs on `any` */)
    {
        super(OverlayType.Data, map, api, native);
    }

    public getBounds(): google.maps.LatLngBounds
    {
        return this.api.geometry.defineBounds(...this.features.list.map(feature => feature.getBounds()));
    }

    /**
     * Creates a marker geometry feature with the specified properties and adds it to the map.
     *
     * @param {Coord} position The position at which the marker should be added.
     * @param {google.maps.Data.FeatureOptions} [options] (Optional) Any native options to assign to the marker.
     * @returns {IGoogleMapsFeature} The wrapper object for the new feature.
     */
    @OutsideAngular
    public createMarker(position: Coord, options?: google.maps.Data.FeatureOptions): IGoogleMapsFeature
    {
        options = this.buildOptions(this.api.geometry.createDataPoint(position), options);

        return this.addFeature(options);
    }

    /**
     * Creates a polygon geometry feature with the specified properties and adds it to the map.
     *
     * @param {CoordPath} path The path describing the polygon coordinates.
     * @param {google.maps.Data.FeatureOptions} [options] (Optional) Any native options to assign to the polygon.
     * @returns {IGoogleMapsFeature} The wrapper object for the new feature.
     */
    @OutsideAngular
    public createPolygon(path: CoordPath, options?: google.maps.Data.FeatureOptions): IGoogleMapsFeature
    {
        options = this.buildOptions(this.api.geometry.createDataPolygon(path), options);

        return this.addFeature(options);
    }

    /**
     * Creates a polyline geometry feature with the specified properties and adds it to the map.
     *
     * @param {Path} path The path describing the polyline coordinates.
     * @param {google.maps.Data.FeatureOptions} [options] (Optional) Any native options to assign to the polyline.
     * @returns {IGoogleMapsFeature} The wrapper object for the new feature.
     */
    @OutsideAngular
    public createPolyline(path: Path, options?: google.maps.Data.FeatureOptions): IGoogleMapsFeature
    {
        options = this.buildOptions(this.api.geometry.createDataPolyline(path), options);

        return this.addFeature(options);
    }

    private buildOptions(geometry: google.maps.Data.Geometry, baseOptions?: google.maps.Data.FeatureOptions): google.maps.Data.FeatureOptions
    {
        return Object.assign({}, baseOptions, { geometry });
    }

    /**
     * Adds a feature to the data layer.
     *
     * @param {IGoogleMapsFeature} feature The feature wrapper to add.
     * @returns {IGoogleMapsFeature} The wrapper object for the new feature.
     */
    public addFeature(feature: IGoogleMapsFeature): IGoogleMapsFeature;
    /**
     * Creates a wrapper object for the feature and adds it to the data layer.
     *
     * @param {(google.maps.Data.FeatureOptions | IGoogleMapsFeature)} feature The native feature or feature wrapper to add.
     * @returns {IGoogleMapsFeature} The wrapper object for the new feature.
     */
    public addFeature(options: google.maps.Data.FeatureOptions): IGoogleMapsFeature;
    /**
     * @ignore
     */
    @OutsideAngular
    public addFeature(feature: google.maps.Data.FeatureOptions | IGoogleMapsFeature): IGoogleMapsFeature
    {
        if (isGoogleMapsFeatureOptions(feature))
            feature = new GoogleMapsFeature(this, this.api, new google.maps.Data.Feature(feature)); 

        this.native.add(feature.native);
        this.features.add(feature);

        return feature;
    }

    /**
     * Removes a feature from the data layer.
     *
     * @param {google.maps.Data.Feature} feature The native feature to remove.
     * @returns {IGoogleMapsFeature} The removed feature wrapper or `null` if not found.
     */
    public removeFeature(feature: google.maps.Data.Feature): IGoogleMapsFeature;
    /**
     * Removes a feature from the data layer.
     *
     * @param {IGoogleMapsFeature} feature The feature to remove.
     * @returns {IGoogleMapsFeature} The removed feature wrapper or `null` if not found.
     */
    public removeFeature(feature: IGoogleMapsFeature): IGoogleMapsFeature;
    /**
     * Removes a feature from the data layer.
     *
     * @param {(number | string)} featureId The id of the feature to remove.
     * @returns {IGoogleMapsFeature} The removed feature wrapper or `null` if not found.
     */
    public removeFeature(featureId: string | number): IGoogleMapsFeature;
    /**
     * @ignore
     */
    @OutsideAngular
    public removeFeature(featureOrId: string | number | google.maps.Data.Feature | IGoogleMapsFeature): IGoogleMapsFeature
    {
        const removed = this.features.remove(featureOrId);

        if (removed)
            this.native.remove(removed.native);

        return removed;
    }

    /**
     * Looks for a feature in the data layer.
     *
     * @param {(string | number)} id The id of the feature to look for.
     * @returns {google.maps.Data.Feature} The feature associated with the specified id or `null` when not found.
     */
    public findFeature(id: string | number): google.maps.Data.Feature { return this.native.getFeatureById(id); }

    /**
     * Creates the GeoJson representation of the data and provides it as an object when the promise resolves.
     * Will automatically take care of the callback required by Google Maps Api internally.
     *
     * @returns {Promise<any>} A promise for the GeoJson object.
     */
    public toGeoJson(): Promise<any>
    {
        return new Promise(resolve => this.native.toGeoJson(resolve));
    }

    /**
     * Downloads GeoJson data from the specified url, interprets it and creates map features for it.
     * Will automatically take care of the callback required by Google Maps Api internally.

     * @param {string} url The url to the GeoJson data to download.
     * @param {google.maps.Data.GeoJsonOptions} [options] (Optional) Configures the process of reading the GeoJson.
     * @returns {google.maps.Data.Feature[]} A promise for the features representing the geometries added from the GeoJson.
     */
    @OutsideAngular
    public loadGeoJson(url: string, options?: google.maps.Data.GeoJsonOptions): Promise<google.maps.Data.Feature[]>
    {
        return new Promise(resolve => this.native.loadGeoJson(url, options, resolve));
    }
}

results matching ""

    No results matching ""