1

您好,我的项目有问题。我正在尝试获取用户位置,然后从 URL 获取一些数据。但问题是当我构建我的 Url 时,userLongitude 和 userLatitude 显示为零。有位置方法没有问题,因为我在 OnMapReady 部分而不是在 OnLoaderCreated 部分中获取用户位置。我在两者上都需要它们。

test1 textView 的结果为 0.0 0.0 ,而 test2 textView 给出了我的坐标。

你能建议一个解决方法吗?

public class MapActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<List<Locations>>, OnMapReadyCallback {

    public static final String LOG_TAG = MapActivity.class.getName();
    private static final String URL="https://maps.googleapis.com/maps/api/place/nearbysearch/json";
    private static final int LOCATION_LOADER_ID = 1;
    private GoogleMap map;
    private ProgressBar pb;
    private TextView loadingTV;
    private ArrayList<Locations> data;
    private double userLongitude;
    private double userLatitude;
    private FrameLayout mapView;
    FusedLocationProviderClient locationProviderClient;
    int PERMISSION_ID = 44;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        permissionRequest();
        locationProviderClient= LocationServices.getFusedLocationProviderClient(this);
        getLastLocation();
        pb = (ProgressBar) findViewById(R.id.progressBar);
        loadingTV = (TextView) findViewById(R.id.loadingTV);
        mapView=(FrameLayout) findViewById(R.id.map);
        mapView.setVisibility(View.GONE);
        ConnectivityManager cm =
                (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
        TextView noNetTV = (TextView) findViewById(R.id.noNetTV);
        if (!isConnected) {
            pb.setVisibility(View.GONE);
            loadingTV.setVisibility(View.GONE);
        } else {
            noNetTV.setVisibility(View.GONE);
            LoaderManager loaderManager = getLoaderManager();
            loaderManager.initLoader(LOCATION_LOADER_ID, null, this);
        }
    }

    private boolean checkPermissions() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        return false;
    }

    private void requestPermissions() {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSION_ID
        );
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_ID) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getLastLocation();
            }
        }
    }

    private void getLastLocation(){
        if (checkPermissions()) {
            if (isLocationEnabled()) {
                locationProviderClient.getLastLocation().addOnCompleteListener(
                        new OnCompleteListener<Location>() {
                            @Override
                            public void onComplete(@NonNull Task<Location> task) {
                                Location location = task.getResult();
                                if (location == null) {
                                    requestNewLocationData();
                                } else {
                                    userLatitude= location.getLatitude();
                                    userLongitude= location.getLongitude();
                                }
                            }
                        }
                );
            } else {
                Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        } else {
            requestPermissions();
        }
    }

    private boolean isLocationEnabled() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(
                LocationManager.NETWORK_PROVIDER
        );
    }

    @Override
    public void onResume(){
        super.onResume();
        if (checkPermissions()) {
            getLastLocation();
        }

    }

    private void requestNewLocationData() {

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(0);
        mLocationRequest.setFastestInterval(0);
        mLocationRequest.setNumUpdates(1);

        locationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        locationProviderClient.requestLocationUpdates(
                mLocationRequest, mLocationCallback,
                Looper.myLooper()
        );
    }

    private LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            Location mLastLocation = locationResult.getLastLocation();
            userLatitude= mLastLocation.getLatitude();
            userLongitude= mLastLocation.getLongitude();
        }
    };

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);
        LatLng user = new LatLng(userLatitude,userLongitude);
        **TextView test2=(TextView)findViewById(R.id.locations);
        test2.setText(""+userLatitude+"  "+userLongitude);**
        Iterator<Locations> iterator=data.iterator();
        while (iterator.hasNext()){
            Locations loc=iterator.next();
            LatLng marker=new LatLng(loc.getLatitude(),loc.getLongitude());
            Toast.makeText(this,"Works",Toast.LENGTH_LONG).show();
            map.addMarker(new MarkerOptions()
                    .position(marker)
                    .title(loc.getName()));
        }
        pb.setVisibility(View.GONE);
        loadingTV.setVisibility(View.GONE);
//        mapView.setVisibility(View.VISIBLE);
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(user, 13));
//            map.moveCamera(CameraUpdateFactory.newLatLng(user));
    }

    @Override
    public Loader<List<Locations>> onCreateLoader(int i, Bundle bundle) {

        String finalUrl=URL+"?"+"type=hospital"+"&key="+getString(R.string.google_maps_key)+"&radius=1500"+"&location="+userLatitude+","+userLongitude;
        **TextView test1=(TextView)findViewById(R.id.locations);
        test1.setText(""+userLatitude+"  "+userLongitude);**
        return new LocationAsyncTaskLoader(MapActivity.this,finalUrl);
    }

    @Override
    public void onLoadFinished(android.content.Loader<List<Locations>> loader, List<Locations> locat) {
        data=new ArrayList<>(locat.size());
        data.addAll(locat);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onLoaderReset(android.content.Loader<List<Locations>> loader) {
//        data.clear();
    }

    private void permissionRequest() {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED) {
        } else {
            ActivityCompat.requestPermissions(this, new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION },
                    1);
        }
    }


}
4

0 回答 0