Git Product home page Git Product logo

mapbox-gl-sync-move's Introduction

@mapbox/mapbox-gl-sync-move CircleCI

Sync movement between two or more Mapbox GL JS maps.

Install

npm install @mapbox/mapbox-gl-sync-move

Usage

This module exports a function that receives as arguments two or more Mapbox GL JS maps whose movements you'd like to sync.

var mapboxgl = require('mapbox-gl');
var syncMaps = require('mapbox-gl-sync-move');

var mapA = new mapboxgl.Map(..);
var mapB = new mapboxgl.Map(..);

syncMaps(mapA, mapB);

Developing

There are unit tests with mocked maps, and there's a page for manual testing.

Run the unit tests with npm test.

To manually test, ensure you have a MapboxAccessToken environment variable set. Then start the server with npm run start.

mapbox-gl-sync-move's People

Contributors

danielfdsilva avatar gampleman avatar rulyotano avatar tmcw avatar tristen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mapbox-gl-sync-move's Issues

Refactored for TypeScript

I ended up having to fork this because of its super outdated dependency on a version of mapbox-gl and re-wrote it in TypeScript.

If this is still going to be maintained someday, maybe this is a helpful starting point for a new release?

import type mapboxgl from "mapbox-gl";

/**
 * Sync movements of two maps.
 * 
 * All interactions that result in movement end up firing
 * a "move" event. The trick here, though, is to
 * ensure that movements don't cycle from one map
 * to the other and back again, because such a cycle
 * - could cause an infinite loop
 * - prematurely halts prolonged movements like
 *   double-click zooming, box-zooming, and flying
 */
export default function syncMaps(...maps: mapboxgl.Map[]) {
	// Create all the movement functions, because if they're created every time
	// they wouldn't be the same and couldn't be removed.
	let fns: Parameters<mapboxgl.Map["on"]>[1][] = [];
	maps.forEach((map, index) => {
		// When one map moves, we turn off the movement listeners
		// on all the maps, move it, then turn the listeners on again
		fns[index] = () => {
			off();

			const center = map.getCenter();
			const zoom = map.getZoom();
			const bearing = map.getBearing();
			const pitch = map.getPitch();

			const clones = maps.filter((o, i) => i !== index);
			clones.forEach((clone) => {
				clone.jumpTo({
					center: center,
					zoom: zoom,
					bearing: bearing,
					pitch: pitch,
				});
			});

			on();
		};
	});

	const on = () => {
		maps.forEach((map, index) => {
			map.on("move", fns[index]);
		});
	};

	const off = () => {
		maps.forEach((map, index) => {
			map.off("move", fns[index]);
		});
	};

	on();

	return () => {
		off();
		fns = [];
		maps = [];
	};
}

I did some general cleanup/simplification as well as moved to arrow functions and using spread syntax for the maps array.

use of mapbox-gl-sync-move without require

How to use this module without the require method ?

By the past, I have successfully browserify modules with bower (https://bower.io/) but this one resist.
Any idea how to synchronize mapbox gl maps with a JavaScript library without node.js and require method ?

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.