我如何适应Android设备的经度?

问题描述 投票:0回答:2

因此,我正在制作这个应用程序,它可以使用JSoup库查找您附近的餐馆,并从食品配送应用程序中获取信息。唯一的问题是,有时纬度和经度会变为空值。我的应用程序正常工作的情况:-开启GPS并等待至少1-2分钟;-打开google地图,将其关闭,然后返回到应用程序;

所以主要问题:启用该位置并点击“查找餐厅”按钮后,我无法立即获取位置,启用位置后我需要等待1-2分钟,然后它才能正常工作。

    private TextView result;
    private FusedLocationProviderClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermission();
        client = LocationServices.getFusedLocationProviderClient(this);

       getBtn = findViewById(R.id.getRestaurants);
       result = findViewById(R.id.restaurantsList);

       getBtn.setOnClickListener(this);
    }



    private void requestPermission(){
        ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1 );
    }


public void onClick(View v) {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                return;
        }

        client.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                result.setText("Getting location...");
                if(location != null){
                double latitude = getLat(location);
                double longitude = getLng(location);

                    result.setText("Finding restaurants near you...");
                getWebsite(latitude, longitude);
                }else{
                    result.setText("Couldn't fetch location!");
                }

            }
            });
java android google-play-services android-location getlocation
2个回答
0
投票

您必须使用requestLocationUpdates()您正在使用getLastLocation(),并且在最后一个已知位置变为空后关闭GPS并打开GPS时,您必须致电requestLocationUpdates()您可以在下面的链接中找到更多信息

https://developer.android.com/training/location/receive-location-updates


0
投票

这是实现FusedLocationProvider的好方法:

private fun startLoc() {
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
        try {
            fusedLocationClient.lastLocation
                .addOnSuccessListener { location: Location? ->
                    //showDialog(this@MapsActivity, TAG, "last know loc = ${location?.latitude} + ${location?.longitude}")
                    if (location != null){
                        lastKnowLoc = LatLng(location.latitude, location.longitude)
                        addMarkerToLocation(lastKnowLoc)
                    }
                }
            val locationRequest = LocationRequest()
            locationRequest.interval = 10000
            locationRequest.fastestInterval = 10000
            locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

            locationCallback = object : LocationCallback() {
                override fun onLocationResult(locationResult: LocationResult?) {
                    locationResult ?: return
                    for (location in locationResult.locations){
                        //showDialog(this@MapsActivity, "locationResult", "location=${location.latitude};${location.longitude}")
                        addMarkerToLocation(LatLng(location.latitude, location.longitude))
                        val speed = location.speed
                        updateCamera(location.bearing)
                    }
                }
            }

            fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)

            btnStartLoc.isEnabled = false
            btnStopLoc.isEnabled = true
        }catch (e:SecurityException){}

    }
© www.soinside.com 2019 - 2024. All rights reserved.