npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ninjagoat-map

v3.8.3

Published

Ninjagoat bindings of [Leaflet](http://leafletjs.com) with built in support for observing GeoJSON data, draw/edit shapes and geocoding.

Downloads

155

Readme

Ninjagoat-map

Ninjagoat bindings of Leaflet with built in support for observing GeoJSON data, draw/edit shapes and geocoding.

Installation

$ npm install ninjagoat-map

Register the module with ninjagoat

//bootstrapper.ts
import {MapModule} from "ninjagoat-map";

application.register(new MapModule())

Add leaflet css and images

/*boostrapper.scss*/

@import "../../node_modules/leaflet/dist/leaflet.css";
@import "../../node_modules/leaflet-draw/dist/leaflet.draw.css";
@import "../../node_modules/leaflet-geosearch/assets/css/leaflet.css";
//smildfile.js

const {CopyDirectory} = require("smild-extra");

module.exports = {
    "projectType": "frontend",
    "postBuild": (buildHelper) => Promise.all([
        CopyDirectory("node_modules/leaflet/dist/images/**/*", `dist/${buildHelper.getCurrentTarget()}/css/images`),
        CopyDirectory("node_modules/leaflet-draw/dist/images/**/*", `dist/${buildHelper.getCurrentTarget()}/css/images`)
    ])
};

Set the dimensions of your map

.leaflet-container {
	height: 400px;
	width: 80%;
	margin: 0 auto;
}

And it's configured!

Usage

Initialize a tiled map

Ninjagoat map doesn't need a specific viewmodel to work with, so you can just register a simple ObservableViewModel bound with a map view. The map can be initialized with a custom center and zoom level and accepts layers (and controls) as children.

import {View} from "ninjagoat";
import * as React from "react";
import {Map, TileLayer} from "ninjagoat-map";

export default class MapView extends View<any> {

    render() {
        return <Map center={[30, 30]} zoom={10} onMapReady={() => {
            //Do something when the map is initialized
        }}>
                    <TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"}/>
                </Map>
    }
}

In this example the tiles are rendered by OpenStreetMap but can configured to work with every map provider (e.g. Mapbox). Notice also that we are using the onMapReady callback: this is done to ensure that all the operations on the map will be done after it's loaded.

Display data

Data is displayed using a GeoJSON layer attached to an observable of type FeatureCollection. Register an observable factory in your viewmodel and pass it to the geojson layer. Every time the map is moved the observable will be recreated with the current bounds (in this case a marker is pinned in the center of the bounds).

//MapViewModel.ts

import {ObservableViewModel, ViewModel} from "ninjagoat";
import {MapContext} from "ninjagoat-map";
import {Observable} from "rx";

@ViewModel("MapIndex")
class MapViewModel extends ObservableViewModel<void> {

    sources = {
        marker: (context: MapContext) => Observable.just(<any>{
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "geometry": {
                        "type": "Point",
                        "coordinates": [context.bounds.getCenter().lng, context.bounds.getCenter().lat]
                    },
                    "properties": {}
                }
            ]
        })
    };

    protected onData(data: any) {

    }
}

export default MapViewModel

//MapView.ts
import {GeoJSONLayer} from "ninjagoat-map";

<Map center={[30, 30]} zoom={10}>
    <TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"}/>
    <GeoJSONLayer observable={this.viewModel.sources.marker}/>
</Map>

To customize the displayed data you can pass some Leaflet callbacks to the layer: here's the official guide.

Draw and edit shapes

Drawing shapes on the map follows the same principles of using the GeoJSONLayer. To restore some previously drawn shapes on map just use a drawing layer. After you have defined a observable factory of shapes in your viewmodel you can add them to the view.

//MapView.ts
import {DrawingLayer} from "ninjagoat-map";

<Map center={[30, 30]} zoom={10}>
    <TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"}/>
    <DrawingLayer observable={this.viewModel.sources.shapes} />
</Map>

To add drawing and editing capabilities some default draw controls are provided (but you can pass your own).

//MapView.ts
import {DrawingLayer, DrawControl} from "ninjagoat-map";

<Map center={[30, 30]} zoom={10}>
    <TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"}/>
    <DrawingLayer observable={this.viewModel.sources.shapes}>
        <DrawControl position="topright" />
    </DrawingLayer>
</Map>

To get notified of the shapes changes when drawing or editing you can add an onChange event handler on the DrawingLayer. The callback will be filled with a FeatureCollection of all the shapes on the drawing layer. Here's an example of a simple drawing and editing layer.

import {GeoJSONCollection} from "ninjagoat-map";

class MapViewModel extends ObservableViewModel<void> {

    savedShapes: GeoJSONCollection;

    sources = {
        draw: context => Observable.just(this.savedShapes)
    };

    protected onData(data: any) {

    }
    
    saveShapes(shapes: GeoJSONCollection) {
        this.savedShapes = shapes;
    }
}

//MapView.ts
import {DrawingLayer, DrawControl} from "ninjagoat-map";

<Map center={[30, 30]} zoom={10}>
    <TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"}/>
    <DrawingLayer observable={this.viewModel.sources.draw} onChange={this.viewModel.saveShapes.bind(this.viewModel)}>
        <DrawControl position="topright" />
    </DrawingLayer>
</Map>

The DrawControl can be customized using the options provided by leaflet-draw.

Geocoding

A geocoding control for places search is also available and it's backed by a geocoding provider. The default geocoding provider is Google Places and requires a google maps api key.

//Module
import {IApiKeyConfig} from "ninjagoat-map";

container.bind<IApiKeyConfig>("IMapApiKeyConfig").toConstantValue({
    key: "your_api_key"
});

//View
import {GeocodingControl} from "ninjagoat-map";

<Map center={[30, 30]} zoom={10}>
    <TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"}/>
    <GeocodingControl position="topleft" />
</Map>

See the optional properties to customize the control.

Map services

Some map services are defined to work with the map: the most important is the MapBoundaries. You can inject it in your viewmodels to manipulate the map view. Here is how it's defined.

interface IMapBoundaries {
    getCenter(): LatLng;
    getBounds(): LatLngBounds;
    getZoom(): number;
    setCenter(center: LatLng, zoom?: number);
    boundsChanges(): Observable<void>;
}

License

Copyright 2016 Tierra SpA

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.