99function addContextMenus ( map ) {
1010 // First we need to subscribe to the "contextmenu" event on the map
1111 map . addEventListener ( 'contextmenu' , function ( e ) {
12- // As we already handle contextmenu event callback on circle object,
13- // we don't do anything if target is different than the map.
14- if ( e . target !== map ) {
15- return ;
16- }
12+ // Create and push the proper context items according to the event target
13+ if ( e . target instanceof H . map . Circle ) {
14+ // Add a single item to the context menu displaying "Remove circle"
15+ e . items . push (
16+ new H . util . ContextItem ( {
17+ label : 'Remove circle' ,
18+ callback : function ( ) {
19+ map . removeObject ( e . target ) ;
20+ }
21+ } )
22+ ) ;
23+ } else {
24+ // "contextmenu" event might be triggered not only by a pointer,
25+ // but a keyboard button as well. That's why ContextMenuEvent
26+ // doesn't have a "currentPointer" property.
27+ // Instead it has "viewportX" and "viewportY" properties
28+ // for the associates position.
1729
18- // "contextmenu" event might be triggered not only by a pointer,
19- // but a keyboard button as well. That's why ContextMenuEvent
20- // doesn't have a "currentPointer" property.
21- // Instead it has "viewportX" and "viewportY" properties
22- // for the associates position.
30+ // Get geo coordinates from the screen coordinates.
31+ var coord = map . screenToGeo ( e . viewportX , e . viewportY ) ;
2332
24- // Get geo coordinates from the screen coordinates.
25- var coord = map . screenToGeo ( e . viewportX , e . viewportY ) ;
33+ // In order to add menu items, you have to push them to the "items"
34+ // property of the event object. That has to be done synchronously, otherwise
35+ // the ui component will not contain them. However you can change the menu entry
36+ // text asynchronously.
37+ e . items . push (
38+ // Create a menu item, that has only a label,
39+ // which displays the current coordinates.
40+ new H . util . ContextItem ( {
41+ label : [
42+ Math . abs ( coord . lat . toFixed ( 4 ) ) + ( ( coord . lat > 0 ) ? 'N' : 'S' ) ,
43+ Math . abs ( coord . lng . toFixed ( 4 ) ) + ( ( coord . lng > 0 ) ? 'E' : 'W' )
44+ ] . join ( ' ' )
45+ } ) ,
46+ // Create an item, that will change the map center when clicking on it.
47+ new H . util . ContextItem ( {
48+ label : 'Center map here' ,
49+ callback : function ( ) {
50+ map . setCenter ( coord , true ) ;
51+ }
52+ } ) ,
53+ // It is possible to add a seperator between items in order to logically group them.
54+ H . util . ContextItem . SEPARATOR ,
55+ // This menu item will add a new circle to the map
56+ new H . util . ContextItem ( {
57+ label : 'Add circle' ,
58+ callback : function ( ) {
59+ // Create and add the circle to the map
60+ map . addObject ( new H . map . Circle ( coord , 5000 ) ) ;
61+ }
62+ } )
63+ ) ;
64+ }
2665
27- // In order to add menu items, you have to push them to the "items"
28- // property of the event object. That has to be done synchronously, otherwise
29- // the ui component will not contain them. However you can change the menu entry
30- // text asynchronously.
31- e . items . push (
32- // Create a menu item, that has only a label,
33- // which displays the current coordinates.
34- new H . util . ContextItem ( {
35- label : [
36- Math . abs ( coord . lat . toFixed ( 4 ) ) + ( ( coord . lat > 0 ) ? 'N' : 'S' ) ,
37- Math . abs ( coord . lng . toFixed ( 4 ) ) + ( ( coord . lng > 0 ) ? 'E' : 'W' )
38- ] . join ( ' ' )
39- } ) ,
40- // Create an item, that will change the map center when clicking on it.
41- new H . util . ContextItem ( {
42- label : 'Center map here' ,
43- callback : function ( ) {
44- map . setCenter ( coord , true ) ;
45- }
46- } ) ,
47- // It is possible to add a seperator between items in order to logically group them.
48- H . util . ContextItem . SEPARATOR ,
49- // This menu item will add a new circle to the map
50- new H . util . ContextItem ( {
51- label : 'Add circle' ,
52- callback : addCircle . bind ( map , coord )
53- } )
54- ) ;
5566 } ) ;
5667}
5768
58- /**
59- * Adds a circle which has a context menu item to remove itself.
60- *
61- * @this H.Map
62- * @param {H.geo.Point } coord Circle center coordinates
63- */
64- function addCircle ( coord ) {
65- // Create a new circle object
66- var circle = new H . map . Circle ( coord , 5000 ) ,
67- map = this ;
68-
69- // Subscribe to the "contextmenu" eventas we did for the map.
70- circle . addEventListener ( 'contextmenu' , function ( e ) {
71- // Add another menu item,
72- // that will be visible only when clicking on this object.
73- //
74- // New item doesn't replace items, which are added by the map.
75- // So we may want to add a separator to between them.
76- e . items . push (
77- new H . util . ContextItem ( {
78- label : 'Remove' ,
79- callback : function ( ) {
80- map . removeObject ( circle ) ;
81- }
82- } )
83- ) ;
84- } ) ;
85-
86- // Make the circle visible, by adding it to the map
87- map . addObject ( circle ) ;
88- }
8969
9070/**
9171 * Boilerplate map initialization code starts below:
@@ -99,7 +79,7 @@ var defaultLayers = platform.createDefaultLayers();
9979
10080// Step 2: initialize a map
10181var map = new H . Map ( document . getElementById ( 'map' ) , defaultLayers . vector . normal . map , {
102- center : { lat : 52.55006203880433 , lng : 13.27548854220585 } ,
82+ center : { lat : 52.55006203880433 , lng : 13.27548854220585 } ,
10383 zoom : 9 ,
10484 pixelRatio : window . devicePixelRatio || 1
10585} ) ;
0 commit comments