Skip to content

Instantly share code, notes, and snippets.

@quydm
Created October 21, 2016 04:17
Show Gist options
  • Save quydm/a458d908c4da2496672f83372304f417 to your computer and use it in GitHub Desktop.
Save quydm/a458d908c4da2496672f83372304f417 to your computer and use it in GitHub Desktop.
Google Maps in Android fragment sample
import android.Manifest;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* @author quydm
*/
public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final String TAG = MapFragment.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private GoogleMap mGoogleMap;
private MapView mMapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);
Log.d(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
Log.d(TAG, "onCreateView");
return rootView;
}
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
mMapView.onResume();
setUpMap();
Log.d(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
mMapView.onPause();
Log.d(TAG, "onPause");
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
Log.d(TAG, "onLowMemory");
}
@Override
public void onConnected(Bundle bundle) {
if (!hasPermission(Manifest.permission.ACCESS_FINE_LOCATION))
return;
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null)
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
else
handleNewLocation(location);
Log.d(TAG, "onConnected");
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "onConnectionSuspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed");
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
Log.d(TAG, "onLocationChanged");
}
private void setUpMap() {
if (mGoogleMap == null)
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
}
});
}
private void handleNewLocation(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mGoogleMap.addMarker(options);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
private boolean hasPermission(String permission) {
return ContextCompat.checkSelfPermission(getActivity(), permission) == PackageManager.PERMISSION_GRANTED;
}
}
Copy link

ghost commented Dec 13, 2016

Thanks for the great code and logic. I implemented the same methodology. But, when I change to another fragment, and return to this fragment, onResume is called but onConnected isnt called. So, the marker disappear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment