Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.postplay;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.view.Menu;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class MapActive extends FragmentActivity {
- private GoogleMap googleMap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_map_active);
- setUpMapIfNeeded();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.map_active, menu);
- return true;
- }
- private void setUpMapIfNeeded() {
- if(googleMap == null)
- {
- googleMap = ((SupportMapFragment) getSupportFragmentManager().
- findFragmentById(R.id.map)).getMap();
- }
- if(googleMap != null)
- {
- setUpMap();
- }
- }
- private void setUpMap() {
- // enable location layer of gmaps
- googleMap.setMyLocationEnabled(true);
- // get locationmanager object from system service
- LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- // object to retrieve provider
- Criteria criteria = new Criteria();
- // get name of best provider
- String provider = locationManager.getBestProvider(criteria,true);
- // get current location
- Location myLocation = locationManager.getLastKnownLocation(provider);
- //set map type
- googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
- //get latitude of the current loc
- double latitude = myLocation.getLatitude();
- //get longitude
- double longitude = myLocation.getLongitude();
- // create a current location object
- LatLng latLng = new LatLng(latitude,longitude);
- //show current location on map
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- //zoom in
- googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
- googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).
- title("You are here!"));
- }
- }
- 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());
- 04-06 01:49:16.829: D/dalvikvm(28484): Rejecting registerization due to ushr-int/lit8 v2, v4, (#1)
- 04-06 01:49:16.839: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v0, v3, (#8)
- 04-06 01:49:16.849: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v4, v6, (#8)
- 04-06 01:49:16.849: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v4, v6, (#8)
- 04-06 01:49:16.849: D/AndroidRuntime(28484): Shutting down VM
- 04-06 01:49:16.849: W/dalvikvm(28484): threadid=1: thread exiting with uncaught exception (group=0x41ab8e10)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): FATAL EXCEPTION: main
- 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
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.access$700(ActivityThread.java:150)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.os.Handler.dispatchMessage(Handler.java:99)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.os.Looper.loop(Looper.java:176)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.main(ActivityThread.java:5279)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at java.lang.reflect.Method.invokeNative(Native Method)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at java.lang.reflect.Method.invoke(Method.java:511)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at dalvik.system.NativeStart.main(Native Method)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): Caused by: java.lang.NullPointerException
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at com.example.postplay.MapActive.setUpMap(MapActive.java:70)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at
- com.example.postplay.MapActive.setUpMapIfNeeded(MapActive.java:45)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at
- com.example.postplay.MapActive.onCreate(MapActive.java:26)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at
- android.app.Activity.performCreate(Activity.java:5267)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
- 04-06 01:49:16.849: E/AndroidRuntime(28484): ... 11 more
- public Location getLastKnownLocation (String provider)
- Added in API level 1
- Returns a Location indicating the data from the last known location fix obtained from the given provider.
- 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.
- If the provider is currently disabled, null is returned.
- Parameters
- provider the name of the provider
- Returns
- **the last known location for the provider, or null**
- double latitude = myLocation.getLatitude();
- Location myLocation = locationManager.getLastKnownLocation(provider);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement