Advertisement
Guest User

Untitled

a guest
May 28th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. package pl.wroc.pwr.indoorlocalizationtieto.UI;
  2.  
  3. import android.content.Context;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.location.LocationManager;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.support.v4.app.Fragment;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.view.ViewTreeObserver;
  15. import android.widget.ImageButton;
  16. import android.widget.Toast;
  17.  
  18. import java.util.ArrayList;
  19.  
  20. import pl.wroc.pwr.indoorlocalizationtieto.Parser.JsonLoadedListener;
  21. import pl.wroc.pwr.indoorlocalizationtieto.Parser.OSMData;
  22. import pl.wroc.pwr.indoorlocalizationtieto.Parser.OSMDataParser;
  23. import pl.wroc.pwr.indoorlocalizationtieto.Parser.OverpassDataFetcher;
  24. import pl.wroc.pwr.indoorlocalizationtieto.R;
  25. import pl.wroc.pwr.indoorlocalizationtieto.localization.Localization;
  26. import pl.wroc.pwr.indoorlocalizationtieto.map.Map;
  27. import pl.wroc.pwr.indoorlocalizationtieto.map.MapObject;
  28. import pl.wroc.pwr.indoorlocalizationtieto.renderer.GeometryRenderer;
  29. import pl.wroc.pwr.indoorlocalizationtieto.renderer.MapObjectPointCalculator;
  30. import pl.wroc.pwr.indoorlocalizationtieto.renderer.MapView;
  31. import pl.wroc.pwr.indoorlocalizationtieto.renderer.PositionUpdateListener;
  32. import pl.wroc.pwr.indoorlocalizationtieto.renderer.PositionUpdateRunnable;
  33.  
  34. public class MapFragment extends Fragment implements View.OnClickListener, JsonLoadedListener, PositionUpdateListener, LocationListener {
  35. // public static final String LATITUDE = "51.09408";
  36. // public static final String LONGITUDE = "17.018144";
  37. public static final String LATITUDE = "51.10897";
  38. public static final String LONGITUDE = "17.06019";
  39. public static final String RADIUS = "100";
  40. private static final float LATIT[] = new float[]{
  41. 51.10894f, 51.10896f, 51.10898f, 51.10900f, 51.10902f
  42. };
  43. private static final float LONGIT[] = new float[]{
  44. 17.06038f, 17.06034f, 17.06028f, 17.06023f, 17.06019f
  45. };
  46.  
  47. private static final float LATIT_IND[] = new float[]{
  48. 51.10891f, 51.10890f, 51.10886f, 51.10886f, 51.10885f, 51.10887f
  49. };
  50. private static final float LONGIT_IND[] = new float[]{
  51. 17.06038f, 17.06036f, 17.06047f, 17.06035f, 17.06030f, 17.06022f
  52. };
  53.  
  54. private static int counter = 0;
  55. Map map;
  56. Localization localization;
  57. LocationManager locationManager;
  58. private int mapLevel = 0;
  59. private MapView mapView;
  60. private GeometryRenderer renderer;
  61. private ImageButton butPlus;
  62. private ImageButton butMinus;
  63. private ImageButton butUp;
  64. private ImageButton butDown;
  65. private Location location;
  66.  
  67. public MapFragment() {
  68. }
  69.  
  70.  
  71. public MapFragment(Context context) {
  72. localization = new Localization(context);
  73. locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  74.  
  75. createMap();
  76. renderer = new GeometryRenderer(new ArrayList<MapObject>(), context);
  77. renderer.setStyle(R.raw.mapjsonzoom);
  78.  
  79. }
  80.  
  81. @Override
  82. public void onResume() {
  83. super.onResume();
  84. localization.registerReceiver();
  85. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, this);
  86.  
  87. }
  88.  
  89. @Override
  90. public void onPause() {
  91. super.onPause();
  92. localization.unregisterReceiver();
  93. locationManager.removeUpdates(this);
  94. }
  95.  
  96.  
  97. @Override
  98. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  99. View rootView = inflater.inflate(R.layout.map_layout, container, false);
  100.  
  101. mapView = (MapView) rootView.findViewById(R.id.view);
  102. butPlus = (ImageButton) rootView.findViewById(R.id.butPlus);
  103. butMinus = (ImageButton) rootView.findViewById(R.id.butMinus);
  104. butUp = (ImageButton) rootView.findViewById(R.id.butUp);
  105. butDown = (ImageButton) rootView.findViewById(R.id.butDown);
  106. butPlus.setOnClickListener(this);
  107. butMinus.setOnClickListener(this);
  108. butUp.setOnClickListener(this);
  109. butDown.setOnClickListener(this);
  110.  
  111. mapView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  112. @Override
  113. public void onGlobalLayout() {
  114. // MapObjectPointCalculator positionCalculator = new MapObjectPointCalculator((float) location.getLatitude(),
  115. // (float) location.getLongitude(), mapView.getHeight(), mapView.getWidth(), Float.valueOf(RADIUS)/**10*/);
  116. MapObjectPointCalculator positionCalculator = new MapObjectPointCalculator(Float.valueOf(LATITUDE),
  117. Float.valueOf(LONGITUDE), mapView.getHeight(), mapView.getWidth(), Float.valueOf(RADIUS)/**10*/);
  118. renderer.setPositionCalculator(positionCalculator);
  119. mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  120. mapView.setPointCalculator(positionCalculator);
  121. Log.i("OnCreateView", "calculator set:" + mapView.getHeight());
  122. }
  123. });
  124. mapView.setRenderer(renderer);
  125. mapView.setMapSize(1200f, 1200f);
  126. return rootView;
  127. }
  128.  
  129. private void createMap() {
  130. OverpassDataFetcher fetcher = new OverpassDataFetcher();
  131. location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  132. // String string[] = new String[]{String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()), RADIUS};
  133. String string[] = new String[]{LATITUDE, LONGITUDE, RADIUS};
  134. Log.i("OBJECTS", "startedloading: ");
  135. fetcher.startFetching(string, this);
  136. }
  137.  
  138. @Override
  139. public void onClick(View v) {
  140. int id = v.getId();
  141. if (id == R.id.butPlus) {
  142. mapView.zoomIn();
  143. } else if (id == R.id.butMinus) {
  144. mapView.zoomOut();
  145. } else if (id == R.id.butUp) {
  146. mapLevel++;
  147. renderer.setMapObjects(map.getObjectsForLevel(mapLevel));
  148. mapView.invalidate();
  149. } else if (id == R.id.butDown) {
  150. mapLevel--;
  151. renderer.setMapObjects(map.getObjectsForLevel(mapLevel));
  152. mapView.invalidate();
  153. }
  154. }
  155.  
  156. @Override
  157. public void onJsonLoaded(final OSMData data) {
  158. final OSMDataParser parser = new OSMDataParser();
  159. Log.i("OBJECTS", "loaded: ");
  160. new Thread(new Runnable() {
  161. @Override
  162. public void run() {
  163. map = parser.parseOSMData(data);
  164. mapView.post(new Runnable() {
  165. @Override
  166. public void run() {
  167. Toast.makeText(getActivity(), "objects loaded: " + map.getObjects().size(), Toast.LENGTH_SHORT).show();
  168. renderer.setMapObjects(map.getObjectsForLevel(mapLevel));
  169. Log.i("OBJECTS", "parsed: ");
  170. mapView.setMapCenter(Float.valueOf(LATITUDE), Float.valueOf(LONGITUDE));
  171. // mapView.setMapCenter((float) location.getLatitude(), (float) location.getLongitude());
  172. mapView.setZoomLevel(1);
  173. final Handler handler = new Handler();
  174. // handler.postDelayed(new Runnable() {
  175. // @Override
  176. // public void run() {
  177. // // mapView.setPosition(LONGIT[counter], LATIT[counter++]);
  178. // mapView.invalidate();
  179. // if (counter < LONGIT.length) {
  180. // //handler.postDelayed(this, 3000);
  181. // }
  182. // }
  183. // }, 1000);
  184. mapView.invalidate();
  185. }
  186. });
  187. }
  188. }).start();
  189.  
  190. new Thread(new PositionUpdateRunnable(this, localization)).start();
  191. }
  192.  
  193. @Override
  194. public void onNewPosition(double lat, double longit, float level) {
  195. if ((int) level != this.mapLevel) {
  196. mapLevel = (int) level;
  197. renderer.setMapObjects(map.getObjectsForLevel(mapLevel));
  198. }
  199. if (lat == 0 || longit == 0) {
  200. if (location != null) {
  201. mapView.setPosition((float) location.getLatitude(), (float) location.getLongitude());
  202. mapView.setMapCenter((float) location.getLatitude(), (float) location.getLongitude());
  203. }
  204. } else {
  205. mapView.setPosition((float) longit, (float) lat);
  206. mapView.setMapCenter((float) longit, (float) lat);
  207. }
  208. mapView.invalidate();
  209. Log.i("Position", "Lat: " + lat + " long: " + longit);
  210. }
  211.  
  212. @Override
  213. public void onLocationChanged(Location location) {
  214. this.location = location;
  215. }
  216.  
  217. @Override
  218. public void onStatusChanged(String provider, int status, Bundle extras) {
  219.  
  220. }
  221.  
  222. @Override
  223. public void onProviderEnabled(String provider) {
  224.  
  225. }
  226.  
  227. @Override
  228. public void onProviderDisabled(String provider) {
  229.  
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement