Search in sources :

Example 6 with LocationCallback

use of com.google.android.gms.location.LocationCallback in project PhoneProfilesPlus by henrichg.

the class LocationGeofenceEditorActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // must by called before super.onCreate() for PreferenceActivity
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        GlobalGUIRoutines.setTheme(this, false, true, false);
    else
        GlobalGUIRoutines.setTheme(this, false, false, false);
    GlobalGUIRoutines.setLanguage(getBaseContext());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_geofence_editor);
    mResultReceiver = new AddressResultReceiver(new Handler(getMainLooper()));
    // Create a GoogleApiClient instance
    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    mLocationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            PPApplication.logE("LocationGeofenceEditorActivity.LocationCallback", "xxx");
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                mLastLocation = location;
                PPApplication.logE("LocationGeofenceEditorActivity.LocationCallback", "location=" + location);
                if (mLocation == null) {
                    mLocation = new Location(mLastLocation);
                    refreshActivity(true);
                } else
                    updateEditedMarker(false);
            }
        }
    };
    createLocationRequest();
    mResolvingError = savedInstanceState != null && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);
    Intent intent = getIntent();
    geofenceId = intent.getLongExtra(LocationGeofencePreference.EXTRA_GEOFENCE_ID, 0);
    if (geofenceId > 0) {
        geofence = DatabaseHandler.getInstance(getApplicationContext()).getGeofence(geofenceId);
        mLocation = new Location("LOC");
        mLocation.setLatitude(geofence._latitude);
        mLocation.setLongitude(geofence._longitude);
    }
    if (geofence == null) {
        geofenceId = 0;
        geofence = new Geofence();
        geofence._name = getString(R.string.event_preferences_location_new_location_name) + "_" + String.valueOf(DatabaseHandler.getInstance(getApplicationContext()).getGeofenceCount() + 1);
        geofence._radius = 100;
    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.location_editor_map);
    mapFragment.getMapAsync(this);
    radiusLabel = findViewById(R.id.location_pref_dlg_radius_seekbar_label);
    SeekBar radiusSeekBar = findViewById(R.id.location_pref_dlg_radius_seekbar);
    radiusSeekBar.setProgress(Math.round(geofence._radius / (float) 20.0) - 1);
    radiusSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            geofence._radius = (progress + 1) * 20;
            updateEditedMarker(false);
        // Log.d("LocationGeofenceEditorActivity.onProgressChanged", "radius="+geofence._radius);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
    geofenceNameEditText = findViewById(R.id.location_editor_geofence_name);
    geofenceNameEditText.setText(geofence._name);
    addressText = findViewById(R.id.location_editor_address_text);
    okButton = findViewById(R.id.location_editor_ok);
    okButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String name = geofenceNameEditText.getText().toString();
            if ((!name.isEmpty()) && (mLocation != null)) {
                geofence._name = name;
                geofence._latitude = mLocation.getLatitude();
                geofence._longitude = mLocation.getLongitude();
                if (geofenceId > 0) {
                    DatabaseHandler.getInstance(getApplicationContext()).updateGeofence(geofence);
                } else {
                    DatabaseHandler.getInstance(getApplicationContext()).addGeofence(geofence);
                /*synchronized (PPApplication.geofenceScannerMutex) {
                            // start location updates
                            if ((PhoneProfilesService.instance != null) && PhoneProfilesService.isGeofenceScannerStarted())
                                PhoneProfilesService.getGeofencesScanner().connectForResolve();
                        }*/
                }
                DatabaseHandler.getInstance(getApplicationContext()).checkGeofence(String.valueOf(geofence._id), 1);
                Intent returnIntent = new Intent();
                returnIntent.putExtra(LocationGeofencePreference.EXTRA_GEOFENCE_ID, geofence._id);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        }
    });
    Button cancelButton = findViewById(R.id.location_editor_cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent returnIntent = new Intent();
            setResult(Activity.RESULT_CANCELED, returnIntent);
            finish();
        }
    });
    AppCompatImageButton myLocationButton = findViewById(R.id.location_editor_my_location);
    myLocationButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mLastLocation != null)
                mLocation = new Location(mLastLocation);
            refreshActivity(true);
        }
    });
    addressButton = findViewById(R.id.location_editor_address_btn);
    addressButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getGeofenceAddress();
        }
    });
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) SeekBar(android.widget.SeekBar) LocationCallback(com.google.android.gms.location.LocationCallback) Handler(android.os.Handler) Intent(android.content.Intent) AppCompatImageButton(android.support.v7.widget.AppCompatImageButton) View(android.view.View) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) LocationResult(com.google.android.gms.location.LocationResult) SupportMapFragment(com.google.android.gms.maps.SupportMapFragment) Button(android.widget.Button) AppCompatImageButton(android.support.v7.widget.AppCompatImageButton) Location(android.location.Location)

Example 7 with LocationCallback

use of com.google.android.gms.location.LocationCallback in project HikingApp by wickhama.

the class Tracking method onCreate.

@Override
public void onCreate() {
    // Asks for permission
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        stopSelf();
    }
    trail = new ArrayList();
    flocatClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
    locationRequest.setInterval(1000);
    locationRequest.setFastestInterval(5000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationCallback = new LocationCallback() {

        private Intent intent = new Intent();

        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            intent.setAction(LOCATION_FOUND);
            for (Location location : locationResult.getLocations()) {
                trail.add(new LatLng(location.getLatitude(), location.getLongitude()));
                intent.putExtra("location", trail);
                sendBroadcast(intent);
            }
        }
    };
}
Also used : LocationCallback(com.google.android.gms.location.LocationCallback) ArrayList(java.util.ArrayList) Intent(android.content.Intent) LatLng(com.google.android.gms.maps.model.LatLng) LocationResult(com.google.android.gms.location.LocationResult) Location(android.location.Location)

Example 8 with LocationCallback

use of com.google.android.gms.location.LocationCallback in project GogoNew by kuldeep725.

the class MapsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    progressDialog = new ProgressDialog(this);
    floatingButton = (FloatingActionButton) findViewById(R.id.pick_me);
    flag2 = 1;
    // broadCast = new NetworkChangeReceiver(new MapsActivity());
    Intent i = new Intent(this, NetworkChangeReceiver.class);
    sendBroadcast(i);
    // registerReceiver(broadCast, new IntentFilter("INTERNET_LOST"));
    // FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    // fab.setOnClickListener(new View.OnClickListener() {
    // @Override
    // public void onClick(View view) {
    // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    // .setAction("Action", null).show();
    // }
    // });
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    distance = (TextView) findViewById(R.id.distance);
    duration = (TextView) findViewById(R.id.time);
    mDatabase = FirebaseDatabase.getInstance().getReference();
    mRequestingLocationUpdates = false;
    if (checkPermissions()) {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        Log.d(TAG, "onCreate ...............................");
        createLocationRequest();
        // show error dialog if GoolglePlayServices not available
        // if (!isGooglePlayServicesAvailable()) {
        // finish();
        // }
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        // mFusedLocationClient is actually responsible for mCurrentLocation
        mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {

            @Override
            public void onSuccess(Location location) {
                // Got last known location. In some rare situations this can be null.
                if (location != null) {
                    mCurrentLocation = location;
                    Log.d(TAG, "@mFusedLocationClient -> mCurrentLocation = " + mCurrentLocation.toString());
                    onMapReady(mMap);
                // if (checkBusSelection != 0 && flag2 != 0) {
                // makeMarkerOnTheLocation();
                // showMarkers();
                // showDistanceInBetween();
                // flag2 = 0;
                // }
                }
            }
        });
        // brilliant function keeps checking the change in the location and update it in the interval set by us in createLocationRequest
        mLocationCallback = new LocationCallback() {

            @Override
            public void onLocationResult(LocationResult locationResult) {
                for (Location location : locationResult.getLocations()) {
                    mCurrentLocation = location;
                    // Log.d(TAG, "mLocationCallback mCurrentLocation= "+ mCurrentLocation.toString());
                    showInternetStatus();
                }
            // showMyLocationMarker();
            }
        };
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setupWindowAnimations();
    }
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) NavigationView(android.support.design.widget.NavigationView) LocationCallback(com.google.android.gms.location.LocationCallback) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) LocationResult(com.google.android.gms.location.LocationResult) SupportMapFragment(com.google.android.gms.maps.SupportMapFragment) DrawerLayout(android.support.v4.widget.DrawerLayout) Toolbar(android.support.v7.widget.Toolbar) Location(android.location.Location)

Aggregations

LocationCallback (com.google.android.gms.location.LocationCallback)8 LocationResult (com.google.android.gms.location.LocationResult)8 Location (android.location.Location)7 Intent (android.content.Intent)3 Handler (android.os.Handler)3 LocationRequest (com.google.android.gms.location.LocationRequest)3 HandlerThread (android.os.HandlerThread)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 SupportMapFragment (com.google.android.gms.maps.SupportMapFragment)2 SuppressLint (android.annotation.SuppressLint)1 NotificationChannel (android.app.NotificationChannel)1 ProgressDialog (android.app.ProgressDialog)1 Message (android.os.Message)1 NavigationView (android.support.design.widget.NavigationView)1 DrawerLayout (android.support.v4.widget.DrawerLayout)1 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 AppCompatImageButton (android.support.v7.widget.AppCompatImageButton)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 Button (android.widget.Button)1