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

plain-js

v1.0.2

Published

Create your map application with same code, get rid of different Map library API ✨.

Downloads

23

Readme

English | 简体中文

example

Features

  1. Layers
    1. Marker
    2. Polyline
    3. Popup
  2. Map Controls
    1. Zoom
    2. FitView
    3. PanTo
  3. Evented
  4. Utils
    1. GetBound
    2. Locate
    3. Coordinate Translate

How to use plain?

Install

Install plain-js via npm, you also could load plain.min.js in html file.

$ npm install plain-js

Create Map

It is simple, use following code after install plain:

// create a plain Object
let plain = new Plain();

// Set the default coordinate system,
// if not, all the map will using the default coordinate system:
// Google and Gaode using GCJ02 in mainland of China, baidu map using BD09.
// we suggest 'GCJ02'.
plain.setCoordType('GCJ02');

// Tell plain which map you want use,
// eg: Google Map 'GMAP', GaodeMap 'AMAP', BaiduMap 'BMAP'
plain.use('GMAP');

// Create a Google map object
let map = plain.Map({
    container: "#map",          // or DivElement
    center: [39.908012, 116.399348],
    zoom: 15
});

By the way, you can create map in the callback function

let plain = new Plain().use('GMAP');
let key = "[your access key]";
plain.loadMap(key, () => {
    let map = window.map = plain.Map({
        container: document.getElementById("map"),
        center: [39.910, 116.404],
        zoom: 15
    });
}, err => {
    // TODO:
};

Layers

Add Marker

let marker = plain.Marker([39.910, 116.404]);
map.addLayer(marker);   // or <Array>Marker

Wanna create a special Marker ? Just set second param:

// Create icon
let icon = plain.Icon({
    url: 'https://unpkg.com/[email protected]/dist/images/marker-icon.png',
    size: [25, 40],
    anchor: [12.5, 40]
});

// Marker configure option
let markerOpt = {
    icon: icon,
    draggable: true
};
let marker2 = plain.Marker([39.910, 116.404], markerOpt);
map.addLayer(marker2);
// Try to remove marker from map,
// But we will not destroy this marker
map.removeLayer(marker);

Add Polyline

There is a path Object before create Polyline, array item should be an array like this: [lat: Number, lng: Number]

let path = [
    [39.910, 116.404],
    [39.71, 116.5],
    [39.909, 117],
    [39.710, 118]
];
let polyline = plain.Polyline(path, {
    color: "#f00",
    weight: 2,
    opacity: 0.8
});
map.addLayer(polyline);

Custom Layer & Popup

let layer = plain.Layer()
    .setContent('text or HTMLElement')
    .setLatLng([31, 116])
    .mount(map) // add to map
    .show()     // set style.display to 'none'
    .hide()     // set style.display to 'block'
    .unmount(); // remove from map
let popup = plain.Popup({closeBtn: false})  // goto popupOptions
    .setContent(document.createElement('button'))
    .setLatLng([31, 116])
    .mount(map)
    .show()
    .hide()
    .unmount();

popupOptions:

{
    closeBtn: false,    // use close btn, default: false
    offset: [-40, 0],   // CSS margin attribute
    zIndex: 999,        // CSS z-index attribute
}

Map Controls

Zoom control

You can set zoom paramter just as:

let map = plain.Map({
    container: "#map",
    center: [39.908012, 116.399348],
    zoom: 15
});

Or use methods:

| method | description | |---|---| | setZoom(zoom: number) | Set zoom level, it's dependent on Map instance. Most of theme are at 1-15. | | getZoom(): number | Get zoom level. | | zoomIn() | Set zoom level++. | | zoomOut() | Set zoom level--. |

FitView

| method | description | |---|---| | fitView(latlngs: LatLng[], opt?: ViewportOption) | Set map viewport. |

interface ViewportOption {
    margins: number[];
}
interface LatLng extends Array<number> {
    [index: number]: number;
}

PanTo

| method | description | |---|---| | panTo(latlng: LatLng) | Change the center point of the map to a given point. |

Evented

So far, we have been able to create a map which shows the basic information, then we are going to addEventListenr.Plain method provides a tool for formatting the incoming event object, the value returned format is as follows

class Event {
    e: any;             // original event Object
    p: F.LatLng;        // coordinate [lat: number, lng: number]
    target: F.Layer;    // could be Plain's Marker or Map
    type: string;       // event name
}

p should be a coordinate which use same coordinate system with plain.setCoordType('GCJ02');.

let listener = map.on('rightclick', function (e) {
    console.log(plain.Util.formatEvent.call(this, e));
    // fit map viewport
    map.fitView(path);
});

Cancel eventListener:

map.off(listener);

Utils

Get bound

| method | description | |---|---| | getBound(latlngs: LatLng[]): LatLng[]| Return a rectangle that cover all points. |

Locate

| method | description | |---|---| | locate(success?: Function, error?: Function): void | Map location. |

Coordinate Translate

| method | description | |---|---| | b2g(latlngs: LatLng[]): LatLng[] | BD09 to GCJ02. | | w2g(latlngs: LatLng[]): LatLng[] | WGS84 to BD09. | | g2w(latlngs: LatLng[]): LatLng[] | GCJ02 to WGS84. | | w2b(latlngs: LatLng[]): LatLng[] | WGS84 to BD09. | | b2w(latlngs: LatLng[]): LatLng[] | BD09 to WGS84. | | g2b(latlngs: LatLng[]): LatLng[] | GCJ02 to BD09. |

License

plain is MIT licensed.