Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. public class BreakDownOnMaps extends FragmentActivity implements
  2.         GoogleApiClient.ConnectionCallbacks,
  3.         GoogleApiClient.OnConnectionFailedListener,
  4.         LocationListener,
  5.         OnMapReadyCallback {
  6.     protected GoogleApiClient mGoogleApiClient;
  7.     private LocationRequest mLocationRequest =  new LocationRequest();
  8.     double currentLatitude;
  9.     double currentLongitude;
  10.     LatLng latLng;
  11.     GoogleMap gMap;
  12.  
  13.  
  14.     @TargetApi(Build.VERSION_CODES.M)
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_break_down_on_maps);
  19.         buildApi();
  20.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  21.                 .findFragmentById(R.id.map);
  22.         mapFragment.getMapAsync(this);
  23.     }
  24.  
  25.     @Override
  26.     protected void onStart() {
  27.         super.onStart();
  28.         mGoogleApiClient.connect();
  29.     }
  30.     @Override
  31.     protected void onResume() {
  32.         super.onResume();
  33.         mGoogleApiClient.connect();
  34.     }
  35.  
  36.     private void buildApi() {
  37.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  38.                 .addConnectionCallbacks(this)
  39.                 .addOnConnectionFailedListener(this)
  40.                 .addApi(LocationServices.API)
  41.                 .build();
  42.     }
  43.  
  44.     public void handleNewLocation(Location loc) {
  45.         currentLatitude = loc.getLatitude();
  46.         currentLongitude = loc.getLongitude();
  47.         latLng = new LatLng(currentLatitude, currentLongitude);
  48.         System.out.println("handleNewLocation ");
  49.         MarkerOptions options = new MarkerOptions()
  50.                 .position(latLng)
  51.                 .title("I am here!");
  52.         gMap.addMarker(options);
  53.         gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  54.     }
  55.  
  56.     @Override
  57.     public void onConnected(Bundle bundle) {
  58.         System.out.println("onConnected");
  59.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
  60.                 && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  61.         }
  62.         Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  63.         if (location == null) {
  64.             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
  65.         }
  66.         else {
  67.             handleNewLocation(location);
  68.         };
  69.     }
  70.  
  71.     @Override
  72.     public void onConnectionSuspended(int i) {}
  73.  
  74.     @Override
  75.     public void onLocationChanged(Location location) {
  76.         handleNewLocation(location);
  77.     }
  78.  
  79.     @Override
  80.     public void onConnectionFailed(ConnectionResult connectionResult) {}
  81.  
  82.     @Override
  83.     public void onMapReady(GoogleMap googleMap) {
  84.         System.out.println("currentLatitude : " + currentLatitude);
  85.         System.out.println("currentLongitude : " + currentLongitude);
  86.         latLng = new LatLng(currentLatitude, currentLongitude);
  87.         setgMap(googleMap);
  88.         if(currentLatitude != 0 || currentLongitude != 0) {
  89.             MarkerOptions options = new MarkerOptions()
  90.                     .position(latLng)
  91.                     .title("I am here!");
  92.             googleMap.addMarker(options);
  93.             googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  94.         }
  95.     }
  96.  
  97.     public void setgMap(GoogleMap gMap) {
  98.         this.gMap = gMap;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement