Advertisement
Guest User

asd123

a guest
Nov 26th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. package com.example.bikeapp;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import android.annotation.SuppressLint;
  8. import android.app.AlertDialog;
  9. import android.app.Fragment;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.graphics.drawable.Drawable;
  13. import android.location.Location;
  14. import android.location.LocationListener;
  15. import android.location.LocationManager;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.Button;
  22. import android.widget.EditText;
  23. import android.widget.Toast;
  24.  
  25. import com.mapquest.android.maps.AnnotationView;
  26. import com.mapquest.android.maps.DefaultItemizedOverlay;
  27. import com.mapquest.android.maps.GeoPoint;
  28. import com.mapquest.android.maps.ItemizedOverlay;
  29. import com.mapquest.android.maps.MapView;
  30. import com.mapquest.android.maps.MyLocationOverlay;
  31. import com.mapquest.android.maps.OverlayItem;
  32.  
  33. @SuppressLint("NewApi")
  34. public class AlertFragment extends Fragment {
  35.  
  36. protected MapView map;
  37. private MyLocationOverlay myLocationOverlay;
  38. private Context context;
  39. private View rootView;
  40. protected Button createPointButton;
  41. AnnotationView annotation;
  42.  
  43. public static List<Location> locations = new ArrayList<Location>();
  44.  
  45. public AlertFragment() {
  46. }
  47.  
  48. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  49. Bundle savedInstanceState) {
  50.  
  51. rootView = inflater.inflate(R.layout.fragment_home, container, false);
  52. context = inflater.getContext();
  53.  
  54. createPointButton = (Button) rootView
  55. .findViewById(R.id.createPointButton);
  56. createPointButton.setOnClickListener(new View.OnClickListener() {
  57. @Override
  58. public void onClick(View v) {
  59. tester();
  60. }
  61. });
  62.  
  63. setupMapView();
  64.  
  65. this.myLocationOverlay = new MyLocationOverlay(context, map);
  66. myLocationOverlay.enableMyLocation();
  67. // Acquire a reference to the system Location Manager
  68. LocationManager locationManager = (LocationManager) getActivity()
  69. .getSystemService(Context.LOCATION_SERVICE);
  70.  
  71. // Define a listener that responds to location updates
  72. LocationListener locationListener = new LocationListener() {
  73. public void onLocationChanged(Location location) {
  74. // Called when a new location is found by the network location
  75. // provider.
  76. locations.add(location);
  77. Log.i("LOCATION", location.toString());
  78. Log.i("COUNT POINTS", String.valueOf(locations.size()));
  79. setUp2();
  80. double distance = 0.0;
  81.  
  82. // for (int i = 0; i < locations.size(); i++) {
  83. // if (i != 0) {
  84. // distance += calculateDistance(locations.get(i - 1),
  85. // locations.get(i));
  86. // }
  87. // }
  88. //
  89. // Log.i("DISTANCE", String.valueOf(distance));
  90. }
  91.  
  92. public void onStatusChanged(String provider, int status,
  93. Bundle extras) {
  94. }
  95.  
  96. public void onProviderEnabled(String provider) {
  97. }
  98.  
  99. public void onProviderDisabled(String provider) {
  100. }
  101. };
  102.  
  103. // Register the listener with the Location Manager to receive location
  104. // updates
  105. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
  106. 0, locationListener);
  107.  
  108. return rootView;
  109. }
  110.  
  111. // set your map and enable default zoom controls
  112. private void setupMapView() {
  113. this.map = (MapView) rootView.findViewById(R.id.map);
  114. // map.setBuiltInZoomControls(true);
  115. }
  116.  
  117. // set up a MyLocationOverlay and execute the runnable once we have a
  118. // location fix
  119. // private void setupMyLocation() {
  120. //
  121. // setUp2();
  122. // }
  123.  
  124. void setUp2(){
  125. myLocationOverlay.runOnFirstFix(new Runnable() {
  126. @Override
  127. public void run() {
  128. GeoPoint currentLocation = myLocationOverlay.getMyLocation();
  129.  
  130. map.getController().animateTo(currentLocation);
  131. map.getController().setZoom(30);
  132. map.getOverlays().add(myLocationOverlay);
  133.  
  134. myLocationOverlay.setFollowing(true);
  135.  
  136.  
  137. }
  138. });
  139. }
  140.  
  141. /**
  142. * Calculate Distance using two GeoPoints
  143. *
  144. * @param StartP
  145. * @param EndP
  146. * @return double distance of StartP - EndP
  147. */
  148. public double calculateDistance(GeoPoint StartP, GeoPoint EndP) {
  149. int Radius = 6371;// radius of earth in Km
  150. double lat1 = StartP.getLatitudeE6() / 1E6;
  151. double lat2 = EndP.getLatitudeE6() / 1E6;
  152. double lon1 = StartP.getLongitudeE6() / 1E6;
  153. double lon2 = EndP.getLongitudeE6() / 1E6;
  154. double dLat = Math.toRadians(lat2 - lat1);
  155. double dLon = Math.toRadians(lon2 - lon1);
  156. double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
  157. + Math.cos(Math.toRadians(lat1))
  158. * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
  159. * Math.sin(dLon / 2);
  160. double c = 2 * Math.asin(Math.sqrt(a));
  161. double valueResult = Radius * c;
  162. double km = valueResult / 1;
  163. DecimalFormat newFormat = new DecimalFormat("####");
  164.  
  165. Integer kmInDec = Integer.valueOf(newFormat.format(km));
  166. double meter = valueResult % 1000;
  167. Integer meterInDec = Integer.valueOf(newFormat.format(meter));
  168.  
  169. Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
  170. + " Meter " + meterInDec);
  171.  
  172. return Radius * c;
  173. }
  174.  
  175. // enable features of the overlay
  176. public void onResume() {
  177. if (myLocationOverlay != null) {
  178. myLocationOverlay.enableMyLocation();
  179. myLocationOverlay.enableCompass();
  180. }
  181. super.onResume();
  182. }
  183.  
  184. // disable features of the overlay when in the background
  185. public void onPause() {
  186. super.onPause();
  187. myLocationOverlay.disableCompass();
  188. myLocationOverlay.disableMyLocation();
  189. }
  190.  
  191. public boolean isRouteDisplayed() {
  192. return false;
  193. }
  194.  
  195. private void addOverlay(String alerta) {
  196. Drawable icon = getResources().getDrawable(R.drawable.caution_512);
  197. final DefaultItemizedOverlay overlay = new DefaultItemizedOverlay(icon);
  198.  
  199. // add items with a title and a snippet
  200. OverlayItem item1 = new OverlayItem(myLocationOverlay.getMyLocation(),
  201. "Alerta", alerta);
  202. overlay.addItem(item1);
  203.  
  204. // add a tap listener
  205. // overlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
  206. // @Override
  207. // public void onTap(GeoPoint pt, MapView MapView) {
  208. // // when tapped, show the annotation for the overlayItem
  209. // int lastTouchedIndex = overlay.getLastFocusedIndex();
  210. // if(lastTouchedIndex>-1){
  211. // OverlayItem tapped = overlay.getItem(lastTouchedIndex);
  212. // annotation.showAnnotationView(tapped);
  213. // }
  214. // }
  215. // });
  216.  
  217. overlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
  218. @Override
  219. public void onTap(GeoPoint pt, MapView MapView) {
  220. // when tapped, show the annotation for the overlayItem
  221. int lastTouchedIndex = overlay.getLastFocusedIndex();
  222. if (lastTouchedIndex > -1) {
  223. OverlayItem tapped = overlay.getItem(lastTouchedIndex);
  224. Toast.makeText(getActivity().getApplicationContext(),
  225. tapped.getTitle() + ": " + tapped.getSnippet(),
  226. Toast.LENGTH_SHORT).show();
  227. }
  228. }
  229. });
  230.  
  231. map.getOverlays().add(overlay);
  232. map.invalidate();
  233. }
  234.  
  235. public void tester() {
  236. AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
  237.  
  238. alert.setTitle("Agregar Alerta");
  239. alert.setMessage("Mensaje");
  240.  
  241. // Set an EditText view to get user input
  242. final EditText input = new EditText(getActivity());
  243. alert.setView(input);
  244.  
  245. alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  246. public void onClick(DialogInterface dialog, int whichButton) {
  247. String value = input.getText().toString();
  248. addOverlay(value);
  249. }
  250. });
  251.  
  252. alert.setNegativeButton("Cancelar",
  253. new DialogInterface.OnClickListener() {
  254. public void onClick(DialogInterface dialog, int whichButton) {
  255. // Canceled.
  256. }
  257. });
  258.  
  259. alert.show();
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement