##Quick explanation of methods used:
This is an Android Studio project that retrieves a user’s current location and displays it with a marker on a map. It is based on the following blog post
protected void onCreate(Bundle savedInstanceState){ setContentView(R.layout.activity_maps); setUpMapIfNeeded(); mGoogleApiClient=new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); //Setting a data object that contains quality of service parameters for requests mLocationRequest=LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(10*1000) // 10 seconds, in milliseconds .setFastestInterval(1*1000); } protected void onResume(){ setUpMapIfNeeded(); mGoogleApiClient.connect(); } protected void onPause(){ // If connected at onResume, then at onPause stop the location updates and disconnect if(mGoogleApiClient.isConnected()){ LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this); mGoogleApiClient.disconnect(); } } 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(); // Check if we were successful in obtaining the map. if(mMap!=null){ setUpMap(); } } } private void setUpMap(){ // You can setup markers based on lat and long here as below: // mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); } public void onConnected(Bundle bundle){ // Connection callback. After achieving a connected state by calling mGoogleApiClient.connect(), do the following: Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(location==null){ // Requests location updates if location does not have a value, while being connected to mGoogleApiClient, // passing in the registered mLocationRequest at onCreate, with this (MainActivity) being the Listener LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this); }else{ // If location does have a value, pass it in the handleNewLocation method and call it handleNewLocation(location); } } private void handleNewLocation(Location location){ double currentLatitude=location.getLatitude(); double currentLongitude=location.getLongitude(); LatLng latLng=new LatLng(currentLatitude,currentLongitude); MarkerOptions options=new MarkerOptions() .position(latLng) .title("I am here!"); mMap.addMarker(options); //Focus and Zoom in to current lat and long mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,12)); } //Called when the client is temporarily in a disconnected state public void onConnectionSuspended(int i){ } // Called when there was an error connecting the client to the service public void onConnectionFailed(ConnectionResult connectionResult){ if(connectionResult.hasResolution()){ try{ // Start an Activity that tries to resolve the error connectionResult.startResolutionForResult(this,CONNECTION_FAILURE_RESOLUTION_REQUEST); }catch(IntentSender.SendIntentException e){ e.printStackTrace(); } }else{ Log.i(TAG,"Location services connection failed with code "+connectionResult.getErrorCode()); } } public void onLocationChanged(Location location){ // Call the handleNewLocation again when the location changes handleNewLocation(location); }