将纬度/经度保存到变量

时间:2016-12-11 16:11:37

标签: java android android-studio

我使用位置管理器来获取当前位置。接下来我要计算两点的距离。

Befor onCreate:

double clientLatitude, clientLongitude;
TextView v1,v2,v3;

在onCreate()中:

    v1 = (TextView) this.findViewById(R.id.textView2);
    v2 = (TextView) this.findViewById(R.id.textView5);
    v3 = (TextView) this.findViewById(R.id.textView);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            2000, 1, (LocationListener) this);

在我的onLocationChanged(),onProviderDisabled(),onProviderEnabled()和onStatusChanged()方法中:

@Override
public void onLocationChanged(Location location) {
    String msg = "New Latitude: " + location.getLatitude()
            + "New Longitude: " + location.getLongitude();
    clientLatitude = location.getLatitude();
    clientLongitude = location.getLongitude();

    Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
}

@Override
public void onProviderDisabled(String provider) {
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
    Toast.makeText(getBaseContext(), "Gps OFF!! ",
            Toast.LENGTH_SHORT).show();
}


@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(getBaseContext(), "Gps ON!! ",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

afterConnect()

                float distance=0;
                Location crntLocation=new Location("crntlocation");
                crntLocation.setLatitude(clientLatitude);
                crntLocation.setLongitude(clientLongitude);

                Location newLocation=new Location("newlocation");
                newLocation.setLatitude(eventLatitudeS);
                newLocation.setLongitude(eventLongitudeS);

                distance =crntLocation.distanceTo(newLocation) / 1000;

                v1.setText(String.valueOf(clientLongitude));
                v2.setText(String.valueOf(clientLatitude));
                v3.setText(String.valueOf(distance));

我将变量clientLatitude和clientLongitude声明为onCreate()方法作为全局变量。当我想吐司当前的Lat / Lng一切都很好。但是当我想将Lat / Lng保存到clientLatitude和clientLongitude中并稍后显示它时,例如在textView中,我总是得到" 0"。

你能告诉我这个请求的原因吗?

0 个答案:

没有答案