-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #656.
- Loading branch information
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Mapbox GL JS debug page</title> | ||
<meta charset='utf-8'> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
|
||
<link rel='stylesheet' href='../dist/mapbox-gl.css' /> | ||
<style> | ||
body { margin: 0; padding: 0; } | ||
html, body, #map { height: 100%; } | ||
|
||
.mapboxgl-overlay { | ||
width: 10px; | ||
height: 10px; | ||
background: red; | ||
margin-top: -5px; | ||
margin-left: -5px; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id='map'></div> | ||
|
||
<script src='mapbox-gl.js'></script> | ||
<script src='access-token.js'></script> | ||
|
||
<script> | ||
var map = window.map = new mapboxgl.Map({ | ||
container: 'map', | ||
zoom: 12.5, | ||
center: [-77.01866, 38.888], | ||
style: 'mapbox://styles/mapbox/streets-v9', | ||
hash: true | ||
}); | ||
|
||
function addRandomOverlay() { | ||
var bounds = map.getBounds(); | ||
var s = bounds.getSouth(); | ||
var n = bounds.getNorth(); | ||
var w = bounds.getWest(); | ||
var e = bounds.getEast(); | ||
|
||
var lng = w + (e - w) * Math.random(); | ||
var lat = s + (n - s) * Math.random(); | ||
|
||
var overlay = new mapboxgl.Overlay() | ||
.setLngLat([lng, lat]) | ||
.addTo(map); | ||
|
||
overlay.getElement().onclick = function(e) { | ||
alert('Hello world'); | ||
e.stopPropagation(); | ||
}; | ||
} | ||
|
||
for (var i = 0; i < 100; i++) addRandomOverlay(); | ||
|
||
map.addControl(new mapboxgl.Navigation()); | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable */ | ||
'use strict'; | ||
|
||
module.exports = Overlay; | ||
|
||
var DOM = require('../util/dom'); | ||
var LngLat = require('../geo/lng_lat'); | ||
|
||
/** | ||
* Creates an overlay component | ||
* @class Overlay | ||
* @param {HTMLElement=} element DOM element to use as an overlay (creates a div element by default) | ||
* @example | ||
* var overlay = new mapboxgl.Overlay() | ||
* .setLngLat([30.5, 50.5]) | ||
* .addTo(map); | ||
*/ | ||
function Overlay(element) { | ||
if (!element) { | ||
element = DOM.create('div'); | ||
} | ||
element.classList.add('mapboxgl-overlay'); | ||
this._el = element; | ||
this._update = this._update.bind(this); | ||
} | ||
|
||
Overlay.prototype = /** @lends Overlay.prototype */{ | ||
/** | ||
* Attaches the overlay to a map | ||
* @param {Map} map | ||
* @returns {Overlay} `this` | ||
*/ | ||
addTo: function(map) { | ||
this.remove(); | ||
this._map = map; | ||
map.getCanvasContainer().appendChild(this._el); | ||
map.on('move', this._update); | ||
this._update(); | ||
return this; | ||
}, | ||
|
||
/** | ||
* Removes the overlay from a map | ||
* @example | ||
* var overlay = new mapboxgl.Overlay().addTo(map); | ||
* overlay.remove(); | ||
* @returns {Overlay} `this` | ||
*/ | ||
remove: function() { | ||
if (this._map) { | ||
this._map.off('move', this._update); | ||
this._map = null; | ||
} | ||
var parent = this._el.parentNode; | ||
if (parent) parent.removeChild(this._el); | ||
return this; | ||
}, | ||
|
||
/** | ||
* Get the overlay's geographical location | ||
* @returns {LngLat} | ||
*/ | ||
getLngLat: function() { | ||
return this._lngLat; | ||
}, | ||
|
||
/** | ||
* Set the overlay's geographical position and move it. | ||
* @param {LngLat} lnglat | ||
* @returns {Popup} `this` | ||
*/ | ||
setLngLat: function(lnglat) { | ||
this._lngLat = LngLat.convert(lnglat); | ||
this._update(); | ||
return this; | ||
}, | ||
|
||
getElement: function() { | ||
return this._el; | ||
}, | ||
|
||
_update: function() { | ||
if (!this._map) return; | ||
var pos = this._map.project(this._lngLat); | ||
DOM.setTransform(this._el, 'translate(' + pos.x + 'px,' + pos.y + 'px)'); | ||
} | ||
}; |