Mercurial > linkmap
annotate labeled_marker.js @ 0:fb5784aa45e6 default tip
from monotone. problematic https vs v2 API
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 21 Oct 2012 23:03:51 +0800 |
parents | |
children |
rev | line source |
---|---|
0
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 /* |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 * LabeledMarker Class |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 * |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * Copyright 2007 Mike Purvis (http://uwmike.com) |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 * |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 * Licensed under the Apache License, Version 2.0 (the "License"); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 * you may not use this file except in compliance with the License. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 * You may obtain a copy of the License at |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
9 * |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 * http://www.apache.org/licenses/LICENSE-2.0 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 * |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 * Unless required by applicable law or agreed to in writing, software |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 * distributed under the License is distributed on an "AS IS" BASIS, |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 * See the License for the specific language governing permissions and |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 * limitations under the License. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 * |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 * This class extends the Maps API's standard GMarker class with the ability |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 * to support markers with textual labels. Please see articles here: |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 * |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 * http://googlemapsbook.com/2007/01/22/extending-gmarker/ |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 * http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/ |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 */ |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
25 /* Constructor */ |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 function LabeledMarker(latlng, options){ |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 this.latlng = latlng; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
28 this.labelText = options.labelText || ""; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 this.labelClass = options.labelClass || "markerLabel"; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
30 this.labelOffset = options.labelOffset || new GSize(0, 0); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
31 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
32 this.clickable = options.clickable || true; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
33 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 if (options.draggable) { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 // This version of LabeledMarker doesn't support dragging. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
36 options.draggable = false; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
38 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 GMarker.apply(this, arguments); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
40 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
41 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
42 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
43 /* It's a limitation of JavaScript inheritance that we can't conveniently |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
44 extend GMarker without having to run its constructor. In order for the |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
45 constructor to run, it requires some dummy GLatLng. */ |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 LabeledMarker.prototype = new GMarker(new GLatLng(0, 0)); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 // Creates the text div that goes over the marker. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 LabeledMarker.prototype.initialize = function(map) { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 // Do the GMarker constructor first. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 GMarker.prototype.initialize.apply(this, arguments); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
53 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
54 var div = document.createElement("div"); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
55 div.className = this.labelClass; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 div.innerHTML = this.labelText; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 div.style.position = "absolute"; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
58 map.getPane(G_MAP_MARKER_PANE).appendChild(div); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
59 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
60 if (this.clickable) { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
61 // Pass through events fired on the text div to the marker. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
62 var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout']; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
63 for(var i = 0; i < eventPassthrus.length; i++) { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 var name = eventPassthrus[i]; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
65 GEvent.addDomListener(div, name, newEventPassthru(this, name)); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
66 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
67 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
68 // Mouseover behaviour for the cursor. |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 div.style.cursor = "pointer"; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
71 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
72 this.map = map; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
73 this.div = div; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
75 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 function newEventPassthru(obj, event) { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 return function() { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 GEvent.trigger(obj, event); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
79 }; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 // Redraw the rectangle based on the current projection and zoom level |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 LabeledMarker.prototype.redraw = function(force) { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 GMarker.prototype.redraw.apply(this, arguments); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
86 // We only need to do anything if the coordinate system has changed |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
87 if (!force) return; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
88 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 // Calculate the DIV coordinates of two opposite corners of our bounds to |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
90 // get the size and position of our rectangle |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
91 var p = this.map.fromLatLngToDivPixel(this.latlng); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
92 var z = GOverlay.getZIndex(this.latlng.lat()); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
93 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
94 // Now position our DIV based on the DIV coordinates of our bounds |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
95 this.div.style.left = (p.x + this.labelOffset.width) + "px"; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
96 this.div.style.top = (p.y + this.labelOffset.height) + "px"; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
97 this.div.style.zIndex = z + 1; // in front of the marker |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
98 } |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
99 |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
100 // Remove the main DIV from the map pane, destroy event handlers |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
101 LabeledMarker.prototype.remove = function() { |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
102 GEvent.clearInstanceListeners(this.div); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
103 this.div.parentNode.removeChild(this.div); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
104 this.div = null; |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
105 GMarker.prototype.remove.apply(this, arguments); |
fb5784aa45e6
from monotone. problematic https vs v2 API
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
106 } |