Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. public class Main_Activity extends Activity {
  2.  
  3. TextView tvdistance;
  4. TextView tvSpeed;
  5.  
  6. double currentLon = 0;
  7. double currentLat = 0;
  8. double lastLon = 0;
  9. double lastLat = 0;
  10. double distance = 0;
  11. float[] dist = {
  12. 0, 0, 0
  13. };
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main_layout);
  19. tvdistance = (TextView) findViewById(R.id.tvdistance);
  20. tvSpeed = (TextView) findViewById(R.id.tvspeed1);
  21.  
  22. LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  23. lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, Loclist);
  24. Location loc2 = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  25.  
  26. if (loc == null) {
  27. tvdistance.setText("No GPS location found");
  28. }
  29. else {
  30. // set Current latitude and longitude
  31. currentLon = loc.getLongitude();
  32. currentLat = loc.getLatitude();
  33.  
  34. }
  35. // Set the last latitude and longitude
  36. lastLat = currentLat;
  37. lastLon = currentLon;
  38.  
  39. }
  40.  
  41. LocationListener Loclist = new LocationListener() {
  42.  
  43. @Override
  44. public void onLocationChanged(Location location) {
  45. // TODO Auto-generated method stub
  46.  
  47. // start location manager
  48. LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  49.  
  50. // Get last location
  51. Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  52.  
  53. // Request new location
  54. lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, Loclist);
  55.  
  56. // Get new location
  57. Location loc2 = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  58.  
  59. // get the current lat and long
  60. currentLat = loc.getLatitude();
  61. currentLon = loc.getLongitude();
  62.  
  63. if (currentLat != 0 && currentLon != 0) {
  64.  
  65. Location.distanceBetween(currentLat, currentLon, location.getLatitude(),
  66. location.getLongitude(), dist);
  67. distance += (long) dist[0];
  68. }
  69.  
  70. currentLat = location.getLatitude();
  71. currentLon = location.getLongitude();
  72.  
  73. float speed = location.getSpeed();
  74. tvSpeed.setText("Speed = " + speed / 1000 * 60 * 60 + "Km/h");
  75. tvdistance.setText("Distance = " + distance);
  76.  
  77. }
  78.  
  79. @Override
  80. public void onProviderDisabled(String provider) {
  81. // TODO Auto-generated method stub
  82.  
  83. }
  84.  
  85. @Override
  86. public void onProviderEnabled(String provider) {
  87. // TODO Auto-generated method stub
  88.  
  89. }
  90.  
  91. @Override
  92. public void onStatusChanged(String provider, int status, Bundle extras) {
  93. // TODO Auto-generated method stub
  94.  
  95. }
  96.  
  97. };
  98.  
  99. }
  100.  
  101. public class MainActivity extends Activity {
  102.  
  103. private GoogleMap googleMap;
  104. // The minimum distance to change Updates in meters
  105. private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1
  106. // meters
  107.  
  108. // The minimum time between updates in milliseconds
  109. private static final long MIN_TIME_BW_UPDATES = 1000; // 1
  110. // second
  111. // Declaring a Location Manager
  112. protected LocationManager locationManager;
  113.  
  114. // The alert dialog to enable GPS
  115. private Dialog alertDialog;
  116.  
  117. // flag for GPS status
  118. boolean isGPSEnabled = false;
  119.  
  120. Location location; // location
  121.  
  122. @Override
  123. protected void onCreate(Bundle savedInstanceState) {
  124. super.onCreate(savedInstanceState);
  125. setContentView(R.layout.activity_main);
  126. }
  127.  
  128. @Override
  129. public boolean onCreateOptionsMenu(Menu menu) {
  130. // Inflate the menu; this adds items to the action bar if it is present.
  131. getMenuInflater().inflate(R.menu.main, menu);
  132. return true;
  133. }
  134.  
  135. /*
  136. * (non-Javadoc)
  137. *
  138. * @see android.app.Activity#onStart()
  139. */
  140. @Override
  141. protected void onStart() {
  142. // fetching your current location
  143. super.onStart();
  144.  
  145. googleMap = ((MapFragment) getFragmentManager().findFragmentById(
  146. R.id.map)).getMap();
  147.  
  148. if (googleMap != null) {
  149.  
  150. googleMap.setMyLocationEnabled(true);
  151. googleMap.getUiSettings().setZoomControlsEnabled(true);
  152. googleMap.getUiSettings().setMyLocationButtonEnabled(true);
  153. googleMap.getUiSettings().setAllGesturesEnabled(true);
  154.  
  155. }
  156.  
  157. // Getting current location and adding the marker
  158.  
  159. locateMe();
  160.  
  161. }
  162.  
  163. /**
  164. *
  165. */
  166. private void locateMe() {
  167. // Checking for GPS Enabled
  168.  
  169. locationManager = (LocationManager) this
  170. .getSystemService(Context.LOCATION_SERVICE);
  171.  
  172. // getting GPS status
  173. isGPSEnabled = locationManager
  174. .isProviderEnabled(LocationManager.GPS_PROVIDER);
  175.  
  176. if (!isGPSEnabled) {
  177. // GPS is disabled
  178.  
  179. askUserToEnableGPS();
  180.  
  181. }
  182.  
  183. }
  184.  
  185. /**
  186. *
  187. */
  188. private void askUserToEnableGPS() {
  189. // Asking user to enable GPS
  190.  
  191. // 1. Instantiate an AlertDialog.Builder with its constructor
  192. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  193.  
  194. // 2. Chain together various setter methods to set the dialog
  195. // characteristics
  196. builder.setMessage(R.string.generic_gps_not_found)
  197. .setTitle(R.string.generic_gps_not_found_message_title)
  198. .setPositiveButton(R.string.generic_yes,
  199. new DialogInterface.OnClickListener() {
  200. @Override
  201. public void onClick(DialogInterface dialog, int id) {
  202. // User selected yes
  203. Intent intent = new Intent(
  204. Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  205. startActivity(intent);
  206. }
  207. })
  208. .setNegativeButton(R.string.generic_no,
  209. new DialogInterface.OnClickListener() {
  210. @Override
  211. public void onClick(DialogInterface dialog, int id) {
  212. // User selected no
  213. }
  214. });
  215.  
  216. // 3. Get the AlertDialog from create()
  217. AlertDialog dialog = builder.create();
  218. dialog.show();
  219.  
  220. }
  221.  
  222. /*
  223. * (non-Javadoc)
  224. *
  225. * @see android.app.Activity#onResume()
  226. */
  227. @Override
  228. protected void onResume() {
  229. // Getting location from GPS
  230. super.onResume();
  231.  
  232. // getting GPS status
  233. isGPSEnabled = locationManager
  234. .isProviderEnabled(LocationManager.GPS_PROVIDER);
  235.  
  236. if (isGPSEnabled) {
  237.  
  238. locationManager.requestLocationUpdates(
  239. LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
  240. MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
  241. Log.e("GPS Enabled", "GPS Enabled");
  242.  
  243. location = locationManager
  244. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  245.  
  246. }
  247.  
  248. }
  249.  
  250. LocationListener listener = new LocationListener() {
  251.  
  252. @Override
  253. public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  254. // TODO Auto-generated method stub
  255.  
  256. }
  257.  
  258. @Override
  259. public void onProviderEnabled(String arg0) {
  260. // TODO Auto-generated method stub
  261.  
  262. }
  263.  
  264. @Override
  265. public void onProviderDisabled(String arg0) {
  266. // TODO Auto-generated method stub
  267.  
  268. }
  269.  
  270. @Override
  271. public void onLocationChanged(Location arg0) {
  272. // Setting the marker
  273.  
  274. if (googleMap == null || location == null) {
  275. return;
  276. } else {
  277.  
  278. googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(
  279. location.getLatitude(), location.getLongitude())));
  280.  
  281. final Handler handler1 = new Handler();
  282. handler1.postDelayed(new Runnable() {
  283. @Override
  284. public void run() {
  285. // Do something after 3000ms
  286. googleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
  287.  
  288. }
  289. }, 1000);
  290.  
  291. Marker myLocation = googleMap.addMarker(new MarkerOptions()
  292. .position(
  293. new LatLng(location.getLatitude(), location
  294. .getLongitude()))
  295. .title("Me")
  296. .snippet("I am here")
  297. .icon(BitmapDescriptorFactory
  298. .fromResource(R.drawable.ic_launcher)));
  299.  
  300. }
  301.  
  302. locationManager.removeUpdates(listener);
  303. locationManager = null;
  304.  
  305. }
  306. };
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement