Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BreakDownOnMaps extends FragmentActivity implements
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener,
- OnMapReadyCallback {
- protected GoogleApiClient mGoogleApiClient;
- private LocationRequest mLocationRequest = new LocationRequest();
- double currentLatitude;
- double currentLongitude;
- LatLng latLng;
- GoogleMap gMap;
- @TargetApi(Build.VERSION_CODES.M)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_break_down_on_maps);
- buildApi();
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- @Override
- protected void onStart() {
- super.onStart();
- mGoogleApiClient.connect();
- }
- @Override
- protected void onResume() {
- super.onResume();
- mGoogleApiClient.connect();
- }
- private void buildApi() {
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- }
- public void handleNewLocation(Location loc) {
- currentLatitude = loc.getLatitude();
- currentLongitude = loc.getLongitude();
- latLng = new LatLng(currentLatitude, currentLongitude);
- System.out.println("handleNewLocation ");
- MarkerOptions options = new MarkerOptions()
- .position(latLng)
- .title("I am here!");
- gMap.addMarker(options);
- gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- }
- @Override
- public void onConnected(Bundle bundle) {
- System.out.println("onConnected");
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
- && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- }
- Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if (location == null) {
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
- }
- else {
- handleNewLocation(location);
- };
- }
- @Override
- public void onConnectionSuspended(int i) {}
- @Override
- public void onLocationChanged(Location location) {
- handleNewLocation(location);
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {}
- @Override
- public void onMapReady(GoogleMap googleMap) {
- System.out.println("currentLatitude : " + currentLatitude);
- System.out.println("currentLongitude : " + currentLongitude);
- latLng = new LatLng(currentLatitude, currentLongitude);
- setgMap(googleMap);
- if(currentLatitude != 0 || currentLongitude != 0) {
- MarkerOptions options = new MarkerOptions()
- .position(latLng)
- .title("I am here!");
- googleMap.addMarker(options);
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- }
- }
- public void setgMap(GoogleMap gMap) {
- this.gMap = gMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement