GPS坐标未更新

时间:2017-01-18 19:00:20

标签: android gps

我正在构建跟踪行进距离的应用程序。目前,我无法显示坐标。代码运行,但是我设置为单击Start时显示坐标的Distance,单击时显示0.00。

public class MainActivity extends Activity {

    private Button historyButton, startButton, stopButton;
    private TextView Distance;
    double currentDistance;
    private SensorManager sensorManager;
    private Chronometer TimeRan;
    private LocationManager mLocManager;
    private LocationListener location;
    double longitude, latitude;

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

        //Initializing all buttons and textviews
        historyButton = (Button) findViewById(R.id.History);
        startButton = (Button) findViewById(R.id.Start);
        stopButton = (Button) findViewById(R.id.Stop);

        Distance = (TextView) findViewById(R.id.Distance);
        TimeRan = (Chronometer) findViewById(R.id.Time_Spent);

        mLocManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        location = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                currentDistance += .01;
                Distance.append("/n " + location.getLongitude() + " " + location.getLatitude());
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }

        };
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 100:
                configureButton();
                break;
            default:
                break;
        }
    }

    public void configureButton(){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
                        ,100);
            }
            return;
        }
        //Request update every minute or 16 meters
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ignore permission
                mLocManager.requestLocationUpdates("gps", 60000, 16, location);
            }
        });
    };

    public void GoToHistory(View v) {
        Intent history = new Intent(this, History.class);
        startActivity(history);
    }

    protected void EndWorkout(View v){
        TimeRan.stop();
        //ignore permission
        mLocManager.removeUpdates(location);
    }


    }

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stuyk.runningapplication">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <uses-feature android:name="android.hardware.sensor.stepcounter"/>
    <uses-feature android:name="android.hardware.sensor.stepdetector"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".History" />

    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

  locationManager.requestLocationUpdates(
        provider,
        MINIMUM_TIME_BETWEEN_UPDATES,
        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
        new MyLocationListener()
  );

您应该将MINIMUM_TIME_BETWEEN_UPDATES设置为您希望频繁更新的时间,并根据您的位置状态设置MINIMUM_DISTANCE_CHANGE_FOR_UPDATES距离更新的距离。