provider.google_static.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * DevExtreme (ui/map/provider.google_static.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var each = require("../../core/utils/iterator").each;
  11. var eventsEngine = require("../../events/core/events_engine");
  12. var Promise = require("../../core/polyfills/promise");
  13. var Provider = require("./provider");
  14. var Color = require("../../color");
  15. var clickEvent = require("../../events/click");
  16. var GOOGLE_STATIC_URL = "https://maps.google.com/maps/api/staticmap?";
  17. var GoogleStaticProvider = Provider.inherit({
  18. _locationToString: function(location) {
  19. var latLng = this._getLatLng(location);
  20. return latLng ? latLng.lat + "," + latLng.lng : location.toString().replace(/ /g, "+")
  21. },
  22. _renderImpl: function() {
  23. return this._updateMap()
  24. },
  25. updateDimensions: function() {
  26. return this._updateMap()
  27. },
  28. updateMapType: function() {
  29. return this._updateMap()
  30. },
  31. updateBounds: function() {
  32. return Promise.resolve()
  33. },
  34. updateCenter: function() {
  35. return this._updateMap()
  36. },
  37. updateZoom: function() {
  38. return this._updateMap()
  39. },
  40. updateControls: function() {
  41. return Promise.resolve()
  42. },
  43. addMarkers: function(options) {
  44. var that = this;
  45. return this._updateMap().then(function(result) {
  46. each(options, function(_, options) {
  47. that._fireMarkerAddedAction({
  48. options: options
  49. })
  50. });
  51. return result
  52. })
  53. },
  54. removeMarkers: function(options) {
  55. var that = this;
  56. return this._updateMap().then(function(result) {
  57. each(options, function(_, options) {
  58. that._fireMarkerRemovedAction({
  59. options: options
  60. })
  61. });
  62. return result
  63. })
  64. },
  65. adjustViewport: function() {
  66. return Promise.resolve()
  67. },
  68. addRoutes: function(options) {
  69. var that = this;
  70. return this._updateMap().then(function(result) {
  71. each(options, function(_, options) {
  72. that._fireRouteAddedAction({
  73. options: options
  74. })
  75. });
  76. return result
  77. })
  78. },
  79. removeRoutes: function(options) {
  80. var that = this;
  81. return this._updateMap().then(function(result) {
  82. each(options, function(_, options) {
  83. that._fireRouteRemovedAction({
  84. options: options
  85. })
  86. });
  87. return result
  88. })
  89. },
  90. clean: function() {
  91. this._$container.css("backgroundImage", "none");
  92. eventsEngine.off(this._$container, this._addEventNamespace(clickEvent.name));
  93. return Promise.resolve()
  94. },
  95. mapRendered: function() {
  96. return true
  97. },
  98. _updateMap: function() {
  99. var key = this._keyOption("googleStatic");
  100. var $container = this._$container;
  101. var requestOptions = ["sensor=false", "size=" + Math.round($container.width()) + "x" + Math.round($container.height()), "maptype=" + this._option("type"), "center=" + this._locationToString(this._option("center")), "zoom=" + this._option("zoom"), this._markersSubstring()];
  102. requestOptions.push.apply(requestOptions, this._routeSubstrings());
  103. if (key) {
  104. requestOptions.push("key=" + key)
  105. }
  106. var request = GOOGLE_STATIC_URL + requestOptions.join("&");
  107. this._$container.css("background", 'url("' + request + '") no-repeat 0 0');
  108. this._attachClickEvent();
  109. return Promise.resolve(true)
  110. },
  111. _markersSubstring: function() {
  112. var that = this;
  113. var markers = [];
  114. var markerIcon = this._option("markerIconSrc");
  115. if (markerIcon) {
  116. markers.push("icon:" + markerIcon)
  117. }
  118. each(this._option("markers"), function(_, marker) {
  119. markers.push(that._locationToString(marker.location))
  120. });
  121. return "markers=" + markers.join("|")
  122. },
  123. _routeSubstrings: function() {
  124. var that = this;
  125. var routes = [];
  126. each(this._option("routes"), function(_, route) {
  127. var color = new Color(route.color || that._defaultRouteColor()).toHex().replace("#", "0x");
  128. var opacity = Math.round(255 * (route.opacity || that._defaultRouteOpacity())).toString(16);
  129. var width = route.weight || that._defaultRouteWeight();
  130. var locations = [];
  131. each(route.locations, function(_, routePoint) {
  132. locations.push(that._locationToString(routePoint))
  133. });
  134. routes.push("path=color:" + color + opacity + "|weight:" + width + "|" + locations.join("|"))
  135. });
  136. return routes
  137. },
  138. _attachClickEvent: function() {
  139. var that = this;
  140. var eventName = this._addEventNamespace(clickEvent.name);
  141. eventsEngine.off(this._$container, eventName);
  142. eventsEngine.on(this._$container, eventName, function(e) {
  143. that._fireClickAction({
  144. event: e
  145. })
  146. })
  147. }
  148. });
  149. module.exports = GoogleStaticProvider;