一旦从转弯位置导航回来,如何获取坐标?

时间:2017-11-21 09:36:20

标签: java android

  

一旦从用于打开位置的意图返回,最好的方法是让getCoordinates方法运行。一旦你打开了位置,当你进行背压时,活动生命周期会发生什么,一旦你从打开位置返回后调用getCoordinates方法,或者甚至首先检查位置是否打开,最佳位置并且只有当它运行getCoordinates方法?

checkLocationOn

public void checkLocationOn () {
    LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;

    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch(Exception ex) {}

    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch(Exception ex) {}

    if(!gps_enabled && !network_enabled) {
        // notify user
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setCancelable(false);
        dialog.setTitle("Location Needed");
        dialog.setMessage("The devices location settings are not enabled, please enable it in order to use this application...");
        dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {

                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);

             }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();
    }

}

getCoordinates

 public void getCoordinates() {
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), 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.
        return;
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {

                     lat = location.getLatitude();
                     lng = location.getLongitude();

                    latitude = Double.toString(lat);
                    longitude = Double.toString(lng);

                    SharedPreferences.Editor e = preferences.edit();
                    e.putString("myLats",latitude);
                    e.putString("myLngs",longitude);
                    e.commit();

                }
            });
}

onCreateView

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.job_search_fragment, container, false);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());

        checkLocationOn();

        getCoordinates();

        //Please help here



    return v;
}

2 个答案:

答案 0 :(得分:0)

覆盖onResume并从那里调用checkLocationOn();getCoordinates();

如果您要求连续的位置更新,可以使用onPause方法删除请求。

答案 1 :(得分:0)

感谢arsena,我刚刚将checkLocationOn()更改为布尔值,然后在onResume()中更改,如果checkLocationOn()为true,那么我就像这样调用getCoordinate()。

public boolean checkLocationOn () {

    LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;

    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch(Exception ex) {}

    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch(Exception ex) {}
    if(!gps_enabled && !network_enabled) {
        // notify user
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setCancelable(false);
        dialog.setTitle("Location Needed");
        dialog.setMessage("The devices location settings are not enabled, please enable it in order to use this application...");
        dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);
            }
        });
        dialog.show();
        return false;
    }else{
        return true;
    }
}

然后在onResume()

@Override
public void onResume() {
    super.onResume();

    if(checkLocationOn()){
        getCoordinates();
    }
}

现在正在工作