geohelper.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /********************************************************************************************
  2. ** Copyright © 2011 Nokia Corporation. All rights reserved.
  3. ** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. ** Java and all Java-based marks are trademarks or registered trademarks of
  5. ** Sun Microsystems, Inc. Other product and company names mentioned herein may be
  6. ** trademarks or trade names of their respective owners.
  7. **
  8. ** Subject to the conditions below, you may, without charge:
  9. **
  10. ** · Use, copy, modify and/or merge copies of this software and
  11. ** associated content and documentation files (the “Software”)
  12. **
  13. ** · Publish, distribute, sub-licence and/or sell new software
  14. ** derived from or incorporating the Software.
  15. **
  16. ** Some of the documentation, content and/or software maybe licensed under open source
  17. ** software or other licenses. To the extent such documentation, content and/or
  18. ** software are included, licenses and/or other terms and conditions shall apply
  19. ** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
  20. ** acknowledgements and notices are reproduced in the materials provided.
  21. **
  22. ** This file, unmodified, shall be included with all copies or substantial portions
  23. ** of the Software that are distributed in source code form.
  24. **
  25. ** The Software cannot constitute the primary value of any new software derived
  26. ** from or incorporating the Software.
  27. **
  28. ** Any person dealing with the Software shall not misrepresent the source of the Software.
  29. **
  30. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  31. ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  32. ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  33. ** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. ********************************************************************************************/
  37. #include "geohelper.h"
  38. #include <QScriptEngine>
  39. #include <QScriptValue>
  40. #include <QScriptValueIterator>
  41. #include <QDeclarativeListReference>
  42. #include <QGeoMapRouteObject>
  43. GeoHelper::GeoHelper(QObject *parent) :
  44. QObject(parent)
  45. {
  46. provider = new QGeoServiceProvider("nokia");
  47. mappingManager = provider->mappingManager();
  48. searchManager = provider->searchManager();
  49. routingManager = provider->routingManager();
  50. mapitem = NULL;
  51. QObject::connect(searchManager, SIGNAL(error(QGeoSearchReply *, QGeoSearchReply::Error, QString)), this, SLOT(searchErrorSlot(QGeoSearchReply *, QGeoSearchReply::Error, QString)));
  52. QObject::connect(searchManager, SIGNAL(finished(QGeoSearchReply*)), this, SLOT(searchFinishedSlot(QGeoSearchReply*)));
  53. QObject::connect(routingManager, SIGNAL(error(QGeoRouteReply*, QGeoRouteReply::Error, QString)), this, SLOT(routingErrorSlot(QGeoRouteReply*, QGeoRouteReply::Error, QString)));
  54. QObject::connect(routingManager, SIGNAL(finished(QGeoRouteReply*)), this, SLOT(routingFinishedSlot(QGeoRouteReply*)));
  55. }
  56. GeoHelper::~GeoHelper()
  57. {
  58. clearMap();
  59. if (provider)
  60. {
  61. delete provider;
  62. provider = NULL;
  63. }
  64. }
  65. void GeoHelper::searchErrorSlot(QGeoSearchReply *reply, QGeoSearchReply::Error error, QString errorString)
  66. {
  67. emit searchError(errorString);
  68. }
  69. void GeoHelper::searchFinishedSlot(QGeoSearchReply *reply)
  70. {
  71. if (reply->error() == QGeoSearchReply::NoError)
  72. {
  73. QScriptEngine scriptEngine;
  74. QScriptValue replyObject = scriptEngine.newArray();
  75. QList<QGeoPlace> places = reply->places();
  76. for (int i = 0; i < places.count(); i++)
  77. {
  78. QScriptValue placeObject = scriptEngine.newObject();
  79. QScriptValue coordinateObject = scriptEngine.newObject();
  80. QGeoCoordinate coordinate = places[i].coordinate();
  81. coordinateObject.setProperty("latitude", QScriptValue(coordinate.latitude()));
  82. coordinateObject.setProperty("longitude", QScriptValue(coordinate.longitude()));
  83. placeObject.setProperty("coordinate", coordinateObject);
  84. QScriptValue addressObject = scriptEngine.newObject();
  85. QGeoAddress address = places[i].address();
  86. if (!address.isEmpty())
  87. {
  88. addressObject.setProperty("country", address.country());
  89. addressObject.setProperty("countryCode", address.countryCode());
  90. addressObject.setProperty("state", address.state());
  91. addressObject.setProperty("county", address.county());
  92. addressObject.setProperty("city", address.city());
  93. addressObject.setProperty("district", address.district());
  94. addressObject.setProperty("street", address.street());
  95. addressObject.setProperty("postcode", address.postcode());
  96. }
  97. placeObject.setProperty("address", addressObject);
  98. replyObject.setProperty(i, placeObject);
  99. }
  100. QScriptValue fun = scriptEngine.evaluate("(function(a) { return JSON.stringify(a); })");
  101. QScriptValueList args;
  102. args << replyObject;
  103. QScriptValue result = fun.call(QScriptValue(), args);
  104. emit searchReply(result.toString());
  105. }
  106. }
  107. void GeoHelper::routingErrorSlot(QGeoRouteReply *reply, QGeoRouteReply::Error error, QString errorString)
  108. {
  109. emit routingError(errorString);
  110. }
  111. void GeoHelper::routingFinishedSlot(QGeoRouteReply * reply)
  112. {
  113. if (reply->error() == QGeoRouteReply::NoError)
  114. {
  115. QScriptEngine scriptEngine;
  116. QScriptValue replyObject = scriptEngine.newArray();
  117. QList<QGeoCoordinate> waypoints = reply->request().waypoints();
  118. double lat1 = 0;
  119. double lon1 = 0;
  120. double lat2 = 0;
  121. double lon2 = 0;
  122. if (waypoints.count() > 0)
  123. {
  124. /*
  125. QString msg = QString("lat %1, lon %2 => lat %3, lon %4").
  126. arg(waypoints.at(0).latitude()).arg(waypoints.at(0).longitude()).
  127. arg(waypoints.at((waypoints.count()-1)).latitude()).arg(waypoints.at((waypoints.count()-1)).longitude());
  128. emit routingError(msg);
  129. */
  130. lat1 = waypoints.at(0).latitude();
  131. lon1 = waypoints.at(0).longitude();
  132. lat2 = waypoints.at((waypoints.count()-1)).latitude();
  133. lon2 = waypoints.at((waypoints.count()-1)).longitude();
  134. }
  135. for (int i = 0; i < reply->routes().size(); ++i)
  136. {
  137. QScriptValue routeObject = scriptEngine.newObject();
  138. QGeoRoute route = reply->routes().at(i);
  139. routeObject.setProperty("distance", QScriptValue(route.distance()));
  140. routeObject.setProperty("travelTime", QScriptValue(route.travelTime()));
  141. routeObject.setProperty("lat1", QScriptValue(lat1));
  142. routeObject.setProperty("lon1", QScriptValue(lon1));
  143. routeObject.setProperty("lat2", QScriptValue(lat2));
  144. routeObject.setProperty("lon2", QScriptValue(lon2));
  145. QScriptValue pathObject = scriptEngine.newArray();
  146. QList<QGeoCoordinate> path = route.path();
  147. for (int p = 0; p < path.length(); p++)
  148. {
  149. QScriptValue coordinateObject = scriptEngine.newObject();
  150. coordinateObject.setProperty("latitude", QScriptValue(path[p].latitude()));
  151. coordinateObject.setProperty("longitude", QScriptValue(path[p].longitude()));
  152. pathObject.setProperty(p, coordinateObject);
  153. }
  154. routeObject.setProperty("path", pathObject);
  155. replyObject.setProperty(i, routeObject);
  156. }
  157. QScriptValue fun = scriptEngine.evaluate("(function(a) { return JSON.stringify(a); })");
  158. QScriptValueList args;
  159. args << replyObject;
  160. QScriptValue result = fun.call(QScriptValue(), args);
  161. emit routingReply(result.toString());
  162. }
  163. }
  164. // ------------- Q_INVOKABLE METHODS -------
  165. void GeoHelper::removeFromMap(QString id)
  166. {
  167. if (mapobjects.contains(id))
  168. {
  169. QGeoMapObject *obj = mapobjects.take(id);
  170. if (obj != NULL)
  171. {
  172. delete obj;
  173. obj = NULL;
  174. }
  175. // Now we have to construct the map's objects list again
  176. // this is because the objects list does not have a method
  177. // to remove a single object.
  178. for (int i = 0; i < listRef.count(); i++)
  179. listRef.at(i)->deleteLater();
  180. listRef.clear();
  181. QStringList keys = mapobjects.keys();
  182. foreach (QString id, keys)
  183. {
  184. emit debugMsg("lisaa uudestaan " + id);
  185. QGeoMapObject *obj = mapobjects.value(id);
  186. if (obj != NULL)
  187. {
  188. if (obj->type() == QGeoMapObject::PolylineType)
  189. {
  190. QGeoMapPolylineObject *newobj = new QGeoMapPolylineObject;
  191. newobj->setPath(((QGeoMapPolylineObject *)obj)->path());
  192. newobj->setPen(QPen(QBrush(Qt::blue), 4));
  193. newobj->setObjectName(obj->objectName());
  194. listRef.append(newobj);
  195. }
  196. else if (obj->type() == QGeoMapObject::PixmapType)
  197. {
  198. QGeoMapPixmapObject *newobj = new QGeoMapPixmapObject;
  199. newobj->setCoordinate(((QGeoMapPixmapObject *)obj)->coordinate());
  200. newobj->setPixmap(((QGeoMapPixmapObject *)obj)->pixmap());
  201. newobj->setOffset(QPoint(-10,-34));
  202. newobj->setObjectName(obj->objectName());
  203. listRef.append(newobj);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. void GeoHelper::clearMap()
  210. {
  211. QStringList keys = mapobjects.keys();
  212. foreach (QString id, keys)
  213. {
  214. QGeoMapObject *obj = mapobjects.take(id);
  215. if (obj != NULL)
  216. {
  217. delete obj;
  218. obj = NULL;
  219. }
  220. }
  221. mapobjects.clear();
  222. for (int i = 0; i < listRef.count(); i++)
  223. listRef.at(i)->deleteLater();
  224. listRef.clear();
  225. }
  226. void GeoHelper::drawPolyline(QString id, QString coordinateArr)
  227. {
  228. /*
  229. [
  230. {"latitude":61.4735985,"longitude":23.7550697},
  231. {"latitude":61.4735985,"longitude":23.7550697}
  232. ]
  233. */
  234. if (mapitem != NULL)
  235. {
  236. removeFromMap(id);
  237. QScriptValue sc;
  238. QScriptEngine engine;
  239. sc = engine.evaluate("(" + QString(coordinateArr) + ")");
  240. if (sc.isArray())
  241. {
  242. QScriptValueIterator it(sc);
  243. QList<QGeoCoordinate> coordinates;
  244. while (it.hasNext())
  245. {
  246. it.next();
  247. if (it.value().property("latitude").toString() != "" && it.value().property("longitude").toString() != "")
  248. {
  249. coordinates << QGeoCoordinate(it.value().property("latitude").toNumber(),it.value().property("longitude").toNumber());
  250. }
  251. }
  252. QGeoMapPolylineObject *obj = new QGeoMapPolylineObject;
  253. obj->setPath(coordinates);
  254. obj->setPen(QPen(QBrush(Qt::blue), 4));
  255. obj->setObjectName(id);
  256. listRef.append(obj);
  257. // keep a copy to construct the map objects list again
  258. QGeoMapPolylineObject *copyobj = new QGeoMapPolylineObject;
  259. copyobj->setPath(coordinates);
  260. copyobj->setPen(QPen(QBrush(Qt::blue), 4));
  261. copyobj->setObjectName(id);
  262. mapobjects.insert(id,copyobj);
  263. }
  264. }
  265. }
  266. void GeoHelper::drawImage(QString id, double latitude, double longitude, QString imagepath, int xOffset, int yOffset)
  267. {
  268. if (mapitem != NULL)
  269. {
  270. //emit debugMsg(QString("offset: %1, %2").arg(xOffset).arg(yOffset));
  271. removeFromMap(id);
  272. QGeoMapPixmapObject *obj = new QGeoMapPixmapObject;
  273. obj->setCoordinate(QGeoCoordinate(latitude,longitude));
  274. obj->setPixmap(imagepath);
  275. obj->setOffset(QPoint(xOffset,yOffset));
  276. //obj->setOffset(QPoint(-10,-34));
  277. obj->setObjectName(id);
  278. listRef.append(obj);
  279. // keep a copy to construct the map objects list again
  280. QGeoMapPixmapObject *copyobj = new QGeoMapPixmapObject;
  281. copyobj->setCoordinate(QGeoCoordinate(latitude,longitude));
  282. copyobj->setPixmap(imagepath);
  283. copyobj->setOffset(QPoint(xOffset,yOffset));
  284. //copyobj->setOffset(QPoint(-10,-34));
  285. copyobj->setObjectName(id);
  286. mapobjects.insert(id,copyobj);
  287. }
  288. }
  289. QBrush usebrush(Qt::darkRed);
  290. QPen usepen(usebrush,1);
  291. QFont usefont("Terminal", 12);
  292. void GeoHelper::drawText(QString id, double latitude, double longitude, QString text)
  293. {
  294. if (mapitem != NULL)
  295. {
  296. //emit debugMsg(QString("offset: %1, %2").arg(xOffset).arg(yOffset));
  297. removeFromMap(id);
  298. QGeoMapTextObject *obj = new QGeoMapTextObject;
  299. obj->setCoordinate(QGeoCoordinate(latitude,longitude));
  300. obj->setText(text);
  301. obj->setFont(usefont);
  302. obj->setOffset(QPoint(20,-10));
  303. obj->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
  304. obj->setBrush(usebrush);
  305. obj->setPen(usepen);
  306. obj->setObjectName(id);
  307. listRef.append(obj);
  308. // keep a copy to construct the map objects list again
  309. QGeoMapTextObject *copyobj = new QGeoMapTextObject;
  310. copyobj->setCoordinate(QGeoCoordinate(latitude,longitude));
  311. copyobj->setText(text);
  312. copyobj->setFont(usefont);
  313. copyobj->setOffset(QPoint(20,-10));
  314. copyobj->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
  315. copyobj->setBrush(usebrush);
  316. copyobj->setPen(usepen);
  317. copyobj->setObjectName(id);
  318. mapobjects.insert(id,copyobj);
  319. }
  320. }
  321. void GeoHelper::findObjectsInCoordinates(double latitude, double longitude)
  322. {
  323. QGeoCoordinate coord(latitude, longitude);
  324. for (int i = 0; i < listRef.count(); i++)
  325. {
  326. if ( ((QGeoMapObject *)listRef.at(i))->contains(coord))
  327. {
  328. emit geomapobjectSelected(listRef.at(i)->objectName(), true);
  329. }
  330. }
  331. }
  332. void GeoHelper::findRoute(double fromLatitude, double fromLongitude, double toLatitude, double toLongitude)
  333. {
  334. QGeoRouteRequest *geoRouteRequest = new QGeoRouteRequest(QGeoCoordinate(fromLatitude, fromLongitude), QGeoCoordinate(toLatitude, toLongitude));
  335. routingManager->calculateRoute(*geoRouteRequest);
  336. }
  337. void GeoHelper::findAddress(double latitude, double longitude)
  338. {
  339. QGeoCoordinate location(latitude,longitude);
  340. searchManager->reverseGeocode(location);
  341. }
  342. void GeoHelper::findCoordinates(QString street, QString city, QString country)
  343. {
  344. /*
  345. NOTE! This geocode method is not capable to handle the number of the street address.
  346. But the use of the plain search alternative seems to work.
  347. QGeoAddress address;
  348. address.setStreet(street);
  349. address.setCity(city);
  350. address.setCountry(country);
  351. searchManager->geocode(address);
  352. */
  353. QString str = QString("%1,%2,%3").arg(street).arg(city).arg(country);
  354. searchManager->search(str, QGeoSearchManager::SearchGeocode);
  355. }