Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. package com.example.postplay;
  2.  
  3. import android.location.Criteria;
  4. import android.location.Location;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. import android.support.v4.app.FragmentActivity;
  8. import android.view.Menu;
  9. import com.google.android.gms.maps.CameraUpdateFactory;
  10. import com.google.android.gms.maps.GoogleMap;
  11. import com.google.android.gms.maps.SupportMapFragment;
  12. import com.google.android.gms.maps.model.LatLng;
  13. import com.google.android.gms.maps.model.MarkerOptions;
  14.  
  15. public class MapActive extends FragmentActivity {
  16.  
  17. private GoogleMap googleMap;
  18.  
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_map_active);
  24.  
  25. setUpMapIfNeeded();
  26. }
  27.  
  28. @Override
  29. public boolean onCreateOptionsMenu(Menu menu) {
  30. // Inflate the menu; this adds items to the action bar if it is present.
  31. getMenuInflater().inflate(R.menu.map_active, menu);
  32. return true;
  33. }
  34.  
  35. private void setUpMapIfNeeded() {
  36.  
  37. if(googleMap == null)
  38. {
  39. googleMap = ((SupportMapFragment) getSupportFragmentManager().
  40. findFragmentById(R.id.map)).getMap();
  41. }
  42. if(googleMap != null)
  43. {
  44. setUpMap();
  45. }
  46.  
  47. }
  48.  
  49. private void setUpMap() {
  50. // enable location layer of gmaps
  51. googleMap.setMyLocationEnabled(true);
  52.  
  53. // get locationmanager object from system service
  54. LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  55.  
  56. // object to retrieve provider
  57. Criteria criteria = new Criteria();
  58.  
  59. // get name of best provider
  60. String provider = locationManager.getBestProvider(criteria,true);
  61.  
  62. // get current location
  63. Location myLocation = locationManager.getLastKnownLocation(provider);
  64.  
  65. //set map type
  66. googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  67.  
  68. //get latitude of the current loc
  69. double latitude = myLocation.getLatitude();
  70.  
  71. //get longitude
  72. double longitude = myLocation.getLongitude();
  73.  
  74. // create a current location object
  75. LatLng latLng = new LatLng(latitude,longitude);
  76.  
  77. //show current location on map
  78. googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  79.  
  80. //zoom in
  81. googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
  82. googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).
  83. title("You are here!"));
  84.  
  85. }
  86.  
  87. }
  88.  
  89. 04-06 01:49:16.819: W/ActivityThread(28484): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
  90. 04-06 01:49:16.829: D/dalvikvm(28484): Rejecting registerization due to ushr-int/lit8 v2, v4, (#1)
  91. 04-06 01:49:16.839: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v0, v3, (#8)
  92. 04-06 01:49:16.849: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v4, v6, (#8)
  93. 04-06 01:49:16.849: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v4, v6, (#8)
  94. 04-06 01:49:16.849: D/AndroidRuntime(28484): Shutting down VM
  95. 04-06 01:49:16.849: W/dalvikvm(28484): threadid=1: thread exiting with uncaught exception (group=0x41ab8e10)
  96. 04-06 01:49:16.849: E/AndroidRuntime(28484): FATAL EXCEPTION: main
  97. 04-06 01:49:16.849: E/AndroidRuntime(28484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.postplay/com.example.postplay.MapActive}: java.lang.NullPointerException
  98. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
  99. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
  100. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.access$700(ActivityThread.java:150)
  101. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
  102. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.os.Handler.dispatchMessage(Handler.java:99)
  103. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.os.Looper.loop(Looper.java:176)
  104. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.main(ActivityThread.java:5279)
  105. 04-06 01:49:16.849: E/AndroidRuntime(28484): at java.lang.reflect.Method.invokeNative(Native Method)
  106. 04-06 01:49:16.849: E/AndroidRuntime(28484): at java.lang.reflect.Method.invoke(Method.java:511)
  107. 04-06 01:49:16.849: E/AndroidRuntime(28484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
  108. 04-06 01:49:16.849: E/AndroidRuntime(28484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
  109. 04-06 01:49:16.849: E/AndroidRuntime(28484): at dalvik.system.NativeStart.main(Native Method)
  110. 04-06 01:49:16.849: E/AndroidRuntime(28484): Caused by: java.lang.NullPointerException
  111. 04-06 01:49:16.849: E/AndroidRuntime(28484): at com.example.postplay.MapActive.setUpMap(MapActive.java:70)
  112. 04-06 01:49:16.849: E/AndroidRuntime(28484): at
  113. com.example.postplay.MapActive.setUpMapIfNeeded(MapActive.java:45)
  114. 04-06 01:49:16.849: E/AndroidRuntime(28484): at
  115. com.example.postplay.MapActive.onCreate(MapActive.java:26)
  116. 04-06 01:49:16.849: E/AndroidRuntime(28484): at
  117. android.app.Activity.performCreate(Activity.java:5267)
  118. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
  119. 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
  120. 04-06 01:49:16.849: E/AndroidRuntime(28484): ... 11 more
  121.  
  122. public Location getLastKnownLocation (String provider)
  123.  
  124. Added in API level 1
  125. Returns a Location indicating the data from the last known location fix obtained from the given provider.
  126.  
  127. This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
  128.  
  129. If the provider is currently disabled, null is returned.
  130.  
  131. Parameters
  132. provider the name of the provider
  133. Returns
  134. **the last known location for the provider, or null**
  135.  
  136. double latitude = myLocation.getLatitude();
  137.  
  138. Location myLocation = locationManager.getLastKnownLocation(provider);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement