To get your current location via Google Map in an Android app, this was how we do it:
1 2 3 4 |
@Override public void onMapReady(GoogleMap googleMap) { Location location = googleMap.getMyLocation(); } |
Stop. Don’t use that! The method GoogleMap.getMyLocation() is deprecated. Instead, we need to use FusedLocationApi
and the callback onLocationChanged()
.
Here’s an example code that works for me ( consider copying the contents of onResume() into onStart() ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
public class MainActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private GoogleMap map; private LocationRequest mLocationRequest; private GoogleApiClient mGoogleApiClient; private Location mLastLocation; private Marker marker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){ buildGoogleApiClient(); mGoogleApiClient.connect(); } if (map == null) { MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } } @Override public void onMapReady(GoogleMap retMap) { map = retMap; setUpMap(); } public void setUpMap(){ map.setMapType(GoogleMap.MAP_TYPE_HYBRID); map.setMyLocationEnabled(true); } @Override protected void onPause(){ super.onPause(); if (mGoogleApiClient != null) { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } } protected synchronized void buildGoogleApiClient() { Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } @Override public void onConnected(Bundle bundle) { Toast.makeText(this,"onConnected", Toast.LENGTH_SHORT).show(); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(1000); mLocationRequest.setFastestInterval(1000); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); //mLocationRequest.setSmallestDisplacement(0.1F); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onLocationChanged(Location location) { mLastLocation = location; //remove previous current location Marker if (marker != null){ marker.remove(); } double dLatitude = mLastLocation.getLatitude(); double dLongitude = mLastLocation.getLongitude(); marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude)) .title("My Location").icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_RED))); map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8)); } } |
Code source: How to get current location of device on google maps api v2, getMyLocation() method is deprecated