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