Android:FusedLocationProvider,每隔几秒钟获取一次位置

时间:2019-07-16 11:11:57

标签: java android gps android-gps

我正在制作一个跟踪用户位置并将其最终上传到Firebase服务器的应用。

我创建了一个简单的应用,当我按下屏幕上的按钮时,该应用就会显示位置。问题是,当我走了几米后再次按下它时,它并不会改变位置(不过,当我重新输入该应用程序时,确实会得到一个新的位置)。我需要添加什么才能使应用每X秒更新一次位置?这是我的活动:

package com.example.gpstest;

import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class MainActivity extends AppCompatActivity {

    private FusedLocationProviderClient client;


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

        requestPermission();
        client = LocationServices.getFusedLocationProviderClient(this);
        Button button = findViewById(R.id.getLocation);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            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) {

                        if(location != null){
                            TextView lat = findViewById(R.id.location1);
                            TextView lon = findViewById(R.id.location2);
                            TextView alt = findViewById(R.id.location3);
                            lat.setText(Double.toString(location.getLatitude()));
                            lon.setText(Double.toString(location.getLongitude()));
                            alt.setText(Double.toString(location.getAltitude()));
                        }
                    }
                });
            }
        });
    }

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


3 个答案:

答案 0 :(得分:0)

因为您从getLastLocation获得位置。为此,您必须实现LocationCallback。

consider this link了解更多信息

答案 1 :(得分:0)

//在您的课程中实现这些接口  GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener。 //比使用此代码

public void onConnected(捆绑包){

    Location mLastLocation = null;
    if (mGoogleApiClient.isConnected()) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            //    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 precise.
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);

        if (mLastLocation != null) {
            latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
        }

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000); //5 seconds
        mLocationRequest.setFastestInterval(3000); //3 seconds
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    } else {
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

}

答案 2 :(得分:0)

我最终用这个视频解决了我的问题: https://youtu.be/4xcrZcnBk60