Custom controls - Google map


SUBMITTED BY: jlolk3r

DATE: Jan. 20, 2016, 9:55 a.m.

FORMAT: HTML

SIZE: 2.4 kB

HITS: 1563

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  5. <meta charset="utf-8">
  6. <title>Custom controls</title>
  7. <style>
  8. html, body {
  9. height: 100%;
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #map {
  14. height: 100%;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="map"></div>
  20. <script>
  21. var map;
  22. var chicago = {lat: 41.85, lng: -87.65};
  23. /**
  24. * The CenterControl adds a control to the map that recenters the map on Chicago.
  25. * This constructor takes the control DIV as an argument.
  26. * @constructor
  27. */
  28. function CenterControl(controlDiv, map) {
  29. // Set CSS for the control border.
  30. var controlUI = document.createElement('div');
  31. controlUI.style.backgroundColor = '#fff';
  32. controlUI.style.border = '2px solid #fff';
  33. controlUI.style.borderRadius = '3px';
  34. controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  35. controlUI.style.cursor = 'pointer';
  36. controlUI.style.marginBottom = '22px';
  37. controlUI.style.textAlign = 'center';
  38. controlUI.title = 'Click to recenter the map';
  39. controlDiv.appendChild(controlUI);
  40. // Set CSS for the control interior.
  41. var controlText = document.createElement('div');
  42. controlText.style.color = 'rgb(25,25,25)';
  43. controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
  44. controlText.style.fontSize = '16px';
  45. controlText.style.lineHeight = '38px';
  46. controlText.style.paddingLeft = '5px';
  47. controlText.style.paddingRight = '5px';
  48. controlText.innerHTML = 'Center Map';
  49. controlUI.appendChild(controlText);
  50. // Setup the click event listeners: simply set the map to Chicago.
  51. controlUI.addEventListener('click', function() {
  52. map.setCenter(chicago);
  53. });
  54. }
  55. function initMap() {
  56. map = new google.maps.Map(document.getElementById('map'), {
  57. zoom: 12,
  58. center: chicago
  59. });
  60. // Create the DIV to hold the control and call the CenterControl() constructor
  61. // passing in this DIV.
  62. var centerControlDiv = document.createElement('div');
  63. var centerControl = new CenterControl(centerControlDiv, map);
  64. centerControlDiv.index = 1;
  65. map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
  66. }
  67. </script>
  68. <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap" async defer>
  69. </script>
  70. </body>
  71. </html>

comments powered by Disqus