1
- ( function ( win , doc ) {
1
+ ( function ( ) {
2
2
'use strict' ;
3
3
4
- var
5
- view = new ol . View ( {
6
- center : [ 0 , 0 ] ,
7
- zoom : 3 ,
8
- minZoom : 2 ,
9
- maxZoom : 20
10
- } ) ,
11
- vectorLayer = new ol . layer . Vector ( {
12
- source : new ol . source . Vector ( )
13
- } ) ,
14
- baseLayer = new ol . layer . Tile ( {
15
- preload : Infinity ,
16
- opacity : 1 ,
17
- source : new ol . source . MapQuest ( { layer : 'osm' } )
18
- } ) ,
19
- map = new ol . Map ( {
20
- target : doc . getElementById ( 'map' ) ,
21
- loadTilesWhileAnimating : true ,
22
- view : view ,
23
- layers : [ baseLayer , vectorLayer ]
24
- } ) ,
25
- // from https://github.com/DmitryBaranovskiy/raphael
26
- elastic = function ( t ) {
27
- return Math . pow ( 2 , - 10 * t ) * Math . sin ( ( t - 0.075 ) * ( 2 * Math . PI ) / 0.3 ) + 1 ;
28
- } ,
29
- center = function ( obj ) {
30
- var pan = ol . animation . pan ( {
31
- duration : 1000 ,
32
- easing : elastic ,
33
- source : view . getCenter ( )
34
- } ) ;
35
- map . beforeRender ( pan ) ;
36
- view . setCenter ( obj . coordinate ) ;
37
- } ,
38
- marker = function ( obj ) {
39
- var
40
- coord4326 = ol . proj . transform ( obj . coordinate , 'EPSG:3857' , 'EPSG:4326' ) ,
41
- template = 'Coordinate is ({x} | {y})' ,
42
- iconStyle = new ol . style . Style ( {
43
- image : new ol . style . Icon ( {
44
- scale : .6 ,
45
- src : 'img/marker.png'
46
- } ) ,
47
- text : new ol . style . Text ( {
48
- offsetY : 25 ,
49
- text : ol . coordinate . format ( coord4326 , template , 2 ) ,
50
- font : '15px Open Sans,sans-serif' ,
51
- fill : new ol . style . Fill ( { color : '#111' } ) ,
52
- stroke : new ol . style . Stroke ( {
53
- color : '#eee' , width : 2
54
- } )
55
- } )
56
- } ) ,
57
- feature = new ol . Feature ( {
58
- type : 'removable' ,
59
- geometry : new ol . geom . Point ( obj . coordinate )
4
+ var view = new ol . View ( {
5
+ center : [ 0 , 0 ] ,
6
+ zoom : 4 ,
7
+ minZoom : 2 ,
8
+ maxZoom : 20
9
+ } ) ,
10
+ vectorLayer = new ol . layer . Vector ( {
11
+ source : new ol . source . Vector ( )
12
+ } ) ,
13
+ baseLayer = new ol . layer . Tile ( {
14
+ source : new ol . source . OSM ( )
15
+ } ) ,
16
+ map = new ol . Map ( {
17
+ target : document . getElementById ( 'map' ) ,
18
+ view : view ,
19
+ layers : [ baseLayer , vectorLayer ]
20
+ } ) ,
21
+ // from https://github.com/DmitryBaranovskiy/raphael
22
+ elastic = function ( t ) {
23
+ return Math . pow ( 2 , - 10 * t ) * Math . sin ( ( t - 0.075 ) * ( 2 * Math . PI ) / 0.3 ) + 1 ;
24
+ } ,
25
+ center = function ( obj ) {
26
+ var pan = ol . animation . pan ( {
27
+ duration : 1000 ,
28
+ easing : elastic ,
29
+ source : view . getCenter ( )
60
30
} ) ;
61
-
62
- feature . setStyle ( iconStyle ) ;
63
- vectorLayer . getSource ( ) . addFeature ( feature ) ;
64
- } ;
31
+ map . beforeRender ( pan ) ;
32
+ view . setCenter ( obj . coordinate ) ;
33
+ } ,
34
+ marker = function ( obj ) {
35
+ var coord4326 = ol . proj . transform ( obj . coordinate , 'EPSG:3857' , 'EPSG:4326' ) ,
36
+ template = 'Coordinate is ({x} | {y})' ,
37
+ iconStyle = new ol . style . Style ( {
38
+ image : new ol . style . Icon ( {
39
+ scale : .6 ,
40
+ src : 'img/pin_drop.png'
41
+ } ) ,
42
+ text : new ol . style . Text ( {
43
+ offsetY : 25 ,
44
+ text : ol . coordinate . format ( coord4326 , template , 2 ) ,
45
+ font : '15px Open Sans,sans-serif' ,
46
+ fill : new ol . style . Fill ( { color : '#111' } ) ,
47
+ stroke : new ol . style . Stroke ( {
48
+ color : '#eee' , width : 2
49
+ } )
50
+ } )
51
+ } ) ,
52
+ feature = new ol . Feature ( {
53
+ type : 'removable' ,
54
+ geometry : new ol . geom . Point ( obj . coordinate )
55
+ } ) ;
56
+
57
+ feature . setStyle ( iconStyle ) ;
58
+ vectorLayer . getSource ( ) . addFeature ( feature ) ;
59
+ } ;
65
60
66
61
var contextmenu_items = [
67
62
{
68
63
text : 'Center map here' ,
69
64
classname : 'bold' ,
65
+ icon : 'img/center.png' ,
70
66
callback : center
71
67
} ,
72
68
{
73
69
text : 'Some Actions' ,
70
+ icon : 'img/view_list.png' ,
74
71
items : [
75
72
{
76
73
text : 'Center map here' ,
74
+ icon : 'img/center.png' ,
77
75
callback : center
78
76
} ,
79
77
{
80
- text : 'Center map here' ,
81
- callback : center
78
+ text : 'Add a Marker' ,
79
+ icon : 'img/pin_drop.png' ,
80
+ callback : marker
82
81
}
83
82
]
84
83
} ,
85
84
{
86
85
text : 'Add a Marker' ,
87
- icon : 'img/marker .png' ,
86
+ icon : 'img/pin_drop .png' ,
88
87
callback : marker
89
88
} ,
90
89
'-' // this is a separator
106
105
callback : removeMarker
107
106
} ;
108
107
109
- var changed = false ;
110
108
contextmenu . on ( 'open' , function ( evt ) {
111
109
var feature = map . forEachFeatureAtPixel ( evt . pixel , function ( ft , l ) {
112
110
return ft ;
117
115
marker : feature
118
116
} ;
119
117
contextmenu . push ( removeMarkerItem ) ;
120
- changed = true ;
121
- } else if ( changed ) {
118
+ } else {
122
119
contextmenu . clear ( ) ;
123
120
contextmenu . extend ( contextmenu_items ) ;
124
121
contextmenu . extend ( contextmenu . getDefaultItems ( ) ) ;
125
- changed = false ;
126
122
}
127
123
} ) ;
128
124
135
131
map . getTarget ( ) . style . cursor = hit ? 'pointer' : '' ;
136
132
} ) ;
137
133
138
- } ) ( window , document ) ;
134
+ } ) ( ) ;
0 commit comments