Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get user location function. #1

Open
KaidiGuo opened this issue Jan 31, 2017 · 0 comments
Open

Get user location function. #1

KaidiGuo opened this issue Jan 31, 2017 · 0 comments

Comments

@KaidiGuo
Copy link
Owner

KaidiGuo commented Jan 31, 2017

LocationListener capture locationchange or other statue.

locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
            }
            public void onProviderDisabled(String provider) {
            }
            public void onProviderEnabled(String provider) {
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
};

LocationManager to get system user location service.
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

While, before call any function from LocationManager, we are asked to check user permission.
Here in the if (we do not have permission), we need to request permission from user.

 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) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            ActivityCompat.requestPermissions(this, LOCATION_PERMS , 1340);
            return;
        }

Once we got the permission, check permission pass, jump through this if, call LocationManager function allowed.

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();

As said above, if permission check failed, in the if function, we need to request permission from user.
Use ActivityCompat.requestPermissions(this, LOCATION_PERMS , 1340) function.
Here, 3 parameters according to Android help,

requestPermissions(Activity activity, String[] permissions, int requestCode)
Requests permissions to be granted to this application.

The second parameter String[] permissions has been defined in the basic class.
final String[] LOCATION_PERMS = {android.Manifest.permission.ACCESS_FINE_LOCATION};
The third parameter too.
final int LOCATION_REQUEST = 1340;
According to Android help,
(https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#requestPermissions)

once function requestPermissions been called, a pop up window will show up to ask for user permission.

If your app does not have the requested permissions the user will be presented with UI for accepting them. After the user has accepted or rejected the requested permissions you will receive a callback reporting whether the permissions were granted or not. Your activity has to implement ActivityCompat.OnRequestPermissionsResultCallback and the results of permission requests will be delivered to its onRequestPermissionsResult(int, String[], int[]) method.

After we call function requestPermissions , we override onRequestPermissionsResult outside
OnCreate(). If the result is "accept", set LocationManager, Location updates.

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode){
            case LOCATION_REQUEST:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, locationListener);
                    onClickStartNewActivity();//First time start : show second view
                } else {
                    Toast.makeText(this, "Location cannot be obtained due to " + "missing permission.", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

And open second Activity in order to renew first activity (so the if check will pass, the code after this if works. )

       final String position= "javascript:relocation("+latitude+","+longitude+")";
       final Button button = (Button) findViewById(R.id.locationbutton);
       button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                webView.loadUrl(position);
                //Toast.makeText(MapsActivity.this , position, Toast.LENGTH_LONG).show();
                // Perform action on click
            }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant