English | 简体中文
Create your map application with same code, get rid of different Map library API ✨.
Features
- Layers
- Marker
- Polyline
- Popup
- Map Controls
- Zoom
- FitView
- PanTo
- Evented
- Utils
- GetBound
- Locate
- 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
:
let plain = new Plain();
plain.setCoordType('GCJ02');
plain.use('GMAP');
let map = plain.Map({
container: "#map",
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 => {
};
Layers
Add Marker
let marker = plain.Marker([39.910, 116.404]);
map.addLayer(marker);
Wanna create a special Marker ? Just set second param:
let icon = plain.Icon({
url: 'https://unpkg.com/leaflet@1.0.3/dist/images/marker-icon.png',
size: [25, 40],
anchor: [12.5, 40]
});
let markerOpt = {
icon: icon,
draggable: true
};
let marker2 = plain.Marker([39.910, 116.404], markerOpt);
map.addLayer(marker2);
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);
let layer = plain.Layer()
.setContent('text or HTMLElement')
.setLatLng([31, 116])
.mount(map)
.show()
.hide()
.unmount();
let popup = plain.Popup({closeBtn: false})
.setContent(document.createElement('button'))
.setLatLng([31, 116])
.mount(map)
.show()
.hide()
.unmount();
popupOptions
:
{
closeBtn: false,
offset: [-40, 0],
zIndex: 999,
}
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;
p: F.LatLng;
target: F.Layer;
type: string;
}
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));
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.