<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
public class MapsActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {}
protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build();}
@Overridepublic void onConnected(Bundle bundle) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), MAP_ZOOM_LEVEL));}
public static final long UPDATE_INTERVAL_IN_MS = 120000; public static final long FASTEST_UPDATE_INTERVAL_IN_MS =UPDATE_INTERVAL_IN_MS / 4;protected LocationRequest mLocationRequest;
private void startLogging() { mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MS); mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MS); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) { mRequestingLocationUpdates = true; startLocationUpdates(); }} protected void startLocationUpdates() { LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this);}
@Overridepublic void onLocationChanged(Location location) { mCurrentLocation = location; DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = new Date(); mLastUpdateTime = dateFormat.format(date).toString(); saveToFirebase(); // Retrieve saved locations and draw as marker on map drawLocations(); // Update UI to draw bread crumb with the latest bus location. mMap.clear(); LatLng mLatlng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); MarkerOptions mMarkerOption = new MarkerOptions() .position(mLatlng) .title(mLastUpdateTime)) .icon(BitmapDescriptorFactory.fromResource(R.drawable.code_the_road_small)); Marker mMarker = mMap.addMarker(mMarkerOption); }
myFirebaseRef = new Firebase("<YOUR-FIREBASE-APP>");
private void saveToFirebase() { Map mLocations = new HashMap(); mLocations.put("timestamp", mLastUpdateTime); Map mCoordinate = new HashMap(); mCoordinate.put(“latitude”, mCurrentLocation.getLatitude()); mCoordinate.put(“longitude”, mCurrentLocation.getLongitude()); mLocations.put("location", mCoordinate); myFirebaseRef.push().setValue(mLocations);}
private void drawLocations() { // Get only latest logged locations - since 'START' button clicked Query queryRef = myFirebaseRef.orderByChild("timestamp").startAt(startLoggingTime); // Add listener for a child added at the data at this location queryRef.addChildEventListener(new ChildEventListener() { LatLngBounds bounds; LatLngBounds.Builder builder = new LatLngBounds.Builder(); @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { Map data = (Map ) dataSnapshot.getValue(); String timestamp = (String) data.get("timestamp"); // Get recorded latitude and longitude Map mCoordinate = (HashMap)data.get("location"); double latitude = (double) (mCoordinate.get("latitude")); double longitude = (double) (mCoordinate.get("longitude")); // Create LatLng for each locations LatLng mLatlng = new LatLng(latitude, longitude); // Make sure the map boundary contains the location builder.include(mLatlng); bounds = builder.build(); // Add a marker for each logged location MarkerOptions mMarkerOption = new MarkerOptions() .position(mLatlng) .title(timestamp) .icon(BitmapDescriptorFactory.fromResource(R.drawable.measle_blue)); Marker mMarker = mMap.addMarker(mMarkerOption); markerList.add(mMarker); // Zoom map to the boundary that contains every logged location mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_ZOOM_LEVEL)); } //Override other abstract methods for addChildEventListener below … }); }
Give us feedback in our Product Forums.