package info.androidhive.materialdesign.activity; import android.content.Intent; import android.graphics.Color; import android.location.Criteria; import android.location.Location; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.PendingResult; import com.google.android.gms.common.api.Status; 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.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.PolylineOptions; import java.text.DateFormat; import java.util.Date; import info.androidhive.materialdesign.R; //import com.google.maps.android.ui.IconGenerator; public class MapActivity extends AppCompatActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private static final String TAG = "MapActivity"; private static final long INTERVAL = 1000 * 30 * 1; //1 minute private static final long FASTEST_INTERVAL = 1000 * 30 * 1; // 1 minute Button btnFusedLocation; TextView tvLocation; LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; Location mCurrentLocation; String mLastUpdateTime; GoogleMap googleMap; double lat1,long1; private static final LatLng LOWER_MANHATTAN = new LatLng(+29.949445, 76.815710 ); private static final LatLng TIMES_SQUARE = new LatLng(29.949445, 76.815710); private static final LatLng BROOKLYN_BRIDGE = new LatLng(29.961051, 76.826355); protected void createLocationRequest() { mLocationRequest = new LocationRequest(); // mLocationRequest.setInterval(INTERVAL); //mLocationRequest.setFastestInterval(FASTEST_INTERVAL); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate ..............................."); //show error dialog if GoolglePlayServices not available if (!isGooglePlayServicesAvailable()) { finish(); } createLocationRequest(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); ////////////////////////////////// Criteria criteria = new Criteria(); setContentView(R.layout.activity_map); SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); googleMap = fm.getMap(); googleMap.getUiSettings().setZoomControlsEnabled(true); // addLines(); } @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart fired .............."); mGoogleApiClient.connect(); } private void addLines() { googleMap .addPolyline((new PolylineOptions()) .add(TIMES_SQUARE, BROOKLYN_BRIDGE, LOWER_MANHATTAN, TIMES_SQUARE).width(5).color(Color.BLUE) .geodesic(true)); // move camera to zoom on map googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOWER_MANHATTAN, 13)); } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop fired .............."); mGoogleApiClient.disconnect(); Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected()); } private boolean isGooglePlayServicesAvailable() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (ConnectionResult.SUCCESS == status) { return true; } else { GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); return false; } } @Override public void onConnected(Bundle bundle) { Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); startLocationUpdates(); } protected void startLocationUpdates() { PendingResult pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); Log.d(TAG, "Location update started ..............: "); } @Override public void onConnectionSuspended(int i) { } public void onClick15(View view) { Intent i = new Intent(this,confirmation_activity.class); startActivity(i); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Log.d(TAG, "Connection failed: " + connectionResult.toString()); } private void addMarker1() { MarkerOptions options = new MarkerOptions(); // following four lines requires 'Google Maps Android API Utility Library' // https://developers.google.com/maps/documentation/android/utility/ // I have used this to display the time as title for location markers // you can safely comment the following four lines but for this info // IconGenerator iconFactory = new IconGenerator(this); // iconFactory.setStyle(IconGenerator.STYLE_PURPLE); // options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime))); // options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV()); lat1 =Double.parseDouble(HomeFragment.latitude); long1=Double.parseDouble(HomeFragment.longitude); LatLng currentLatLng = new LatLng(lat1,long1); options.position(currentLatLng); Marker mapMarker = googleMap.addMarker(options); long atTime = mCurrentLocation.getTime(); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime)); mapMarker.setTitle("RAVeN assist"); Log.d(TAG, "Marker added............................."); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 13)); Log.d(TAG, "Zoom done............................."); } @Override public void onLocationChanged(Location location) { Log.d(TAG, "Firing onLocationChanged.............................................."); mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); addMarker(); addMarker1(); } private void addMarker() { MarkerOptions options = new MarkerOptions(); // following four lines requires 'Google Maps Android API Utility Library' // https://developers.google.com/maps/documentation/android/utility/ // I have used this to display the time as title for location markers // you can safely comment the following four lines but for this info // IconGenerator iconFactory = new IconGenerator(this); // iconFactory.setStyle(IconGenerator.STYLE_PURPLE); // options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime))); // options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV()); LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); options.position(currentLatLng); Marker mapMarker = googleMap.addMarker(options); long atTime = mCurrentLocation.getTime(); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime)); mapMarker.setTitle("User"+mLastUpdateTime); Log.d(TAG, "Marker added............................."); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 13)); Log.d(TAG, "Zoom done............................."); } @Override protected void onPause() { super.onPause(); stopLocationUpdates(); } protected void stopLocationUpdates() { LocationServices.FusedLocationApi.removeLocationUpdates( mGoogleApiClient, this); Log.d(TAG, "Location update stopped ......................."); } @Override public void onResume() { super.onResume(); if (mGoogleApiClient.isConnected()) { startLocationUpdates(); Log.d(TAG, "Location update resumed ....................."); } } }