Created
December 9, 2013 15:11
-
-
Save sultaniman/7873680 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyFragment extends SupportMapFragment { | |
| public static final String TAG = "MyFragment"; | |
| // Milliseconds per second | |
| private static final int MILLISECONDS_PER_SECOND = 1000; | |
| // Update frequency in seconds | |
| public static final int UPDATE_INTERVAL_IN_SECONDS = 120; | |
| // Update frequency in milliseconds | |
| private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; | |
| // The fastest update frequency, in seconds | |
| private static final int FASTEST_INTERVAL_IN_SECONDS = 60; | |
| // A fast frequency ceiling in milliseconds | |
| private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; | |
| Location currentLocation; | |
| LocationClient locationClient; | |
| LocationRequest locationRequest; | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
| if (savedInstanceState != null) { | |
| currentLocation = savedInstanceState.getParcelable("CURRENT_LOCATION"); | |
| } | |
| return super.onCreateView(inflater, container, savedInstanceState); | |
| } | |
| @Override | |
| public void onStart() { | |
| super.onStart(); | |
| map = this.getMap(); | |
| locationClient = new LocationClient(getActivity(), locationConnectionCallbacks, locationConnectionFailed); | |
| locationClient.connect(); | |
| // Current location restored | |
| if (currentLocation != null) { | |
| locationListener.onLocationChanged(currentLocation); | |
| } | |
| setHasOptionsMenu(true); | |
| } | |
| @Override | |
| public void onStop() { | |
| super.onStop(); | |
| locationClient.disconnect(); | |
| if (locationClient.isConnected()) { | |
| locationClient.removeLocationUpdates(locationListener); | |
| } | |
| } | |
| GooglePlayServicesClient.ConnectionCallbacks locationConnectionCallbacks = new GooglePlayServicesClient.ConnectionCallbacks() { | |
| @Override | |
| public void onConnected(Bundle bundle) { | |
| Log.d(TAG, "Location Callback. onConnected"); | |
| currentLocation = locationClient.getLastLocation(); | |
| LatLng point = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); | |
| map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 15.8f)); | |
| // Create the LocationRequest object | |
| locationRequest = LocationRequest.create(); | |
| // Use high accuracy | |
| locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); | |
| // Set the update interval to 5 seconds | |
| locationRequest.setInterval(UPDATE_INTERVAL); | |
| // Set the fastest update interval to 1 second | |
| locationRequest.setFastestInterval(FASTEST_INTERVAL); | |
| locationClient.requestLocationUpdates(locationRequest, locationListener); | |
| } | |
| @Override | |
| public void onDisconnected() { | |
| Log.d(TAG, "Location Callback. onDisconnected"); | |
| } | |
| }; | |
| GooglePlayServicesClient.OnConnectionFailedListener locationConnectionFailed = new GooglePlayServicesClient.OnConnectionFailedListener() { | |
| @Override | |
| public void onConnectionFailed(ConnectionResult connectionResult) { | |
| Log.d(TAG, "Location Callback. onConnectionFailed"); | |
| } | |
| }; | |
| LocationListener locationListener = new LocationListener() { | |
| @Override | |
| public void onLocationChanged(Location location) { | |
| Log.d(TAG, "onLocationChanged"); | |
| currentLocation = location; | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment