source: main/trunk/greenstone3/web/interfaces/default/js/label-overlay-class.js@ 33081

Last change on this file since 33081 was 33081, checked in by wy59, 5 years ago
  1. Beginnings of label for shapes. 2. Commented out rough working sample code for label appearing on map (if in future we want a label appearing on SHAPES in the map).
File size: 3.6 KB
Line 
1LabelOverlay.prototype = new google.maps.OverlayView();
2
3/** @constructor */
4function LabelOverlay(map) {
5
6 // Initialize all properties.
7 //this.centre_ = map.center;
8 this.map_ = map;
9
10 // Define a property to hold the image's div. We'll
11 // actually create this div upon receipt of the onAdd()
12 // method so we'll leave it null for now.
13 this.div_ = null;
14
15 // Explicitly call setMap on this overlay.
16 this.setMap(map);
17}
18
19// https://stackoverflow.com/questions/16205734/draw-text-on-google-maps talks about using custom overlays
20// Custom Overlays: https://developers.google.com/maps/documentation/javascript/customoverlays
21// General: https://developers.google.com/maps/documentation/javascript/drawinglayer
22// https://webapps.stackexchange.com/questions/72849/is-it-possible-to-attach-a-label-to-a-line-in-the-google-drawings-app
23// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
24// https://developers.google.com/maps/documentation/javascript/examples/marker-labels
25// ! https://stackoverflow.com/questions/20138882/text-labels-on-google-maps-v3
26// https://medium.com/@barvysta/google-marker-api-lets-play-level-1-dynamic-label-on-marker-f9b94f2e3585
27
28// Draggable text/label custom overlays
29// https://www.gmapgis.com/
30// http://jsfiddle.net/doktormolle/QRuW8/
31// https://stackoverflow.com/questions/14865550/google-maps-canvas-custom-overlay-zoom
32// https://ephesoft.com/docs/2019-1/review-validation/zoom-overlay-for-extracted-content/
33LabelOverlay.prototype.onAdd = function () {
34
35 console.log("label onAdd called!");
36
37
38 var div = document.createElement('div');
39 div.style.borderStyle = 'solid';
40 div.style.borderWidth = '1px';
41 div.style.backgroundColor = 'white';
42 div.style.position = 'absolute';
43 div.style.padding = '4px';
44 div.style.zIndex = 1000;
45 div.innerHTML = "PINKY WAS HERE!";
46
47 this.div_ = div;
48
49 // Add the element to the "overlayLayer" pane.
50 var panes = this.getPanes();
51 panes.overlayLayer.appendChild(div);
52
53};
54
55LabelOverlay.prototype.draw = function() {
56 console.log("label draw called!");
57
58 // We use the south-west and north-east
59 // coordinates of the overlay to peg it to the correct position and size.
60 // To do this, we need to retrieve the projection from the overlay.
61 var overlayProjection = this.getProjection();
62
63 // Retrieve the south-west and north-east coordinates of this overlay
64 // in LatLngs and convert them to pixel coordinates.
65 // We'll use these coordinates to resize the div.
66
67 var bounds_ = this.map.getBounds();
68 var sw = overlayProjection.fromLatLngToDivPixel(bounds_.getSouthWest());
69 var ne = overlayProjection.fromLatLngToDivPixel(bounds_.getNorthEast());
70
71 // Resize the image's div to fit the indicated dimensions.
72 var div = this.div_;
73 div.style.left = sw.x/2 + 'px';
74 div.style.top = ne.y/2 + 'px';
75 div.style.width = (ne.x - sw.x)/2 + 'px';
76 div.style.height = (sw.y - ne.y)/2 + 'px';
77};
78
79// The onRemove() method will be called automatically from the API if
80// we ever set the overlay's map property to 'null'.
81LabelOverlay.prototype.onRemove = function() {
82 this.div_.parentNode.removeChild(this.div_);
83 this.div_ = null;
84
85};
86
87
88// TODO: Might be useful, but we would need to get the bounds for the shape rather than for the map as is done here
89/*
90// https://aiocollective.com/blog/getbounds-in-google-maps-api-v3/
91google.maps.event.addListener(this.map, 'bounds_changed', function() {
92 var bounds = map.getBounds();
93 var ne = bounds.getNorthEast();
94 var sw = bounds.getSouthWest();
95
96});
97*/
Note: See TracBrowser for help on using the repository browser.