• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • plugins
  • render
  • foursquare
FoursquareModel.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2012 Utku Aydın <utkuaydin34@gmail.com>
9 //
10 
11 #include "FoursquareModel.h"
12 #include "FoursquarePlugin.h"
13 #include "FoursquareItem.h"
14 
15 #include "MarbleGlobal.h"
16 #include "MarbleModel.h"
17 #include "GeoDataCoordinates.h"
18 #include "GeoDataLatLonAltBox.h"
19 #include "MarbleDebug.h"
20 #include "MarbleMath.h"
21 
22 #include <QUrl>
23 #include <QScriptEngine>
24 #include <QScriptValue>
25 #include <QScriptValueIterator>
26 
27 namespace Marble
28 {
29 
30 FoursquareModel::FoursquareModel(const MarbleModel *marbleModel, QObject* parent)
31  : AbstractDataPluginModel("foursquare", marbleModel, parent)
32 {
33  // Enjoy laziness
34 }
35 
36 FoursquareModel::~FoursquareModel()
37 {
38 }
39 
40 void FoursquareModel::getAdditionalItems( const GeoDataLatLonAltBox& box, qint32 number )
41 {
42  if( marbleModel()->planetId() != "earth" ) {
43  return;
44  }
45 
46  QString clientId = "YPRWSYFW1RVL4PJQ2XS5G14RTOGTHOKZVHC1EP5KCCCYQPZF";
47  QString clientSecret = "5L2JDCAYQCEJWY5FNDU4A1RWATE4E5FIIXXRM41YBTFSERUH";
48 
49  QString apiUrl( "https://api.foursquare.com/v2/venues/search" );
50  qreal const distanceLon = marbleModel()->planetRadius() * distanceSphere( box.west(), box.north(), box.east(), box.north() );
51  qreal const distanceLat = marbleModel()->planetRadius() * distanceSphere( box.west(), box.north(), box.west(), box.south() );
52  qreal const area = distanceLon * distanceLat;
53  if ( area > 10 * 1000 * KM2METER * KM2METER ) {
54  // Large area (> 10.000 km^2) => too large for bbox queries
55  apiUrl += "?ll=" + QString::number( box.center().latitude(Marble::GeoDataCoordinates::Degree) );
56  apiUrl += ',' + QString::number( box.center().longitude(Marble::GeoDataCoordinates::Degree) );
57  apiUrl += "&intent=checkin";
58  } else {
59  apiUrl += "?ne=" + QString::number( box.north(Marble::GeoDataCoordinates::Degree) );
60  apiUrl += ',' + QString::number( box.east(Marble::GeoDataCoordinates::Degree) );
61  apiUrl += "&sw=" + QString::number( box.south(Marble::GeoDataCoordinates::Degree) );
62  apiUrl += ',' + QString::number( box.west(Marble::GeoDataCoordinates::Degree) );
63  apiUrl += "&intent=browse";
64  }
65  apiUrl += "&limit=" + QString::number( number );
66  apiUrl += "&client_id=" + clientId;
67  apiUrl += "&client_secret=" + clientSecret;
68  apiUrl += "&v=20120601";
69  downloadDescriptionFile( QUrl( apiUrl ) );
70 }
71 
72 void FoursquareModel::parseFile( const QByteArray& file )
73 {
74  QScriptValue data;
75  QScriptEngine engine;
76  // Qt requires parentheses around JSON
77  data = engine.evaluate( '(' + QString::fromUtf8( file ) + ')' );
78  data = data.property("response");
79 
80  // Parse if any result exists
81  if ( data.property( "venues" ).isArray() ) {
82  QScriptValueIterator iterator( data.property( "venues" ) );
83  // Add items to the list
84  QList<AbstractDataPluginItem*> items;
85  do {
86  iterator.next();
87  QString id = iterator.value().property( "id" ).toString();
88  QString name = iterator.value().property( "name" ).toString();
89  QString category = iterator.value().property( "categories" ).property( 0 ).property( "name" ).toString();
90  QString address = iterator.value().property( "location" ).property( "address" ).toString();
91  QString city = iterator.value().property( "location" ).property( "city" ).toString();
92  QString country = iterator.value().property( "location" ).property( "country" ).toString();
93  double latitude = iterator.value().property( "location" ).property( "lat" ).toString().toDouble();
94  double longitude = iterator.value().property( "location" ).property( "lng" ).toString().toDouble();
95  int usersCount = iterator.value().property( "stats" ).property( "usersCount" ).toInteger();
96 
97  QScriptValue categoryIcon = iterator.value().property( "categories" ).property( 0 ).property( "icon" );
98  QString iconUrl;
99  QString largeIconUrl;
100  if ( categoryIcon.isValid() ) {
101  iconUrl = categoryIcon.property( "prefix" ).toString()
102  + "32" // That's the icon size hardcoded
103  + categoryIcon.property( "name" ).toString();
104 
105  largeIconUrl = categoryIcon.property( "prefix" ).toString()
106  + "64" // Larger icon
107  + categoryIcon.property( "name" ).toString();
108  }
109 
110  if( !itemExists( id ) ) {
111  GeoDataCoordinates coordinates( longitude, latitude, 0.0, GeoDataCoordinates::Degree );
112  FoursquareItem *item = new FoursquareItem( this );
113  item->setId( id );
114  item->setCoordinate( coordinates );
115  item->setTarget( "earth" );
116  item->setName( name );
117  item->setCategory( category );
118  item->setAddress( address );
119  item->setCity( city );
120  item->setCountry( country );
121  item->setUsersCount( usersCount );
122  item->setCategoryIconUrl( iconUrl );
123  item->setCategoryLargeIconUrl( largeIconUrl );
124 
125  items << item;
126  }
127  }
128  while ( iterator.hasNext() );
129  addItemsToList( items );
130  }
131 }
132 
133 }
134 
135 #include "FoursquareModel.moc"
GeoDataCoordinates.h
Marble::AbstractDataPluginModel::items
QList< AbstractDataPluginItem * > items(const ViewportParams *viewport, qint32 number=10)
Get the items on the viewport Returns the currently downloaded images in the viewport.
Definition: AbstractDataPluginModel.cpp:274
Marble::AbstractDataPluginItem::setTarget
void setTarget(const QString &target)
Definition: AbstractDataPluginItem.cpp:66
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::FoursquareItem
Definition: FoursquareItem.h:20
Marble::FoursquareItem::setCategory
void setCategory(const QString &category)
Definition: FoursquareItem.cpp:71
FoursquareModel.h
MarbleMath.h
Marble::AbstractDataPluginModel::itemExists
bool itemExists(const QString &id) const
Testing the existence of the item id in the list.
Definition: AbstractDataPluginModel.cpp:563
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::KM2METER
const qreal KM2METER
Definition: MarbleGlobal.h:204
Marble::AbstractDataPluginItem::setId
void setId(const QString &id)
Definition: AbstractDataPluginItem.cpp:86
FoursquarePlugin.h
Marble::AbstractDataPluginModel
An abstract data model (not based on QAbstractModel) for a AbstractDataPlugin.
Definition: AbstractDataPluginModel.h:45
Marble::AbstractDataPluginModel::marbleModel
const MarbleModel * marbleModel() const
Definition: AbstractDataPluginModel.cpp:269
Marble::FoursquareItem::setCategoryLargeIconUrl
void setCategoryLargeIconUrl(const QString &url)
Definition: FoursquareItem.cpp:148
Marble::FoursquareModel::parseFile
void parseFile(const QByteArray &file)
Parses the file which getAdditionalItems downloads and prepares the data for usage.
Definition: FoursquareModel.cpp:72
Marble::distanceSphere
qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method calculates the shortest distance between two points on a sphere.
Definition: MarbleMath.h:52
Marble::FoursquareModel::FoursquareModel
FoursquareModel(const MarbleModel *marbleModel, QObject *parent=0)
Definition: FoursquareModel.cpp:30
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
QObject
MarbleDebug.h
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::FoursquareItem::setName
void setName(const QString &name)
Definition: FoursquareItem.cpp:56
Marble::GeoDataLatLonBox::north
qreal north(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the northern boundary of the bounding box.
Definition: GeoDataLatLonBox.cpp:93
Marble::AbstractDataPluginModel::downloadDescriptionFile
void downloadDescriptionFile(const QUrl &url)
Download the description file from the url.
Definition: AbstractDataPluginModel.cpp:392
Marble::GeoDataLatLonBox::east
qreal east(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the eastern boundary of the bounding box.
Definition: GeoDataLatLonBox.cpp:135
MarbleGlobal.h
Marble::FoursquareItem::setAddress
void setAddress(const QString &address)
Definition: FoursquareItem.cpp:84
Marble::FoursquareItem::setUsersCount
void setUsersCount(const int count)
Definition: FoursquareItem.cpp:122
Marble::FoursquareModel::getAdditionalItems
virtual void getAdditionalItems(const GeoDataLatLonAltBox &box, qint32 number=10)
Generates the download url for the description file from the web service depending on the box surroun...
Definition: FoursquareModel.cpp:40
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
Marble::GeoDataLatLonAltBox::center
virtual GeoDataCoordinates center() const
returns the center of this box
Definition: GeoDataLatLonAltBox.cpp:152
Marble::FoursquareItem::setCountry
void setCountry(const QString &country)
Definition: FoursquareItem.cpp:109
Marble::FoursquareItem::setCity
void setCity(const QString &city)
Definition: FoursquareItem.cpp:96
Marble::GeoDataLatLonBox::west
qreal west(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the western boundary of the bounding box.
Definition: GeoDataLatLonBox.cpp:156
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:96
Marble::FoursquareModel::~FoursquareModel
~FoursquareModel()
Definition: FoursquareModel.cpp:36
GeoDataLatLonAltBox.h
FoursquareItem.h
Marble::GeoDataLatLonBox::south
qreal south(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the southern boundary of the bounding box.
Definition: GeoDataLatLonBox.cpp:114
Marble::MarbleModel::planetRadius
qreal planetRadius() const
Definition: MarbleModel.cpp:452
Marble::FoursquareItem::setCategoryIconUrl
void setCategoryIconUrl(const QString &url)
Definition: FoursquareItem.cpp:135
Marble::BillboardGraphicsItem::setCoordinate
void setCoordinate(const GeoDataCoordinates &coordinates)
Definition: BillboardGraphicsItem.cpp:98
Marble::GeoDataLatLonAltBox
A class that defines a 3D bounding box for geographic data.
Definition: GeoDataLatLonAltBox.h:49
Marble::AbstractDataPluginModel::addItemsToList
void addItemsToList(const QList< AbstractDataPluginItem * > &items)
Adds the items to the list of initialized items.
Definition: AbstractDataPluginModel.cpp:408
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal