You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. *
  3. * This file is part of QMapControl,
  4. * an open-source cross-platform map widget
  5. *
  6. * Copyright (C) 2007 - 2008 Kai Winter
  7. * Copyright (C) 2014 Mattes Jaehne
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * original software by Kai Winter
  23. * Contact e-mail: kaiwinter@gmx.de
  24. * Program URL : http://qmapcontrol.sourceforge.net/
  25. *
  26. * modified to use Google Static Maps API V2 by
  27. * Mattes Jaehne <mattes@dev.uavp.ch>
  28. * for
  29. * NGOS - The Next Generation multicopter OS
  30. * http://ng.uavp.ch
  31. *
  32. */
  33. #include "bingapimapadapter.h"
  34. #define _USE_MATH_DEFINES
  35. #ifndef M_E
  36. #include <math.h>
  37. #endif
  38. bingApiMapadapter::bingApiMapadapter(QString mapType, QString apiKey)
  39. : TileMapAdapter("dev.virtualearth.net", "/REST/v1/Imagery/Map/", 256, 0, 21),
  40. myKey(apiKey),
  41. myMapType(mapType)
  42. {
  43. mNumberOfTiles = pow(2., mCurrent_zoom + 0.0);
  44. coord_per_x_tile = 360. / mNumberOfTiles;
  45. coord_per_y_tile = 180. / mNumberOfTiles;
  46. if (! myKey.isEmpty())
  47. myKey.prepend("&key=");
  48. if (myMapType.isEmpty())
  49. myMapType.append("Road");
  50. }
  51. bingApiMapadapter::~bingApiMapadapter()
  52. {
  53. }
  54. QPoint bingApiMapadapter::coordinateToDisplay(const QPointF& coordinate) const
  55. {
  56. qreal x = (coordinate.x() + 180.) * (mNumberOfTiles * mTileSize) / 360.; // coord to pixel!
  57. qreal y = (1. - log(tan(coordinate.y() * M_PI / 180.) + 1. / cos(coordinate.y() * M_PI / 180.)) / M_PI) / 2. * (mNumberOfTiles*mTileSize);
  58. x += mTileSize / 2;
  59. y += mTileSize / 2;
  60. return QPoint(int(x), int(y));
  61. }
  62. QPointF bingApiMapadapter::displayToCoordinate(const QPoint& point) const
  63. {
  64. qreal lon = (point.x() - mTileSize / 2) / (mNumberOfTiles * mTileSize) * 360. - 180.;
  65. qreal lat = M_PI - 2. * M_PI * (point.y() - mTileSize / 2) / (mNumberOfTiles * mTileSize);
  66. lat = 180. / M_PI * atan(0.5 * (exp(lat) - exp(-lat)));
  67. return QPointF(lon, lat);
  68. }
  69. qreal bingApiMapadapter::getMercatorLatitude(qreal YCoord) const
  70. {
  71. if (YCoord > M_PI) return 9999.;
  72. if (YCoord < -M_PI) return -9999.;
  73. qreal t = atan(exp(YCoord));
  74. qreal res = (2. * t) - (M_PI / 2.);
  75. return res;
  76. }
  77. qreal bingApiMapadapter::getMercatorYCoord(qreal lati) const
  78. {
  79. qreal phi = M_PI * lati / 180.;
  80. qreal res = 0.5 * log((1. + sin(phi)) / (1. - sin(phi)));
  81. return res;
  82. }
  83. void bingApiMapadapter::zoom_in()
  84. {
  85. if (mCurrent_zoom >= maxZoom())
  86. return;
  87. mCurrent_zoom += 1;
  88. mNumberOfTiles = pow(2, mCurrent_zoom + 0.0);
  89. coord_per_x_tile = 360. / mNumberOfTiles;
  90. coord_per_y_tile = 180. / mNumberOfTiles;
  91. }
  92. void bingApiMapadapter::zoom_out()
  93. {
  94. if (mCurrent_zoom <= minZoom())
  95. return;
  96. mCurrent_zoom -= 1;
  97. mNumberOfTiles = pow(2, mCurrent_zoom + 0.0);
  98. coord_per_x_tile = 360. / mNumberOfTiles;
  99. coord_per_y_tile = 180. / mNumberOfTiles;
  100. }
  101. QString bingApiMapadapter::getQ(qreal longitude, qreal latitude, int zoom) const
  102. {
  103. QString location = "/REST/v1/Imagery/Map/";
  104. if (! myMapType.isEmpty())
  105. location.append(myMapType + "/");
  106. else
  107. location.append("Road/");
  108. location.append(QVariant(latitude).toString() + ",");
  109. location.append(QVariant(longitude).toString() + "/");
  110. location.append(QString::number(zoom) + "?");
  111. location.append("&mapSize=" + QString::number(mTileSize) + "," + QString::number(mTileSize));
  112. if (! myKey.isEmpty())
  113. location.append(myKey);
  114. else
  115. fprintf(stderr, "You are useing Bing Maps API without a (valid) key. This is not possible...\r\n");
  116. return location;
  117. }
  118. void bingApiMapadapter::setKey(QString apiKey)
  119. {
  120. if (apiKey.isEmpty())
  121. return;
  122. myKey.clear();
  123. myKey.append("&key=" + apiKey);
  124. }
  125. void bingApiMapadapter::setMapType(QString mapType) /* Aerial, AerialWithLabels, Road */
  126. {
  127. if (mapType.isEmpty())
  128. return;
  129. myMapType.clear();
  130. myMapType.append(mapType);
  131. }