Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
emikhalev committed Sep 24, 2013
1 parent 4ebc118 commit 29c96df
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 3 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Eugene Mikhalev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
26 changes: 23 additions & 3 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,24 @@
leaflet-2gis
============

Leaflet 2GIS plugin Leaflet 2GIS plugin
===================
This plugin add 2GIS support to the leaflet.

## Requirements
Leaflet 0.6 or newer
2GIS api library (see example)

## Demo
http://

## Usage
```javascript
var map = new L.Map("map", {
center: new L.LatLng(54.99014, 73.365319),
zoom: 10,
zoomAnimation: true
});
var dgis = new L.DGis();
map.addLayer(dgis);
```

## License
Leaflet 2GIS is free software, and may be redistributed under the MIT-LICENSE.
163 changes: 163 additions & 0 deletions dgis.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,163 @@
L.DGis = L.Class.extend({
includes: L.Mixin.Events,

options: {
attribution: '',
opacity: 0.99
},

// Init object
initialize: function(options) {
L.Util.setOptions(this, options);
},

// Layer events
onAdd: function(map, insertAtTheBottom) {
this._map = map;
this._insertAtTheBottom = insertAtTheBottom;

// Create map container
this._initContainer();

// Create map object
this._initMapObject();

// Setting events handlers
this._setHandlers();

map._controlCorners['bottomright'].style.marginBottom = "3em";
this._reset();
},
onRemove: function(map) {
// Remove map container
this._map._container.removeChild(this._container);
// Unset events handlers
this._unsetHandlers();
this._map._controlCorners['bottomright'].style.marginBottom = "0em";
},

// Methods
getAttribution: function() {
return this.options.attribution;
},

setOpacity: function(opacity) {
this.options.opacity = opacity;
if (opacity < 1) {
L.DomUtil.setOpacity(this._container, opacity);
}
},

setElementSize: function(e, size) {
e.style.width = size.x + "px";
e.style.height = size.y + "px";
},

// Helpers
_initContainer: function() {
var tilePane = this._map._container,
first = tilePane.firstChild;

if (!this._container) {
this._container = L.DomUtil.create('div', 'leaflet-2gis-layer leaflet-top leaflet-left leaflet-zoom-hide');
this._container.id = "_2GisContainer_" + L.Util.stamp(this);
this._container.style.zIndex = "auto";
}

if (this.options.overlay) {
first = this._map._container.getElementsByClassName('leaflet-map-pane')[0];
first = first.nextSibling;
// XXX: Bug with layer order
if (L.Browser.opera)
this._container.className += " leaflet-objects-pane";
}
tilePane.insertBefore(this._container, first);

this.setOpacity(this.options.opacity);
this.setElementSize(this._container, this._map.getSize());
},

_initMapObject: function() {
// Init main object
if (typeof this._map == "undefined") {
return;
}
if (typeof this._dg == "undefined") {
this._dg = new DG.Map(this._container);
this._dg.fullscreen.disable();
}

// Set map center and zoom
this._setCenter();

var zoom = this._map.getZoom();
this._dg.setZoom(zoom);
},

_setCenter: function(){
var center = this._map.getCenter();
this._dg.setCenter(new DG.GeoPoint(center.lng, center.lat) );
},

_setHandlers: function(){
this._map.on('viewreset', this._resetCallback, this);
this._limitedUpdate = L.Util.limitExecByInterval(this._move, 150, this);
this._map.on('move', this._move, this);
this._map.on('moveend', this._move, this);
},

_unsetHandlers: function(){
this._map.off('viewreset', this._resetCallback, this);
this._map.off('move', this._move, this);
this._map.off('moveend', this._move, this);
},

// Event handlers
_resetCallback: function(e) {
this._reset(e.hard);
},

_reset: function(clearOldContainer) {
this._initContainer();
},

_move: function(force) {
if (typeof this._dg == "undefined") {
return;
}
if (!this._map) return;
this._resize(force);
// Zoom
var zoom = this._map.getZoom();
if (force || this._dg.getZoom() != zoom)
this._dg.setZoom(zoom);

// Position
var llCenter = this._map.getCenter(); // L.LatLng
var dgCenter = this._dg.getCenter(); // DG.GeoPoint
var dgllCenter = [dgCenter.lat, dgCenter.lon]; // L.LatLng <- DG.GeoPoint

var llCenterPx = this._map.project(llCenter); // L.Point
var dgCenterPx = this._map.project(dgllCenter); // L.Point

var offsetX = dgCenterPx.x - llCenterPx.x;
var offsetY = dgCenterPx.y - llCenterPx.y;

this._dg.moveE( -offsetX ); // X-offset (East)
this._dg.moveN( offsetY ); // Y-offset (North)

},

_resize: function(force) {
if (typeof this._dg == "undefined") {
return;
}
var size = this._map.getSize(), style = this._container.style;
if (style.width == size.x + "px" &&
style.height == size.y + "px")
if (force != true) return;
this.setElementSize(this._container, size);
var b = this._map.getBounds(), sw = b.getSouthWest(), ne = b.getNorthEast();
this._dg.redraw();
}
});
33 changes: 33 additions & 0 deletions example.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<title>Leaflet: 2GIS Example</title>

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
<![endif]-->

<script src="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.js"></script>
<script src="dgis.js"></script>
<script src="http://maps.api.2gis.ru/1.0" type="text/javascript"></script>

</head>
<body>

<div id="map" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;"></div>

<script type="text/javascript">
var map = new L.Map("map", {
center: new L.LatLng(54.99014, 73.365319),
zoom: 10,
zoomAnimation: false
});
var dgis = new L.DGis();
map.addLayer(dgis);
</script>

</body>
</html>

0 comments on commit 29c96df

Please sign in to comment.