Code Monkey home page Code Monkey logo

ol-hashed's Introduction

ol-hashed

A simple utility for synchronizing your OpenLayers map state with the URL hash.

Installation

npm install ol ol-hashed

The ol-hashed module is meant to be used together with (and depends on) the ol package.

Usage

The default export from the ol-hashed module is a function that you call with a map.

import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import XYZSource from 'ol/source/XYZ.js';
import sync from 'ol-hashed';

// create a map as you would normally
const map = new Map({
  target: 'map-container',
  layers: [
    new TileLayer({
      source: new XYZSource({
        url: 'http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg'
      })
    })
  ],
  view: new View({
    center: [0, 0],
    zoom: 2
  })
});

// synchronize the map view with the URL hash
sync(map);

You can pass a second options argument to the sync function to control synchronization behavior.

By default, transitions in the map view are animated when browser history changes. The animate option allows you to control this behavior.

// update the view without any animation on history changes
sync(map, {animate: false});

By default, animations last 250 ms. You can provide an object for the animate option to control duration and easing of the animation.

// slower transitions on history changes
sync(map, {animate: {duration: 500}});

Calling the sync function with a map sets up listeners so that the URL hash is updated when the map view changes and the map view is updated when the URL hash changes (for example, when the user navigates back through history). The function returns an unregister function that can be called to unregister all listeners.

// synchronize the map view with the URL hash
const unregister = sync(map);

// later, if you want the map to no longer by synchronized
unregister();

ol-hashed's People

Contributors

tschaub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

wanggotao

ol-hashed's Issues

Getting Error: TypeError: _proj2.default is undefined

I have the following code which works, except when I add the sync(map); then the following error show in the console and the hash is never updated. I'm using npm with:

TypeError: _proj2.default is undefined[Learn More] index.js:47:8
serialize
index.js:47:8
parcelRequire<[162]</Schema.prototype.serialize
schema.js:60
parcelRequire<[110]</Store.prototype._initializeProvider/<
store.js:163
parcelRequire<[162]</Schema.prototype.forEachKey
schema.js:42
parcelRequire<[110]</Store.prototype._initializeProvider
store.js:160
parcelRequire<[110]</Store.prototype.register
store.js:128
parcelRequire<[32]</exports.register
index.js:36
synchronize
index.js:95:15
anonymous
http://localhost:1234/ol4-app.4d5fd9f3.js line 62318 > Function:158:2
newRequire
http://localhost:1234/ol4-app.4d5fd9f3.js:48:7
hmrAccept
http://localhost:1234/ol4-app.4d5fd9f3.js:62349:3
parcelRequire<[306]</ws.onmessage/<
http://localhost:1234/ol4-app.4d5fd9f3.js:62235:11
forEach self-hosted:268:13 parcelRequire<[306]</ws.onmessage
http://localhost:1234/ol4-app.4d5fd9f3.js:62233:7

Code:

import 'ol/ol.css';
import 'ol-layerswitcher/src/ol-layerswitcher.css';

import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import LayerGroup from 'ol/layer/group';
import {fromLonLat, toLonLat} from 'ol/proj';
import {defaults as defaultControls} from 'ol/control.js';
import MousePosition from 'ol/control/MousePosition.js';
import {createStringXY} from 'ol/coordinate.js';
import OSM from 'ol/source/OSM';
import TileWMS from 'ol/source/TileWMS';

import LayerSwitcher from 'ol-layerswitcher';
import sync from 'ol-hashed';

// default center, zoom, rotation
let center = [47.1556584, 15.2162729];
let zoom = 7;
let rotation = 0;

const mousePositionControl = new MousePosition({
    coordinateFormat: createStringXY(6),
    projection: 'EPSG:4326',
    undefinedHTML: '&nbsp;'
});

const map = new Map({
    target: 'map',
    controls: defaultControls({
        attributionOptions: {
            collapsible: false
        }
    }).extend([mousePositionControl]),
    layers: [
        new LayerGroup({
            title: 'Base maps',
            layers: [
                new TileLayer({
                    title: 'OSM',
                    type: 'base',
                    visible: true,
                    source: new OSM()
                }),
                new TileLayer({
                    title: 'Yemen Raster',
                    type: 'base',
                    visible: false,
                    source: new TileWMS({
                        projection: 'EPSG:4326',
                        url: 'http://127.0.0.1:8088/cgi-bin/mapserv?map=/data/satmap/maps/yemen.map',
                        params: {
                            'LAYERS': 'government,raster'
                        }
                    })
                })
            ]
        }),
        new LayerGroup({
            title: 'Overlays',
            layers: [
                new TileLayer({
                    title: 'Yemen All',
                    type: 'overlay',
                    visible: true,
                    source: new TileWMS({
                        projection: 'EPSG:4326',
                        url: 'http://127.0.0.1:8088/cgi-bin/mapserv?map=/data/satmap/maps/yemen.map',
                        params: {
                            'LAYERS': 'all'
                        }
                    })
                }),
                new TileLayer({
                    title: 'Yemen Raster',
                    type: 'overlay',
                    visible: false,
                    source: new TileWMS({
                        projection: 'EPSG:4326',
                        url: 'http://127.0.0.1:8088/cgi-bin/mapserv?map=/data/satmap/maps/yemen.map',
                        params: {
                            'LAYERS': 'government,raster'
                        }
                    })
                }),
                new TileLayer({
                    title: 'Yemen Roads',
                    type: 'overlay',
                    visible: false,
                    source: new TileWMS({
                        projection: 'EPSG:4326',
                        url: 'http://127.0.0.1:8088/cgi-bin/mapserv?map=/data/satmap/maps/yemen.map',
                        params: {
                            'LAYERS': 'roads4,roads3,roads2,roads1'
                        }
                    })
                }),
                new TileLayer({
                    title: 'Yemen Villages',
                    type: 'overlay',
                    visible: false,
                    source: new TileWMS({
                        projection: 'EPSG:4326',
                        url: 'http://127.0.0.1:8088/cgi-bin/mapserv?map=/data/satmap/maps/yemen.map',
                        params: {
                            'LAYERS': 'village'
                        }
                    })
                })
            ]
        })
    ],
    view: new View({
        center: fromLonLat(center),
        zoom: zoom,
        rotation: rotation
    })
});

var layerSwitcher = new LayerSwitcher();
map.addControl(layerSwitcher);

sync(map);

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Once you have installed CI on this repository, you’ll need to re-trigger Greenkeeper’s initial Pull Request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organiszation’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.