I'm trying to use the user's gps location in my app. The idea is to have a button that will fetch his current location and display it on a map. I don't want to have a real time update of his position. His location will be updated ONLY when he presses the button. Here is a sample of my fragment.
public class HomeFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11;
private static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 12;
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationManager mLocationManager;
private String mLatitudeText;
private String mLongitudeText;
private View view;
private MarkerOptions mMarker = new MarkerOptions();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
Log.d("debug", "inside TRy");
view = inflater.inflate(R.layout.fragment_homepage, container, false);
} catch (Exception e) {
Log.d("debug", "error inside try:"+e.toString());
} finally {
SupportMapFragment mMap = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
ImageButton mGpsUpdate = (ImageButton) view.findViewById(R.id.update_gps);
Log.d("debug", "after inflater");
mGpsUpdate.setOnClickListener(this);
createGoogleMapClient(); // <- moi
mMap.getMapAsync(this);
return view;
}
}
public void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
public void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
private void createGoogleMapClient(){
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d("debug", "in");
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng champlain = new LatLng(45.5164522,-73.52062409999996);
mMap.addMarker(new MarkerOptions().position(champlain).title("Champlain College"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(champlain));//Move camera to Champlain
CameraPosition oldPos = mMap.getCameraPosition();
CameraPosition pos = CameraPosition.builder(oldPos).bearing(-103).build(); //rotate map
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
//Debug
updateLocation();
mMap.setMinZoomPreference((float)17.3);
mMap.setMaxZoomPreference(20);
mMap.getUiSettings().setRotateGesturesEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
updateLocation();
}
private void checkPermission(){
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions( getActivity(), new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
MY_PERMISSION_ACCESS_COARSE_LOCATION );
ActivityCompat.requestPermissions( getActivity(), new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION },
MY_PERMISSION_ACCESS_FINE_LOCATION );
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onDestroyView() {
FragmentManager fm = getFragmentManager();
Fragment xmlFragment = fm.findFragmentById(R.id.map);
if (xmlFragment != null) {
fm.beginTransaction().remove(xmlFragment).commit();
}
super.onDestroyView();
}
public void updateLocation(){
checkPermission();
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText = String.valueOf(mLastLocation.getLatitude());
mLongitudeText = String.valueOf(mLastLocation.getLongitude());
Log.d("Coordinates",(mLatitudeText + ", " + mLongitudeText));
LatLng me = new LatLng(Double.parseDouble(mLatitudeText), Double.parseDouble(mLongitudeText));
mMap.addMarker(mMarker.position(me).title("me"));
}
}
@Override
public void onClick(View v) {
Log.d("debug", "inside Button Listener");
switch(v.getId()){
case R.id.update_gps:
Log.d("debug", "inside good case");
mMap.clear();
updateLocation();
break;
}
}
}
Any ideas?