Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current location in google map android

Actually my problem is I am not getting current location latitude and longitude I tried so many ways.I know that this question already asked in SO I tried that answers also still I didn't get answer.Please help me Code:

    if (googleMap == null) {         googleMap = ((MapFragment) getFragmentManager().findFragmentById(                 R.id.map)).getMap();          // check if map is created successfully or not         if (googleMap == null) {             Toast.makeText(getApplicationContext(),                     "Sorry! unable to create maps", Toast.LENGTH_SHORT)                     .show();         }     }     googleMap.setMyLocationEnabled(true);     Location myLocation = googleMap.getMyLocation();  //Nullpointer exception.........     LatLng myLatLng = new LatLng(myLocation.getLatitude(),             myLocation.getLongitude());      CameraPosition myPosition = new CameraPosition.Builder()             .target(myLatLng).zoom(17).bearing(90).tilt(30).build();     googleMap.animateCamera(         CameraUpdateFactory.newCameraPosition(myPosition)); 
like image 337
skyshine Avatar asked Jan 28 '14 10:01

skyshine


People also ask

How do I get Google Maps to show my current location?

Quick tip: In the iPhone and Android apps, tapping the Current Location button twice will turn on compass mode. The app will center on your location, but the map will rotate to match the direction you're currently facing in real life.

How do I turn on my location on Google Maps Android?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.


2 Answers

Please check the sample code for the Google Maps Android API v2. Using this will solve your problem.

private void setUpMapIfNeeded() {     // Do a null check to confirm that we have not already instantiated the map.     if (mMap == null) {         // Try to obtain the map from the SupportMapFragment.         mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();         mMap.setMyLocationEnabled(true);         // Check if we were successful in obtaining the map.         if (mMap != null) {             mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {                 @Override                 public void onMyLocationChange(Location arg0) {                     mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));                 }             });         }     } } 

Call this function in onCreate function.

Update:

The method mMap.setOnMyLocationChangeListener is now deprecated, you need to use the FusedLocationProviderClient now.

private FusedLocationProviderClient fusedLocationClient; @Override protected void onCreate(Bundle savedInstanceState) {     fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); } 

To request the last known location, call the getLastLocation() method. The following code snippet illustrates the request and a simple handling of the response:

fusedLocationClient.getLastLocation()         .addOnSuccessListener(this, new OnSuccessListener<Location>() {             @Override             public void onSuccess(Location location) {                 // Got last known location. In some rare situations this can be null.                 if (location != null) {                     // Logic to handle location object                 }             }         }); 

Reference: https://developer.android.com/training/location/retrieve-current.html

like image 81
Shailendra Madda Avatar answered Sep 20 '22 09:09

Shailendra Madda


I think that better way now is:

Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

Documentation. Getting the Last Known Location

like image 27
Andrew Avatar answered Sep 18 '22 09:09

Andrew