• This repository has been archived on 16/Nov/2018
  • Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Map integration for famo.us (Google Maps, Leaflet, Open Layers 3 & Mapbox GL)

famous-map

Map integration for Famo.us, supporting the following map-providers:

Screenshot

Famous-map makes it possible to add a map-component to the famo.us render-tree. Additionally, famous transitions can be used to pan the map and modifiers can be used to sync the position of renderables with a geographical position.

Demos

note: Hit refresh if the demo doesn't load properly. GitHub sometimes rejects loading famous-map.min.js the first time, but it always loads the next time :?

Getting started

Install using bower or npm:

bower install famous-map

npm install famous-map

Google Maps

Include google-maps in the html file:

<head>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
</head>

Create a google-maps view:

var MapView = require('famous-map/MapView');

var mapView = new MapView({
	type: MapView.MapType.GOOGLEMAPS,
    mapOptions: {
        zoom: 3,
        center: {lat: 51.4484855, lng: 5.451478},
        mapTypeId: google.maps.MapTypeId.TERRAIN
    }
});
this.add(mapView);

// Wait for the map to load and initialize
mapView.on('load', function () {

    // Move across the globe and zoom-in when done
    mapView.setPosition(
        {lat: 51.4484855, lng: 5.451478},
        { duration: 5000 },
        function () {
            mapView.getMap().setZoom(7);
        }
    );
}.bind(this));

IMPORTANT: Don't forget to read this instruction on google maps running on mobile devices.

Leaflet

Include leaflet in the html file:

<head>
	<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
</head>

Create a leaflet map:

var MapView = require('famous-map/MapView');

var mapView = new MapView({
	type: MapView.MapType.LEAFLET,
    mapOptions: {
        zoom: 3,
        center: {lat: 51.4484855, lng: 5.451478}
    }
});
this.add(mapView);

// Wait for the map to load and initialize
mapView.on('load', function () {

    // Add tile-layer (you can also get your own at mapbox.com)
    L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
		attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>'
	}).addTo(mapView.getMap());
}.bind(this));

OpenLayers 3

Include OpenLayers in the html file:

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.4.0/ol.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.4.0/ol.js"></script>
</head>

Create an open-layers map (uses canvas):

var MapView = require('famous-map/MapView');

var mapView = new MapView({
	type: MapView.MapType.OPENLAYERS3,
    mapOptions: {
        zoom: 3,
        center: {lat: 51.4484855, lng: 5.451478}
    }
});
this.add(mapView);

// Wait for the map to load and initialize
mapView.on('load', function () {

    // Add tile-layer (OSM is just one of many options)
	mapView.getMap().addLayer(new ol.layer.Tile({
		source: new ol.source.OSM()
	}));
}.bind(this));

Mapbox GL

Include Mapbox GL in the html file:

<head>
  <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.7.0/mapbox-gl.css' rel='stylesheet' />
  <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.7.0/mapbox-gl.js'></script>
</head>

Create an mapbox GL map:

var MapView = require('famous-map/MapView');

var mapView = new MapView({
    type: MapView.MapType.MAPBOXGL,
    mapOptions: {
        zoom: 3,
        center: {lat: 51.4484855, lng: 5.451478}
    }
});
this.add(mapView);

IMPORTANT: Touch events are not yet supported by mapbox-gl-js on mobile, so swiping, pinching, etc.. does not work on your phone or tablet: mapbox/mapbox-gl-js#949

Documentation

To access the underlying map object, use MapView.getMap(). The Map-object is only safely accessible after the 'load' event, because the DOM-object must first be created and the map needs to load.

mapView.on('load', function () {
    var map = mapView.getMap();
    ...
});
LatLng notation

Multiple LatLng formats are supported by the famous-map functions:

var pos = { lat: 57.876, lng: -13.242 }; // object literal
var pos = [57.876, -13.242]; // array: [lat, lng]
var pos = new google.maps.LatLng(57.876, -13.242); // object with .lat() and .lng() functions
Panning the map using transitions

To pan the map using famo.us transitions, use MapView.setPosition(). Transitions are chained, so you can create paths that the map will follow.

mapView.setPosition(
    {lat: 51.4484855, lng: 5.451478},
    {duration: 5000, curve: Easing.outBack},
    function () {
        mapView.getMap().setZoom(7)
    }
);
Linking a renderable to a geographical coordinate on the map

To place a renderable on the map like a marker, use MapModifier or MapStateModifier:

var MapModifier = require('famous-map/MapModifier');

var surface = new Surface({
    size: [50, 50],
    properties: {
        backgroundColor: 'white'
    }
});
var modifier = new Modifier({
    align: [0, 0],
    origin: [0.5, 0.5]
});
var mapModifier = new MapModifier({
    mapView: mapView,
    position: {lat: 51.4484855, lng: 5.451478}
});
this.add(mapModifier).add(modifier).add(surface);
Moving a renderable across the map

MapStateModifier relates to MapModifier in the same way StateModifier relates to Modifier. MapStateModifier makes it possible to change the position from one place to another, using a transitionable. Transitions are chained, so you can create paths that the renderable will follow:

MapStateModifier = require('famous-map/MapStateModifier');

var surface = new Surface({
    size: [50, 50],
    properties: {
        backgroundColor: 'white'
    }
});
var modifier = new Modifier({
    align: [0, 0],
    origin: [0.5, 0.5]
});
var mapStateModifier = new MapStateModifier({
    mapView: mapView,
    position: {lat: 51.4484855, lng: 5.451478}
});
this.add(mapStateModifier).add(modifier).add(surface);

// Animate the renderable across the map
mapStateModifier.setPosition(
    {lat: 52.4484855, lng: 6.451478},
    {method: 'map-speed', speed: 200} // 200 km/h
);
mapStateModifier.setPosition(
    {lat: 50.4484855, lng: 3.451478},
    {duration: 4000}
);
Enable auto-scaling when the map is zoomed in or out

To enable auto-scaling set zoomBase to the zoom-level you wish the renderables to be displayed in its true size. In this example where zoomBase is set to 5, this would mean that at zoom-level 4 its size will 1/4 of its original size:

var mapModifier = new MapModifier({
    mapView: mapView,
    position: {lat: 51.4484855, lng: 5.451478},
    zoomBase: 5
});

To use a different zooming strategy, use zoomScale. ZoomScale can be set to either a number or a getter function:

var mapModifier = new MapModifier({
    mapView: mapView,
    position: {lat: 51.4484855, lng: 5.451478},
    zoomBase: 5,
    zoomScale: 0.5
});

var mapModifier = new MapModifier({
    mapView: mapView,
    position: {lat: 51.4484855, lng: 5.451478},
    zoomBase: 5,
    zoomScale: function (baseZoom, currentZoom) {
        var zoom = currentZoom - baseZoom;
        if (zoom < 0) {
            return 1 / (2 * (Math.abs(zoom) + 1));
        } else {
            return 1 + (2 * zoom);
        }
    }
});
API reference
Class Description
MapView View class which encapsulates a maps object.
MapModifier Stateless modifier which positions a renderable based on a geographical position {LatLng}.
MapStateModifier Modifier which positions a renderable based on a geographical position {LatLng}, using transitions.
MapUtility General purpose utility functions.
MapTransition Transition for moving at a certain speed over the map (km/h).
MapPositionTransitionable Transitionable for geographical coordinates {LatLng}.

Known issues & performance

Google-Maps and Drag/Pinch on mobile devices

Famo.us prevents 'touchmove' events on mobile devices, which causes drag-to-move and pinch-to-zoom to break in Google Maps. To workaround this problem, disable 'app-mode' on mobile devices and instead install the custom MapView touch-handler before the main-context is created:

var Engine = require('famous/core/Engine');
var MapView = require('famous-map/MapView');
var isMobile = require('ismobilejs'); // https://github.com/kaimallea/isMobile

// On mobile, disable app-mode and install the custom MapView
// touch-handler so that Google Maps works.
if (isMobile.any) {
    Engine.setOptions({appMode: false});
    MapView.installSelectiveTouchMoveHandler();
}

var mainContext = Engine.createContext();
...

Resources:

Panning the map & smoothness

Panning the map using MapView.setPosition() and a transition works great, but is not as smooth in all scenarios and on all devices. Panning is smoothest for smaller tile-distances. To see map panning in action at different speeds, view the nyat-cat demo.

Google-Maps and Zoom-levels < 3

At the lower zoom-levels, renderables may not be positioned correctly using Google Maps. This happens when the entire world fits more than once on the surface. In this case, the bounding east and west longitude cannot be determined through the google-maps API, which are required for calculating the x position. To workaround this issue, set mapOptions.minZoom to 3.

Renderables lag and Leaflet

The leaflet-API returns the position and zoom-level after animations have occured. This causes a small lag in the position of renderables when panning the map. When zooming the map, the renderables are re-positioned after the zoom and smooth zooming is therefore not possible and disabled.

Contribute

Feel free to contribute to this project in any way. The easiest way to support this project is by giving it a star.

© 2014 / 2015 - Hein Rutjes

More Repositories

1

react-native-shared-element

Native shared element transition "primitives" for react-native 💫
TypeScript
2,190
star
2

react-native-bundle-visualizer

See what packages are increasing your react-native bundle size 📦
TypeScript
1,466
star
3

react-navigation-shared-element

React Navigation bindings for react-native-shared-element 💫
TypeScript
1,266
star
4

autolayout.js

Apple's Auto Layout and Visual Format Language for javascript (using cassowary constraints)
JavaScript
1,034
star
5

react-native-magic-move

Create magical move transitions between scenes in react-native 🐰🎩✨
JavaScript
973
star
6

firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
TypeScript
378
star
7

famous-flex

Animatable layouts, FlexScrollView & widgets for famo.us.
JavaScript
279
star
8

kiwi.js

Fast TypeScript implementation of the Cassowary constraint solving algorithm 🖖
JavaScript
250
star
9

react-navigation-magic-move

Bindings for using react-navigation with react-native-magic-move 🐰🎩✨
JavaScript
135
star
10

react-tag-cloud

Create beautiful tag/word clouds using React ☁️
TypeScript
118
star
11

visualformat-editor

Editor & previewer for Apple's Visual Format Language (built with autolayout.js)
JavaScript
90
star
12

node-web-bluetooth

Web Bluetooth API and interactive device picker for node.js
JavaScript
75
star
13

react-navigation-shared-element-demo

A simple app that demonstrates how to use react-navigation-shared-element in react-native
JavaScript
65
star
14

famous-autolayout

Apple's Auto Layout and Visual Format language for famo.us
JavaScript
55
star
15

famous-flex-chat

Chat-demo for famo.us using true-size chat bubbles, snap to bottom, sticky headers & pull-to-refresh
JavaScript
52
star
16

pnglib-es6

Create png images in pure javascript (modern & fast ES6 version using typed Arrays)
JavaScript
45
star
17

famous-boxlayout

Layout-view for quickly setting margins or creating flexible layouts
JavaScript
28
star
18

famous-animatedIcon

Material design'ish button-animation using famo.us
JavaScript
27
star
19

famous-flex-datepicker

Date/time picker demo for famo.us
JavaScript
26
star
20

rtfToHtml

Parse RTF and write output as an HTML file (written specifically for InDesign generated RTF, but works for any source)
JavaScript
21
star
21

android-tv-browser-autolaunch

Android TV App (react-native) that auto-launches on boot and shows a browser with a hard-coded URL
Objective-C
20
star
22

famous-sizeconstraint

SizeConstraint makes it possible to set the scale, padding, max-size, min-size and aspect-ratio for famo.us renderables
JavaScript
20
star
23

famous-kenburnscontainer

Famo.us view for performing ken-burns style zooming and panning
JavaScript
19
star
24

famous-listview

famous-listview extends famo.us ScrollContainer with insert/remove animations, selection (single/multiple) and support for a placeholder.
JavaScript
18
star
25

famous-lagometer

Lagometer for famo.us showing the FPS, animation-frames times and script times
JavaScript
18
star
26

famous-bkimagesurface

Drop-in replacement for ImageSurface supporting AspectFit & AspectFill
JavaScript
18
star
27

famous-flex-tabbar

TabBar widget demo for famo.us
JavaScript
18
star
28

famous-flex-tabbarcontroller

TabBarController widget demo for famo.us
JavaScript
17
star
29

famous-white-tile-firebase

Popular white tile (piano tiles) game implemention using famo.us and firebase
JavaScript
17
star
30

famous-refresh-loader

Spinning pull to refresh loader for famo.us
JavaScript
15
star
31

famous-autosizetextarea

Auto-sizing TextareaSurface for famo.us
JavaScript
14
star
32

famous-components

Overview of famo.us components
14
star
33

famous-flex-demo

Demo for showcasing famous-flex layout technology
JavaScript
9
star
34

react-native-magic-move-presentation

Presentation App/Slides for the react-native-magic-move presentation given at ReactEurope 2019
JavaScript
9
star
35

react-native-clipped

Clipping effects and animations for react-native 🍠🥒🍕
JavaScript
7
star
36

famous-flex-tablelayout

iOS inspired table-layout for famo.us
JavaScript
7
star
37

expo-firebase-demo

Firebase Demo running on Expo
TypeScript
6
star
38

famous-white-tile

Popular white tile (piano tiles) game implemention using famo.us
JavaScript
5
star
39

famous-autofontsizesurface

Surface that automatically scales the font-size based on the content.
JavaScript
5
star
40

famous-flex-animationcontroller

Animating from one famo.us view to another in awesome ways
JavaScript
5
star
41

ttvflash

TTV Flash Presentatie App
JavaScript
4
star
42

famous-Starterkit

Starterkit for famo.us (app + web) containing examples, instructions & best practises
JavaScript
4
star
43

famous-resizableImage

Resizable image for famo.us
JavaScript
4
star
44

top2000-stemlijst

Importeer jouw Top 2000 stemlijst eenvoudig naar Spotify, Apple Music of Deezer
TypeScript
4
star
45

firestore-cms

A free, flexible and easy to use CMS for Google Firestore 🎉
JavaScript
4
star
46

famous-sizemodifier

Deprecated - Use famous-sizeconstraint instead
JavaScript
3
star
47

famous-resources

Unofficial list of famo.us resources
2
star
48

expo-git-import-main

TypeScript
2
star
49

wkwebview-crash

Project to intentionally crash WkWebView for testing purposes
Objective-C
2
star
50

famous-lib-tester

Project for testing whether my libraries and famo.us can be build successfully using webpack, browserify, etc...
JavaScript
1
star
51

famous-test-positionabsolute

Test for absolute positioning issue when showing keyboard on mobile (iOS & android)
JavaScript
1
star
52

expo-av-9596-repro

JavaScript
1
star
53

famous-zindex

JavaScript
1
star
54

famous-bling

View templates (with animations) to jumpstart your project or for production use
JavaScript
1
star
55

famous-flex-scrollview-linking

Demo for showcasing FlexScrollView leading & trailing scrollview linking
1
star
56

ijzerenhein-website

My personal public website
JavaScript
1
star
57

famous-physics-playground

Playground for experimenting with physics based animation effects
JavaScript
1
star
58

contributors-code

Life as an open source contributor
1
star
59

expo-av-music-control

Java
1
star
60

famous-flex-truesize-layoutcontroller-demo

JavaScript
1
star
61

scrollview.js

Lightweight & fast javascript scrollview, look no further
JavaScript
1
star
62

famous-playground

General purpose playground repo for famo.us
JavaScript
1
star