-
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.
* add an overlay class Refs #656. * Rename "Overlay" to "ElementMarker" (#2804) * Rename "Overlay" to "DOMMarker" * Rename DOMMarker to ElementMarker * Remove unnecessary @Lends annotation
- 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-marker { | ||
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 addRandomMarker() { | ||
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 marker = new mapboxgl.ElementMarker() | ||
.setLngLat([lng, lat]) | ||
.addTo(map); | ||
|
||
marker.getElement().onclick = function(e) { | ||
alert('Hello world'); | ||
e.stopPropagation(); | ||
}; | ||
} | ||
|
||
for (var i = 0; i < 100; i++) addRandomMarker(); | ||
|
||
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 = ElementMarker; | ||
|
||
var DOM = require('../util/dom'); | ||
var LngLat = require('../geo/lng_lat'); | ||
|
||
/** | ||
* Creates a marker component | ||
* @class ElementMarker | ||
* @param {HTMLElement=} element DOM element to use as a marker (creates a div element by default) | ||
* @example | ||
* var marker = new mapboxgl.ElementMarker() | ||
* .setLngLat([30.5, 50.5]) | ||
* .addTo(map); | ||
*/ | ||
function ElementMarker(element) { | ||
if (!element) { | ||
element = DOM.create('div'); | ||
} | ||
element.classList.add('mapboxgl-marker'); | ||
this._el = element; | ||
this._update = this._update.bind(this); | ||
} | ||
|
||
ElementMarker.prototype = { | ||
/** | ||
* Attaches the marker to a map | ||
* @param {Map} map | ||
* @returns {ElementMarker} `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 marker from a map | ||
* @example | ||
* var marker = new mapboxgl.ElementMarker().addTo(map); | ||
* marker.remove(); | ||
* @returns {ElementMarker} `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 marker's geographical location | ||
* @returns {LngLat} | ||
*/ | ||
getLngLat: function() { | ||
return this._lngLat; | ||
}, | ||
|
||
/** | ||
* Set the marker'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)'); | ||
} | ||
}; |