ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
Poi.java
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013, Peter Graf
3 
4  This file is part of Arvos - AR Viewer Open Source for Android.
5  Arvos is free software.
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20  For more information on the AR Viewer Open Source or Peter Graf,
21  please see: http://www.mission-base.com/.
22  */
23 
24 package com.mission_base.arviewer_android.viewer;
25 
26 import android.location.*;
27 import com.mission_base.arviewer_android.*;
28 import com.mission_base.arviewer_android.viewer.opengl.*;
29 import java.util.*;
30 import org.json.*;
31 
38 public class Poi
39 {
40  public long mAnimationDuration;
41 
42  public Float mLongitude = null;
43  public Float mLatitude = null;
44  public String mDeveloperKey;
45 
46  public Augment mParent;
47  public List<PoiObject> mPoiObjects;
48 
49  private List<PoiObject> mObjectsToDeactivate = new LinkedList<PoiObject>();
50  private List<PoiObject> mObjectsToStart = new LinkedList<PoiObject>();
51  private List<PoiObject> mObjectsClicked = new LinkedList<PoiObject>();
52 
53  private Arvos mInstance;
54 
61  public Poi(Augment augment)
62  {
63  mParent = augment;
64  mPoiObjects = new LinkedList<PoiObject>();
65  mInstance = Arvos.getInstance();
66  }
67 
76  public void parse(JSONObject jsonPoi) throws JSONException
77  {
78  mAnimationDuration = jsonPoi.has("animationDuration") ? jsonPoi.getInt("animationDuration") : 0;
79 
80  if (jsonPoi.has("lat"))
81  {
82  mLatitude = (float) jsonPoi.getDouble("lat");
83  }
84  if (jsonPoi.has("lon"))
85  {
86  mLongitude = (float) jsonPoi.getDouble("lon");
87  }
88  mDeveloperKey = jsonPoi.has("developerKey") ? jsonPoi.getString("developerKey") : null;
89 
90  JSONArray jsonPoiObjects = new JSONArray(jsonPoi.getString("poiObjects"));
91  if (jsonPoiObjects == null || jsonPoiObjects.length() == 0)
92  {
93  throw new JSONException("No poiObjects found in poi.");
94  }
95 
96  for (int i = 0; i < jsonPoiObjects.length(); i++)
97  {
98  JSONObject jsonPoiObject = jsonPoiObjects.getJSONObject(i);
99  if (jsonPoiObject != null)
100  {
101  PoiObject poiObject = new PoiObject(this);
102  poiObject.parse(jsonPoiObject);
103  mPoiObjects.add(poiObject);
104  }
105  }
106  return;
107  }
108 
121  public void getObjects(long time, List<ArvosObject> result, List<ArvosObject> arvosObjects)
122  {
123  float deviceLatitude = mInstance.mLatitude;
124  float deviceLongitude = mInstance.mLongitude;
125  float poiLatitude = 0f;
126  float poiLongitude = 0f;
127 
128  float offsetX = 0f;
129  float offsetZ = 0f;
130 
131  if (mLatitude != null)
132  {
133  poiLatitude = mLatitude;
134  Location currentLoction = new Location(LocationManager.GPS_PROVIDER);
135  currentLoction.setLatitude(deviceLatitude);
136 
137  Location poiLocation = new Location(LocationManager.GPS_PROVIDER);
138  poiLocation.setLatitude(poiLatitude);
139 
140  offsetZ = currentLoction.distanceTo(poiLocation);
141  if (deviceLatitude > poiLatitude)
142  {
143  if (offsetZ < 0)
144  {
145  offsetZ = -offsetZ;
146  }
147  }
148  else
149  {
150  if (offsetZ > 0)
151  {
152  offsetZ = -offsetZ;
153  }
154  }
155 
156  }
157  if (mLongitude != null)
158  {
159  poiLongitude = mLongitude;
160  Location currentLoction = new Location(LocationManager.GPS_PROVIDER);
161  currentLoction.setLatitude(deviceLongitude);
162 
163  Location poiLocation = new Location(LocationManager.GPS_PROVIDER);
164  poiLocation.setLatitude(poiLongitude);
165 
166  offsetX = currentLoction.distanceTo(poiLocation);
167  if (deviceLongitude > poiLongitude)
168  {
169  if (offsetX > 0)
170  {
171  offsetX = -offsetX;
172  }
173  }
174  else
175  {
176  if (offsetX < 0)
177  {
178  offsetX = -offsetX;
179  }
180  }
181  }
182 
183  HashSet<String> objectsToDraw = new HashSet<String>();
184  for (PoiObject poiObject : mPoiObjects)
185  {
186  ArvosObject arvosObject = poiObject.getObject(time, arvosObjects);
187  if (arvosObject != null)
188  {
189  arvosObject.mPosition[0] += offsetX;
190  arvosObject.mPosition[2] += offsetZ;
191 
192  objectsToDraw.add(arvosObject.mName);
193  result.add(arvosObject);
194  }
195  }
196 
197  synchronized (mObjectsClicked)
198  {
199  for (PoiObject poiObject : mObjectsClicked)
200  {
201  poiObject.onClick();
202  }
203  mObjectsClicked.clear();
204  }
205 
206  for (PoiObject poiObject : mObjectsToDeactivate)
207  {
208  poiObject.stop();
209  }
210 
211  for (PoiObject poiObject : mObjectsToStart)
212  {
213  poiObject.start(time);
214 
215  if (!objectsToDraw.contains(poiObject.mName))
216  {
217  ArvosObject arvosObject = poiObject.getObject(time, arvosObjects);
218  if (arvosObject != null)
219  {
220  objectsToDraw.add(arvosObject.mName);
221  result.add(arvosObject);
222  }
223  }
224  }
225 
226  mObjectsToDeactivate.clear();
227  mObjectsToStart.clear();
228  }
229 
236  public PoiObject findPoiObject(String name)
237  {
238  for (PoiObject poiObject : mPoiObjects)
239  {
240  if (name.equals(poiObject.mName))
241  {
242  return poiObject;
243  }
244  }
245  for (Poi poi : mParent.mPois)
246  {
247  for (PoiObject poiObject : poi.mPoiObjects)
248  {
249  if (name.equals(poiObject.mName))
250  {
251  return poiObject;
252  }
253  }
254  }
255  return null;
256  }
257 
264  public PoiObject findPoiObject(int id)
265  {
266  for (Poi poi : mParent.mPois)
267  {
268  for (PoiObject poiObject : poi.mPoiObjects)
269  {
270  if (id == poiObject.mId)
271  {
272  return poiObject;
273  }
274  }
275  }
276  return null;
277  }
278 
284  public void requestActivate(PoiObject poiObject)
285  {
286  poiObject.mIsActive = true;
287  requestStart(poiObject);
288  }
289 
295  public void requestStart(PoiObject poiObject)
296  {
297  mObjectsToStart.add(poiObject);
298  }
299 
305  public void requestStop(PoiObject poiObject)
306  {
307  mObjectsToDeactivate.add(poiObject);
308  }
309 
315  public void requestDeactivate(PoiObject poiObject)
316  {
317  poiObject.mIsActive = false;
318  requestStop(poiObject);
319  }
320 
326  public void addClick(PoiObject poiObject)
327  {
328  synchronized (mObjectsClicked)
329  {
330  mObjectsClicked.add(poiObject);
331  }
332  }
333 }