How to supply location services to diffent activities on Android - android

I am currently working on an Android app and I am new to the field.
I want to do the following:
Create a class that encapsulates the required features to let an activity know the users location and deliver a map to the activity
This should accomplish the following:
I have a class that connects to the location services to get information and other activities can use this class to get a map (fragment) along with other information (lat long etc) for programmatic use.
I cannot figure it out...I do not want my stuff to an Activity itself
But it seems as if the whole Google Location API of the Play services relies on it being a FragmentActivity (whatever that is)
Any ideas?
PS: I need to maintain support for 2.3.3
UPDATE
I made it to implement a class extending SupportMapFragment (shortened), but I am having trouble with the error handling for the GMS. Works fine on my Galaxy Note 3 but the emulator has an older version of GMS which (due to currently crappy errorhandling) eventually leads to a NullPointer Exception:
public class LocationFragment extends SupportMapFragment implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.map_fragment, container, false);
initilizeMap();
configureLocationClient();
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
// Start with updates turned off
updatesRequested = false;
map.getUiSettings().setMyLocationButtonEnabled(true);
locationClient = new LocationClient(activity.getBaseContext(), this, this);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
checkGooglePlayServices();
}
private boolean checkGooglePlayServices() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity.getBaseContext());
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d(TAG + ".servicesConnected()", "Google Play services is available.");
// Continue
return true;
// Google Play services was not available for some reason
} else {
Log.d(TAG + ".servicesConnected()", "Google Play services is unavailable or outdated: " + resultCode);
// Get the error dialog from Google Play services
try {
GooglePlayServicesUtil.getErrorDialog(resultCode, activity, CONNECTION_FAILURE_RESOLUTION_REQUEST).show();
} catch (Exception e) {
Log.e("Error: GooglePlayServiceUtil: ", "" + e);
}
/**
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
// Set the dialog in the DialogFragment
errorFragment.setDialog(errorDialog);
// Show the error dialog in the DialogFragment
errorFragment.show(activity.getSupportFragmentManager(),"Location Updates");
}*/
return false;
}
}
The corresponding Activity is:
public class LocationTest extends FragmentActivity implements LocationListener {
private static final String TAG = LocationTest.class.getSimpleName();
private int count = 0;
private TextView lat;
private TextView lng;
private TextView quality;
private TextView conState;
private TextView refreshCount;
private TextView distance;
private LocationFragment locFrag;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationtest);
FragmentManager fManager = getSupportFragmentManager();
locFrag = new LocationFragment();
FragmentTransaction ft = fManager.beginTransaction();
ft.replace(R.id.map, locFrag);
ft.addToBackStack(null);
ft.commit();
locFrag.setGoal(Double.valueOf(8.83749747285), Double.valueOf(53.0663656578));
lat = (TextView) findViewById(R.id.curLat);
lng = (TextView) findViewById(R.id.curLng);
quality = (TextView) findViewById(R.id.curQuality);
conState = (TextView) findViewById(R.id.conState);
refreshCount = (TextView) findViewById(R.id.count);
distance = (TextView) findViewById(R.id.distance);
}
public void refreshLocation(View v) {
locFrag.refreshLocation(v);
}
public void toggleUpdates(View v) {
locFrag.toggleUpdates(v);
}
public void onLocationChanged(Location location) {
// Let the location parent know about the location change
// super.onLocationChanged(location);
count++;
lat.setText(Double.toString(locFrag.getCurrentLatitude()));
lng.setText(Double.toString(locFrag.getCurrentLongitude()));
quality.setText(Float.toString(locFrag.getAccuracy()));
conState.setText(Boolean.toString(locFrag.isConnected()));
refreshCount.setText(Integer.toString(count));
distance.setText(Float.toString(locFrag.getDistance()));
}
#Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
}
}
I wonder if it is possible to get the ErrorDialog suggested by google to work from that SupportMapFragment to be able to handle the error or else stop the inflation of the fragment and return to the previous activity
any help appreciated :)

Use this code in present class which is capturing the location
currentLocation is the location Object
Intent intent = new Intent(this,target.class)
Bundle b = new Bundle();
b.putParcelable("Location", currentLocation);
i.putExtra("Location", b);
startActivity(i);
Receive Activity code:
b = getArguments();
Location location = b.getParcelable("Location");
By this way you can pass the location.I think it may be useful to you.

Related

GoogleApiClient in Fragment Android

I try to learn Android by myself. And in my app, I want to use fragment to show google map and when user open my app, I want to get current location by using GoogleApiClient
My fragment code:
public class HomeFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public interface comuticateParent {
public void sendMess(String text);
}
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
comuticateParent callback;
Button btn1;
TextView textView;
MapView mMapView;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
LocationRequest mLocationRequest;
private GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
googleMap.setMyLocationEnabled(true);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.d("onCreateView", Boolean.toString(mGoogleApiClient.isConnected()));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 13));
}
});
return rootView;
}
#Override
public void onStart() {
mGoogleApiClient.connect();
Log.d("ConnectonStart", "Connected ");
Log.d("ONstart", Boolean.toString(mGoogleApiClient.isConnected()));
super.onStart();
}
#Override
public void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
#Override
public void onResume() {
mGoogleApiClient.connect();
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
#Override
public void onConnected(Bundle bundle) {
Log.d("Connect", "Connected ");
Log.d("onConnected", Boolean.toString(mGoogleApiClient.isConnected()));
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Connect", "failed ");
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity;
if (context instanceof Activity) {
activity = (Activity) context;
callback = (comuticateParent) getActivity();
}
}
}
And problem here:
-Log in method onCreateView appears before log in method onconnected, so I can't get getLastLocation() because The googleAPIClient is not connect yet.
I have search in google but I don't know how to fix it.
Please help me!
Sorry about my best English.
First problem Log in method onCreateView appears before log in method onconnected, because GoogleApiClient will accessing Google APIs, verify app configuration, verify certificate fingerprint, etc. It take too long to process. To avoid blocking the UI thread, it'll execute in another thread and use asynchronous callback.
Second problem I can't get getLastLocation() because The googleAPIClient is not connect yet, because FusedLocationApi will only maintain background, so you need to get GoogleMap in background.
See my sample here: https://gist.github.com/quydm/a458d908c4da2496672f83372304f417
You will have to wait until the GoogleApiClient has connected before you can request location updates. In order to do so, you would move this block of code (currently found in your onCreateView() method):
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.d("onCreateView", Boolean.toString(mGoogleApiClient.isConnected()));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 13));
to the body of the overridden method onConnected().
You cannot have this code directly in onCreateView() because of the very problem you had. The connection must be established first before any location requests are sent. The connection can establish quickly or take longer than normal. It depends on many things outside of your control.
For that reason, I would suggest showing some sort of progress bar to indicate that location services is connecting and then remove the progress bar when onConnected() is called. Of course this depends entirely on your app. If location services is critical for the user to have then this is a must, if not, then you may not need to add it.

Nested fragments - Can't retain fragements that are nested in other fragments

I have an android app that consists of a number of nested fragments, for example I have a set of tabs and clicking a tab loads a fragment into the content area.
One of my tabs is a map showing locations on it and I am doing a geo lookup on a postcode to get back the coordinates for it.
This is all working and now finally I need to ensure that if a user clicks the map tab and initiates the async task which does the geo lookup that a device rotation doesnt stop this.
My map fragment which is loaded when the tab is clicked:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = (View)inflater.inflate(R.layout.fragment_locations, container, false);
// Make sure user's device supports Google play services
try {
MapsInitializer.initialize(getActivity());
mapView = (MapView) rootView.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
googleMap = mapView.getMap();
Log.i(LOG_TAG, "Google play service is available.");
if(googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(), "Problem creating Google map", Toast.LENGTH_LONG).show();
} else {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings().setZoomControlsEnabled(true);
FragmentManager fm = getFragmentManager();
mRetainedTaskFragment = (LocationNetworkOperationsRetainedFragment) fm.findFragmentByTag(TAG_RETAINED_TASK_FRAGMENT);
// If the Fragment is non-null, then it is currently being
// retained across a configuration change.
if (mRetainedTaskFragment == null) {
mRetainedTaskFragment = new LocationNetworkOperationsRetainedFragment();
fm.beginTransaction().add(mRetainedTaskFragment, TAG_RETAINED_TASK_FRAGMENT).commit();
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "Google play service is not available.");
Toast.makeText(getActivity().getApplicationContext(), "Google play service is not available", Toast.LENGTH_LONG).show();
}
return rootView;
}
My async task:
public class LocationNetworkOperations extends AsyncTask<String, Void, LatLng> {
#Override
protected LatLng doInBackground(String... postcodes) {
Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
LatLng coords= null;
try {
List<Address> addresses = geocoder.getFromLocationName(postcodes[0], 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
coords = new LatLng(address.getLatitude(), address.getLongitude());
} else {
Toast.makeText(getActivity().getApplicationContext(), "Unable to geocode postcode" + postcodes[0], Toast.LENGTH_LONG).show();
}
} catch(Exception ioe) {
ioe.printStackTrace();
}
return coords;
}
#Override
protected void onPostExecute(LatLng coords) {
if(coords != null) {
Marker mapMarkerHome = googleMap.addMarker(new MarkerOptions().position(coords).title("Home")
.snippet("E10 6JQ").icon(BitmapDescriptorFactory
.fromResource(R.drawable.home_map_marker)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(coords).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
The retained fragment:
public class LocationNetworkOperationsRetainedFragment extends Fragment {
interface LocationNetworkOperationsTaskCallbacks {
void onPreExecute();
void onProgressUpdate(int percent);
void onCancelled();
void onPostExecute();
}
private LocationNetworkOperationsTaskCallbacks mCallbacks;
private LocationsFragment.LocationNetworkOperations mTask;
#Override
public void onAttach(Context context) {
super.onAttach(context);
mCallbacks = (LocationNetworkOperationsTaskCallbacks) context;
}
/**
* This method will only be called once when the retained
* Fragment is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retain this fragment across configuration changes.
setRetainInstance(true);
// Create and execute the background task.
mTask = new LocationsFragment().new LocationNetworkOperations();
mTask.execute("E106JQ");
}
#Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
}
So I followed a tutorial and wrote a retained fragment and kick off the aysnc task in that but I am running into the following exception:
java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments
at android.support.v4.app.Fragment.setRetainInstance(Fragment.java:820)
at com.example.android.LocationNetworkOperationsRetainedFragment.onCreate(LocationNetworkOperationsRetainedFragment.java:37)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Why is it not possible to retain the fragment if it is nested and is there a solution to this?
Try using FragmentManager.saveFragmentInstanceState(Fragment) to retrieve a fragment state. The return value implements Parcelable, so you can put it in a Bundle.
For restoration, you can provide the state after creating the fragment using Fragment.setInitialSavedState(Fragment.SavedState).
Can't retain fragements that are nested in other fragments
This is a limitation of nested Fragments. I'm guessing one or more of your child Fragments have setRetainInstance(true) somewhere in their code. You need to remove that to prevent the error.

Android Maps V2 memory leak LocationClientHelper

We're trying to track down a memory leak happening on the GoogleMap in our Android app, which ends in an OOM after about 40-50 device rotations. The map gets set around 3500 markers.
The App has a minSDK of 9 and therefore using the SupportMapFragment from the V4 Support Library.
We've tried multiple things including:
Caching the LatLng's
Caching CameraUpdates
Removing markers from map
Removing listeners from map
Removing all listeners, markers etc so that we just have a plain map
Updating Google Play Services library
Updating Support library
Analyzing the memory dump in MAT shows that we accumulate lots of instances of
com.google.android.gms.location.internal.LocationClientHelper$ListenerTransport
which we have no clue where they are coming from.
Anyone has an idea on what could be the cause of this memory leak?
The following code has already all markes and listeners removed and still leaks. First the base class:
public abstract class BaseMapFragment extends Fragment {
public static final int MENU_ITEM_ID_SEARCH= 102;
public static final int MENU_ITEM_ID_SHOW_LIST= 100;
public static final int ZOOM_LEVEL_DEFAULT= 14;
private static final String SAVED_INSTANCE_LATITUDE= "savedLatitude";
private static final String SAVED_INSTANCE_LONGITUDE= "savedLongitutde";
private static final String SAVED_INSTANCE_ZOOM= "savedZoom";
protected static final String CLASSTAG= BaseMapFragment.class.getSimpleName();
private GoogleMap mMap;
private CameraUpdate mResumeCameraUpdate= null;
private double mSavedLatitude;
private double mSavedLongitude;
private float mSavedZoom;
private static View mView;
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mMap != null) {
outState.putDouble(SAVED_INSTANCE_LATITUDE, mMap.getCameraPosition().target.latitude);
outState.putDouble(SAVED_INSTANCE_LONGITUDE, mMap.getCameraPosition().target.longitude);
outState.putFloat(SAVED_INSTANCE_ZOOM, mMap.getCameraPosition().zoom);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (savedInstanceState != null) {
mSavedLatitude= savedInstanceState.getDouble(SAVED_INSTANCE_LATITUDE, Constants.EXTRA_VALUE_NONE);
mSavedLongitude= savedInstanceState.getDouble(SAVED_INSTANCE_LONGITUDE, Constants.EXTRA_VALUE_NONE);
mSavedZoom= savedInstanceState.getFloat(SAVED_INSTANCE_ZOOM, Constants.EXTRA_VALUE_NONE);
}
if (mView != null) {
ViewGroup parent= (ViewGroup) mView.getParent();
if (parent != null)
parent.removeView(mView);
}
try {
mView= inflater.inflate(R.layout.map_layout, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return mView;
}
protected GoogleMap initializeMap() {
if (mMap != null) {
if (mSavedLatitude != Constants.EXTRA_VALUE_NONE && mSavedLatitude != 0.0) {
mResumeCameraUpdate= Context.getCamUpdate(mSavedZoom, mSavedLatitude, mSavedLongitude);
} else {
mResumeCameraUpdate= Context.getCamUpdate(mMap.getCameraPosition().zoom, mMap.getCameraPosition().target.latitude, mMap.getCameraPosition().target.longitude);
}
}
SupportMapFragment mapFragment= (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
mapFragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
MapsInitializer.initialize(getActivity());
mapFragment= SupportMapFragment.newInstance();
mMap= mapFragment.getMap();
} else {
mMap= mapFragment.getMap();
}
} else {
mMap= mapFragment.getMap();
}
// check if map is created successfully or not
if (mMap == null) {
Toast.makeText(getActivity().getApplicationContext(), R.string.map_create_unable, Toast.LENGTH_SHORT).show();
} else {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick() {
if (mMap.getMyLocation() != null) {
CameraUpdate newLatLngZoom= Context.getCamUpdate(ZOOM_LEVEL_DEFAULT, mMap.getMyLocation());
mMap.animateCamera(newLatLngZoom);
} else {
Toast.makeText(getActivity().getApplicationContext(), R.string.map_location_services_disabled, Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
return mMap;
}
}
Subclass
public class MySupportMapFragment extends BaseMapFragment {
private LinearLayout mStaoButtonsLayout;
private ToggleButton mStaoButton;
private ToggleButton mGasStaoButton;
private Boolean mInitialLocationChange;
private CameraUpdate mResumeCameraUpdate;
private GoogleMap mMap;
private double mBundleLatitude;
private double mBundleLongitude;
#Override
public void addRequiredModelClasses(LinkedHashSet<Class<? extends ComergeModel<?>>> set) {
set.add(AboModel.class);
set.add(StationModel.class);
super.addRequiredModelClasses(set);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putDouble(BUNDLE_EXTRA_CENTER_LATITUDE, mBundleLatitude);
outState.putDouble(BUNDLE_EXTRA_CENTER_LONGITUDE, mBundleLongitude);
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(showSearchButton());
final StationModel stationModel= getContext().getModel(StationModel.class);
mStaoButtonsLayout= (LinearLayout) getActivity().findViewById(R.id.mapStaoButtons);
mStaoButtonsLayout.setVisibility(View.VISIBLE);
mStaoButton= (ToggleButton) mStaoButtonsLayout.findViewById(R.id.staoButton);
mStaoButton.setChecked(stationModel.isStationButtonChecked());
mGasStaoButton= (ToggleButton) mStaoButtonsLayout.findViewById(R.id.gasStaoButton);
mGasStaoButton.setChecked(stationModel.isGasStationButtonChecked());
mMap= initializeMap();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
addSearchButton(menu);
}
}
I had a similar problem before. I added following code to solve my problem:
#Override
public void onDestroy() {
if (mMap != null) {
mMap.setMyLocationEnabled(false);
}
}
It seems that LocationClientHelper$ListenerTransport is related to setMyLocationEnabled(). I had to unregister some callbacks to prevent memory leak.

google api client callback is never called

I am trying to get the last known location using google services API, but after I build the GoogleApiClient, no callback method is ever fired.
My activity looks like that :
public class MainActivity extends Activity implements FragmentObserver, SessionSpotListObserver,
ConnectionCallbacks, OnConnectionFailedListener{
//Objects used for the location API
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Unique tag for the error dialog fragment
private static final String DIALOG_ERROR = "dialog_error";
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;
public static final String STATE_RESOLVING_ERROR = "resolving_state";
//Request code to use when launching the activity to fix the connection to google API
private static final int REQUEST_SOLVE_CONNEXION = 999;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//We make sure that google play service is available on the device
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS){
//We get a connection to the Google Play Service API to get the location of the user
buildGoogleApiClient();
}
else {
GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
this,
REQUEST_SOLVE_CONNEXION);
}
}
#Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
showErrorDialog(result.getErrorCode());
mResolvingError = true;
}
}
// The rest of this code is all about building the error dialog
/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
// Create a fragment for the error dialog
ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt(DIALOG_ERROR, errorCode);
dialogFragment.setArguments(args);
dialogFragment.show(getFragmentManager(), "errordialog");
}
/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
mResolvingError = false;
}
/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() { }
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt(DIALOG_ERROR);
return GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), REQUEST_RESOLVE_ERROR);
}
#Override
public void onDismiss(DialogInterface dialog) {
((MainActivity)getActivity()).onDialogDismissed();
}
}
#Override
public void onConnected(Bundle arg0) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
Log.d("API Connection", "The API has connected and the last location is :" + mLastLocation);
if (mLastLocation != null) {
}
}
#Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
/**
* Creates the connexion to the Google API. Once the API is connected, the
* onConnected method is called.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
I placed breakpoints on all callback methods, that is how I know that none is called.
Because at this stage I am not using Google Map Api, I did not register my app to get a key. Do I need to do that even if I just get the location ?
Don't hesitate to tell me if you need more info.
Thank you all.
You never call mGoogleApiClient.connect() after building your GoogleApiClient. Your onCreate() should instead be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
Note that there is no need to call GooglePlayServicesUtil.isGooglePlayServicesAvailable() if you are using GoogleApiClient as connect() includes that check as well.
consider calling onLocationChanged() and passing it's Location parameter to mLastLocation for continuous location update when the user location changes. Also you might want to reduce the drain on your battery by setting LocationRequest() interval and distance to a small value.

Initialize MapFragment programmatically with Maps API v2

I'm trying to add a MapFragment to my current Fragment. The use of nested fragments is restricted to FragmentTransactions, you can't use the xml tag in your layout.
Also, I want it to be added to the main Fragment when the user presses a button. So, I'm creating the MapFragment programmatically with getInstance() when the user presses that button and adding it to the proper place. It is shown correctly, so far so good.
The problem is that after attaching the MapFragment I need to get a reference to GoogleMap to place a Marker, but the getMap() method returns null (as the fragment's onCreateView() hasn't been called yet).
I looked at the demo example code and I found the solution they use is initializing the MapFragment in onCreate() and getting the reference to GoogleMap in onResume(), after onCreateView() has been called.
I need to get the reference to GoogleMap right after the MapFragment initialization, because I want the users to be able to show or hide the map with a button. I know a possible solution would be to create the Map at the start as said above and just set it's visibility gone, but I want the map to be off by default so it doesn't take the user's bandwidth if they don't explicitly asked for it.
I tried with the MapsInitializer, but doesn't work either. I'm kind of stuck. Any ideas?
Here is my testing code so far:
public class ParadaInfoFragment extends BaseDBFragment {
// BaseDBFragment is just a SherlockFragment with custom utility methods.
private static final String MAP_FRAGMENT_TAG = "map";
private GoogleMap mMap;
private SupportMapFragment mMapFragment;
private TextView mToggleMapa;
private boolean isMapVisible = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_parada_info, container, false);
mToggleMapa = (TextView) v.findViewById(R.id.parada_info_map_button);
return v;
}
#Override
public void onStart() {
super.onStart();
mToggleMapa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isMapVisible) {
openMap();
} else {
closeMap();
}
isMapVisible = !isMapVisible;
}
});
}
private void openMap() {
// Creates initial configuration for the map
GoogleMapOptions options = new GoogleMapOptions().camera(CameraPosition.fromLatLngZoom(new LatLng(37.4005502611301, -5.98233461380005), 16))
.compassEnabled(false).mapType(GoogleMap.MAP_TYPE_NORMAL).rotateGesturesEnabled(false).scrollGesturesEnabled(false).tiltGesturesEnabled(false)
.zoomControlsEnabled(false).zoomGesturesEnabled(false);
// Modified from the sample code:
// It isn't possible to set a fragment's id programmatically so we set a
// tag instead and search for it using that.
mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
// We only create a fragment if it doesn't already exist.
if (mMapFragment == null) {
// To programmatically add the map, we first create a
// SupportMapFragment.
mMapFragment = SupportMapFragment.newInstance(options);
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.parada_info_map_container, mMapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}
// We can't be guaranteed that the map is available because Google Play
// services might not be available.
setUpMapIfNeeded(); //XXX Here, getMap() returns null so the Marker can't be added
// The map is shown with the previous options.
}
private void closeMap() {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.remove(mMapFragment);
fragmentTransaction.commit();
}
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 = mMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.addMarker(new MarkerOptions().position(new LatLng(37.4005502611301, -5.98233461380005)).title("Marker"));
}
}
}
}
Thanks
The good AnderWebs gave me an answer in Google+ but he is too laz.... emm busy to write it here again, so here is the short version:
Extend the MapFragment class and override the onCreateView() method. After this method is done we can get a non-null reference to que GoogleMap object.
This is my particular solution:
public class MiniMapFragment extends SupportMapFragment {
private LatLng mPosFija;
public MiniMapFragment() {
super();
}
public static MiniMapFragment newInstance(LatLng posicion){
MiniMapFragment frag = new MiniMapFragment();
frag.mPosFija = posicion;
return frag;
}
#Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
View v = super.onCreateView(arg0, arg1, arg2);
initMap();
return v;
}
private void initMap(){
UiSettings settings = getMap().getUiSettings();
settings.setAllGesturesEnabled(false);
settings.setMyLocationButtonEnabled(false);
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija,16));
getMap().addMarker(new MarkerOptions().position(mPosFija).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
}
}
Now in the previous Fragment class I do
mMapFragment = MiniMapFragment.newInstance(new LatLng(37.4005502611301, -5.98233461380005));
Maybe it's not perfect yet, because the screen blinks when showing the map. But not sure if the problem is because of this or something else.
Thanks, found this very helpful. Am posting my slightly modified solution, as it was cleaner for me to tell the parent Fragment when the map was ready. This method also works with a saveInstanceState / restoreInstanceState cycle.
public class CustomMapFragment extends SupportMapFragment {
private static final String LOG_TAG = "CustomMapFragment";
public CustomMapFragment() {
super();
}
public static CustomMapFragment newInstance() {
CustomMapFragment fragment = new CustomMapFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
View v = super.onCreateView(arg0, arg1, arg2);
Fragment fragment = getParentFragment();
if (fragment != null && fragment instanceof OnMapReadyListener) {
((OnMapReadyListener) fragment).onMapReady();
}
return v;
}
/**
* Listener interface to tell when the map is ready
*/
public static interface OnMapReadyListener {
void onMapReady();
}
}
To use as a nested Fragment:-
public class ParentFragment extends Fragment implements OnMapReadyListener {
...
mMapFragment = CustomMapFragment.newInstance();
getChildFragmentManager().beginTransaction().replace(R.id.mapContainer, mMapFragment).commit();
#Override
public void onMapReady() {
mMap = mMapFragment.getMap();
}
...
}
Hope it helps someone.
Here's my solution to this, I took inspiration from the code previously posted and cleaned it up. I also added the static methods with and without the GoogleMapOptions parameters.
public class GoogleMapFragment extends SupportMapFragment {
private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";
public static interface OnGoogleMapFragmentListener {
void onMapReady(GoogleMap map);
}
public static GoogleMapFragment newInstance() {
return new GoogleMapFragment();
}
public static GoogleMapFragment newInstance(GoogleMapOptions options) {
Bundle arguments = new Bundle();
arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);
GoogleMapFragment fragment = new GoogleMapFragment();
fragment.setArguments(arguments);
return fragment;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnGoogleMapFragmentListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().getClass().getName() + " must implement OnGoogleMapFragmentListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (mCallback != null) {
mCallback.onMapReady(getMap());
}
return view;
}
private OnGoogleMapFragmentListener mCallback;
}
The usage pattern is as follows:
public class MyMapActivity implements OnGoogleMapFragmentListener {
...
#Override
public void onMapReady(GoogleMap map) {
mUIGoogleMap = map;
...
}
...
private GoogleMap mUIGoogleMap;
}
No need to cutomize SupportMapFragment you can do this directly by using following piece of code,
FragmentManager fm = getSupportFragmentManager(); // getChildFragmentManager inside fragments.
CameraPosition cp = new CameraPosition.Builder()
.target(initialLatLng) // your initial co-ordinates here. like, LatLng initialLatLng
.zoom(zoom_level)
.build();
SupportMapFragment mapFragment = SupportMapFragment.newInstance(new GoogleMapOptions().camera(cp));
fm.beginTransaction().replace(R.id.rl_map, mapFragment).commit();
Add this piece of code for layout
<RelativeLayout
android:id="#+id/rl_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
This will load GoogleMap at particular Location directly i.e, initialLatLng.

Categories

Resources