hjg
2024-07-09 30304784e82d4bba24121328da8eb8490aec4f4f
提交 | 用户 | 时间
58d006 1 <!DOCTYPE html>
A 2 <html>
3 <head>
4   <meta charset="utf-8">
5   <title>GMaps.js &mdash; Context menu</title>
6   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
7   <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
8   <script type="text/javascript" src="../gmaps.js"></script>
9   <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css" />
10   <link rel="stylesheet" type="text/css" href="examples.css" />
11   <script type="text/javascript">
12     var map;
13     $(document).ready(function(){
14       map = new GMaps({
15         el: '#map',
16         lat: -12.043333,
17         lng: -77.028333
18       });
19       map.setContextMenu({
20         control: 'map',
21         options: [{
22           title: 'Add marker',
23           name: 'add_marker',
24           action: function(e){
25             console.log(e.latLng.lat());
26             console.log(e.latLng.lng());
27             this.addMarker({
28               lat: e.latLng.lat(),
29               lng: e.latLng.lng(),
30               title: 'New marker'
31             });
32             this.hideContextMenu();
33           }
34         }, {
35           title: 'Center here',
36           name: 'center_here',
37           action: function(e){
38             this.setCenter(e.latLng.lat(), e.latLng.lng());
39           }
40         }]
41       });
42       map.setContextMenu({
43         control: 'marker',
44         options: [{
45           title: 'Center here',
46           name: 'center_here',
47           action: function(e){
48             this.setCenter(e.latLng.lat(), e.latLng.lng());
49           }
50         }]
51       });
52     });
53   </script>
54 </head>
55 <body>
56   <h1>GMaps.js &mdash; Context menu</h1>
57   <div class="row">
58     <div class="span11">
59       <div id="map"></div>
60     </div>
61     <div class="span6">
62       <p>You can define a context menu (which will show on right click) with:</p>
63       <pre>map.setContextMenu({
64   control: 'map',
65   options: [{
66     title: 'Add marker',
67     name: 'add_marker',
68     action: function(e){
69       this.addMarker({
70         lat: e.latLng.lat(),
71         lng: e.latLng.lng(),
72         title: 'New marker'
73       });
74     }
75   }, {
76     title: 'Center here',
77     name: 'center_here',
78     action: function(e){
79       this.setCenter(e.latLng.lat(), e.latLng.lng());
80     }
81   }]
82 });</pre>
83       <p>You must define the <strong>control</strong> that the context menu is attached (<strong>map</strong> or <strong>marker</strong>) and an array of <strong>options</strong> with <code>title</code>, <code>name</code> and <code>action</code> Inside <code>action</code> you can use <code>this</code> for the GMaps.js object (<code>map</code> in this case) and MouseEvent object <code>e</code>.</p>
84     </div>
85   </div>
86 </body>
87 </html>