initial API simplification

This commit is contained in:
Vladimir Agafonkin
2012-07-03 17:06:44 +03:00
parent df6ea68438
commit 0d6749a3ac
17 changed files with 174 additions and 87 deletions

View File

@ -19,14 +19,15 @@
<script type="text/javascript">
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution}),
latlng = new L.LatLng(50.5, 30.51);
var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/997/256/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
key: 'BC9A493B41014CAABB98F0471D759707'
});
var map = new L.Map('map', {center: latlng, zoom: 15, layers: [cloudmade]});
//map.on('click', function () { alert('hi'); });
var map = L.map('map')
.setView([50.5, 30.51], 15)
.addLayer(cloudmade);
var markers = new L.FeatureGroup();

View File

@ -16,48 +16,31 @@
<div id="map"></div>
<script>
var map = L.map('map')
.setView([51.505, -0.09], 13);
var map = new L.Map('map');
var cloudmade = new L.TileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
maxZoom: 18,
var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
maxZoom: 18,
key: 'BC9A493B41014CAABB98F0471D759707',
styleId: 997
});
}).addTo(map);
map.setView(new L.LatLng(51.505, -0.09), 13).addLayer(cloudmade);
L.marker([51.5, -0.09])
.bindPopup("<b>Hello world!</b><br />I am a popup.")
.addTo(map)
.openPopup();
L.circle([51.508, -0.11], 500, {color: '#f03', opacity: 0.7})
.bindPopup("I am a circle.")
.addTo(map);
var markerLocation = new L.LatLng(51.5, -0.09),
marker = new L.Marker(markerLocation);
map.addLayer(marker);
marker.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
var circleLocation = new L.LatLng(51.508, -0.11),
circleOptions = {
color: '#f03',
opacity: 0.7
},
circle = new L.Circle(circleLocation, 500, circleOptions);
circle.bindPopup("I am a circle.");
map.addLayer(circle);
var p1 = new L.LatLng(51.509, -0.08),
p2 = new L.LatLng(51.503, -0.06),
p3 = new L.LatLng(51.51, -0.047),
polygonPoints = [ p1, p2, p3 ],
polygon = new L.Polygon(polygonPoints);
polygon.bindPopup("I am a polygon.");
map.addLayer(polygon);
L.polygon([
[51.509, -0.08],
[51.503, -0.06],
[51.51, -0.047]])
.bindPopup("I am a polygon.")
.addTo(map);
</script>
</body>
</html>

View File

@ -15,7 +15,6 @@ L.LatLng = function (/*Number*/ rawLat, /*Number*/ rawLng, /*Boolean*/ noWrap) {
lng = (lng + 180) % 360 + ((lng < -180 || lng === 180) ? 180 : -180); // wrap longtitude into -180..180
}
//TODO change to lat() & lng()
this.lat = lat;
this.lng = lng;
};
@ -58,3 +57,13 @@ L.LatLng.prototype = {
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
};
L.latLng = function (a, b, c) {
if (a instanceof L.LatLng) {
return a;
}
if (a instanceof Array) {
return new L.LatLng(a[0], a[1]);
}
return new L.LatLng(a, b, c);
};

View File

@ -4,10 +4,10 @@
L.LatLngBounds = L.Class.extend({
initialize: function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[])
if (!southWest) {
return;
}
var latlngs = (southWest instanceof Array ? southWest : [southWest, northEast]);
if (!southWest) { return; }
var latlngs = northEast ? [southWest, northEast] : southWest;
for (var i = 0, len = latlngs.length; i < len; i++) {
this.extend(latlngs[i]);
}
@ -15,6 +15,12 @@ L.LatLngBounds = L.Class.extend({
// extend the bounds to contain the given point or bounds
extend: function (/*LatLng or LatLngBounds*/ obj) {
if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
obj = L.latLng(obj)
} else {
obj = L.latLngBounds(obj);
}
if (obj instanceof L.LatLng) {
if (!this._southWest && !this._northEast) {
this._southWest = new L.LatLng(obj.lat, obj.lng, true);
@ -67,6 +73,12 @@ L.LatLngBounds = L.Class.extend({
},
contains: function (/*LatLngBounds or LatLng*/ obj) /*-> Boolean*/ {
if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
obj = L.latLng(obj)
} else {
obj = L.latLngBounds(obj);
}
var sw = this._southWest,
ne = this._northEast,
sw2, ne2;
@ -83,6 +95,8 @@ L.LatLngBounds = L.Class.extend({
},
intersects: function (/*LatLngBounds*/ bounds) {
bounds = L.latLngBounds(bounds);
var sw = this._southWest,
ne = this._northEast,
sw2 = bounds.getSouthWest(),
@ -107,3 +121,10 @@ L.LatLngBounds = L.Class.extend({
});
//TODO International date line?
L.latLngBounds = function (a, b) {
if (a instanceof L.Bounds) {
return a;
}
return new L.LatLngBounds(a, b);
};

View File

@ -3,12 +3,12 @@
*/
L.Bounds = L.Class.extend({
initialize: function (min, max) { //(Point, Point) or Point[]
if (!min) {
return;
}
var points = (min instanceof Array ? min : [min, max]);
initialize: function (a, b) { //(Point, Point) or Point[]
if (!a) { return; }
var points = b ? [a, b] : a;
for (var i = 0, len = points.length; i < len; i++) {
this.extend(points[i]);
}
@ -16,9 +16,10 @@ L.Bounds = L.Class.extend({
// extend the bounds to contain the given point
extend: function (/*Point*/ point) {
point = L.point(point);
if (!this.min && !this.max) {
this.min = new L.Point(point.x, point.y);
this.max = new L.Point(point.x, point.y);
this.min = point.clone();
this.max = point.clone();
} else {
this.min.x = Math.min(point.x, this.min.x);
this.max.x = Math.max(point.x, this.max.x);
@ -44,6 +45,12 @@ L.Bounds = L.Class.extend({
contains: function (/*Bounds or Point*/ obj)/*->Boolean*/ {
var min, max;
if (typeof obj[0] === 'number' || obj instanceof L.Point) {
obj = L.point(obj)
} else {
obj = L.bounds(obj);
}
if (obj instanceof L.Bounds) {
min = obj.min;
max = obj.max;
@ -58,6 +65,8 @@ L.Bounds = L.Class.extend({
},
intersects: function (/*Bounds*/ bounds) {
bounds = L.bounds(bounds);
var min = this.min,
max = this.max,
min2 = bounds.min,
@ -70,3 +79,10 @@ L.Bounds = L.Class.extend({
}
});
L.bounds = function (a, b) {
if (a instanceof L.Bounds) {
return a;
}
return new L.Bounds(a, b);
};

View File

@ -64,3 +64,13 @@ L.Point.prototype = {
L.Util.formatNum(this.y) + ')';
}
};
L.point = function (x, y, round) {
if (x instanceof L.Point) {
return x;
}
if (x instanceof Array) {
return new L.Point(x[0], x[1]);
}
return new L.Point(x, y, round);
};

View File

@ -7,7 +7,7 @@ L.ImageOverlay = L.Class.extend({
initialize: function (url, bounds, options) { // (String, LatLngBounds)
this._url = url;
this._bounds = bounds;
this._bounds = L.latLngBounds(bounds);
L.Util.setOptions(this, options);
},
@ -32,7 +32,7 @@ L.ImageOverlay = L.Class.extend({
onRemove: function (map) {
map.getPanes().overlayPane.removeChild(this._image);
map.off('viewreset', this._reset, this);
if (map.options.zoomAnimation) {

View File

@ -66,7 +66,7 @@ L.Popup = L.Class.extend({
},
setLatLng: function (latlng) {
this._latlng = latlng;
this._latlng = L.latLng(latlng);
this._update();
return this;
},
@ -185,7 +185,7 @@ L.Popup = L.Class.extend({
this._container.style.bottom = this._containerBottom + 'px';
this._container.style.left = this._containerLeft + 'px';
},
_zoomAnimation: function (opt) {
var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center)._round();

View File

@ -17,7 +17,7 @@ L.Marker = L.Class.extend({
initialize: function (latlng, options) {
L.Util.setOptions(this, options);
this._latlng = latlng;
this._latlng = L.latLng(latlng);
},
onAdd: function (map) {
@ -33,6 +33,11 @@ L.Marker = L.Class.extend({
this.update();
},
addTo: function (map) {
map.addLayer(this);
return this;
},
onRemove: function (map) {
this._removeIcon();
@ -54,7 +59,7 @@ L.Marker = L.Class.extend({
},
setLatLng: function (latlng) {
this._latlng = latlng;
this._latlng = L.latLng(latlng);
this.update();
@ -195,3 +200,7 @@ L.Marker = L.Class.extend({
L.DomUtil.setOpacity(this._icon, this.options.opacity);
}
});
L.marker = function (latlng, options) {
return new L.Marker(latlng, options);
};

View File

@ -74,6 +74,11 @@ L.TileLayer = L.Class.extend({
this._update();
},
addTo: function (map) {
map.addLayer(this);
return this;
},
onRemove: function (map) {
map._panes.tilePane.removeChild(this._container);
@ -410,3 +415,7 @@ L.TileLayer = L.Class.extend({
layer._tileLoaded();
}
});
L.tileLayer = function (url, options) {
return new L.TileLayer(url, options);
};

View File

@ -6,7 +6,7 @@ L.Circle = L.Path.extend({
initialize: function (latlng, radius, options) {
L.Path.prototype.initialize.call(this, options);
this._latlng = latlng;
this._latlng = L.latLng(latlng);
this._mRadius = radius;
},
@ -15,7 +15,7 @@ L.Circle = L.Path.extend({
},
setLatLng: function (latlng) {
this._latlng = latlng;
this._latlng = L.latLng(latlng);
return this.redraw();
},
@ -44,7 +44,7 @@ L.Circle = L.Path.extend({
return new L.LatLngBounds(sw, ne);
},
getLatLng: function () {
return this._latlng;
},
@ -67,7 +67,7 @@ L.Circle = L.Path.extend({
return "AL " + p.x + "," + p.y + " " + r + "," + r + " 0," + (65535 * 360);
}
},
getRadius: function () {
return this._mRadius;
},
@ -91,3 +91,7 @@ L.Circle = L.Path.extend({
p.x + r < vp.min.x || p.y + r < vp.min.y;
}
});
L.circle = function (latlng, radius, options) {
return new L.Circle(latlng, radius, options);
};

View File

@ -22,3 +22,7 @@ L.CircleMarker = L.Circle.extend({
return this.redraw();
}
});
L.circleMarker = function (latlng, options) {
return new L.CircleMarker(latlng, options);
};

View File

@ -42,6 +42,11 @@ L.Path = L.Class.extend({
}, this);
},
addTo: function (map) {
map.addLayer(this);
return this;
},
onRemove: function (map) {
this._map = null;

View File

@ -10,10 +10,10 @@ L.Polygon = L.Polyline.extend({
initialize: function (latlngs, options) {
L.Polyline.prototype.initialize.call(this, latlngs, options);
if (latlngs && (latlngs[0] instanceof Array)) {
/*if (latlngs && (latlngs[0] instanceof Array)) {
this._latlngs = latlngs[0];
this._holes = latlngs.slice(1);
}
}*/
},
projectLatlngs: function () {
@ -62,3 +62,7 @@ L.Polygon = L.Polyline.extend({
return str + (L.Browser.svg ? 'z' : 'x');
}
});
L.polygon = function (latlngs, options) {
return new L.Polygon(latlngs, options);
};

View File

@ -169,3 +169,7 @@ L.Polyline = L.Path.extend({
L.Path.prototype._updatePath.call(this);
}
});
L.polyline = function (latlngs, options) {
return new L.Polyline(latlngs, options);
};

View File

@ -9,15 +9,19 @@ L.Rectangle = L.Polygon.extend({
setBounds: function (latLngBounds) {
this.setLatLngs(this._boundsToLatLngs(latLngBounds));
},
_boundsToLatLngs: function (latLngBounds) {
return [
latLngBounds.getSouthWest(),
latLngBounds.getNorthWest(),
latLngBounds.getNorthEast(),
latLngBounds.getSouthEast(),
latLngBounds.getSouthWest()
];
},
_boundsToLatLngs: function (latLngBounds) {
return [
latLngBounds.getSouthWest(),
latLngBounds.getNorthWest(),
latLngBounds.getNorthEast(),
latLngBounds.getSouthEast(),
latLngBounds.getSouthWest()
];
}
});
L.rectangle = function (latLngBounds, options) {
return new L.Rectangle(latLngBounds, options);
};

View File

@ -33,7 +33,7 @@ L.Map = L.Class.extend({
}
if (options.center && options.zoom !== undefined) {
this.setView(options.center, options.zoom, true);
this.setView(L.latLng(options.center), options.zoom, true);
}
this._initLayers(options.layers);
@ -44,7 +44,7 @@ L.Map = L.Class.extend({
// replaced by animation-powered implementation in Map.PanAnimation.js
setView: function (center, zoom) {
this._resetView(center, this._limitZoom(zoom));
this._resetView(L.latLng(center), this._limitZoom(zoom));
return this;
},
@ -246,7 +246,7 @@ L.Map = L.Class.extend({
var bounds = this.getPixelBounds(),
sw = this.unproject(bounds.getBottomLeft()),
ne = this.unproject(bounds.getTopRight());
return new L.LatLngBounds(sw, ne);
},
@ -323,12 +323,12 @@ L.Map = L.Class.extend({
getPanes: function () {
return this._panes;
},
getContainer: function () {
return this._container;
},
// TODO replace with universal implementation after refactoring projections
getZoomScale: function (toZoom) {
@ -345,7 +345,7 @@ L.Map = L.Class.extend({
project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
zoom = zoom === undefined ? this._zoom : zoom;
return this.options.crs.latLngToPoint(latlng, zoom);
return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
},
unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
@ -358,7 +358,7 @@ L.Map = L.Class.extend({
},
latLngToLayerPoint: function (latlng) { // (LatLng)
return this.project(latlng)._round()._subtract(this._initialTopLeftPoint);
return this.project(L.latLng(latlng))._round()._subtract(this._initialTopLeftPoint);
},
containerPointToLayerPoint: function (point) { // (Point)
@ -374,7 +374,7 @@ L.Map = L.Class.extend({
},
latLngToContainerPoint: function (latlng) {
return this.layerPointToContainerPoint(this.latLngToLayerPoint(latlng));
return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
},
mouseEventToContainerPoint: function (e) { // (MouseEvent)
@ -552,7 +552,7 @@ L.Map = L.Class.extend({
_onMouseClick: function (e) {
if (!this._loaded || (this.dragging && this.dragging.moved())) { return; }
this.fire('preclick');
this._fireMouseEvent(e);
},
@ -642,4 +642,8 @@ L.Map.addInitHook = function (fn) {
};
this.prototype._initializers.push(init);
};
};
L.map = function (id, options) {
return new L.Map(id, options);
};