将变量从方法传递到onCreate方法中的另一个变量

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

我正在创建一个Android应用程序。目前,该位置是硬编码的。经度和纬度在oncreate方法中声明。我创建了一个方法来获取oncreate方法之外的经度和纬度。

这是oncreate方法

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

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

    //Doublin Co-ordinates
    final double latitude = 53.3498;
    final double longitude = -6.2603;

    //Getting th data
    getForecast(latitude, longitude);
    Log.d(Tag,"MAIN UI code running");
}

你可以看到位置是在latitudelatitude变量中硬编码的。

我还创建了一种方法来获取用户手机的最后已知坐标。这不是oncreate方法。

void getLocation() {
    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(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

    } else {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null){
            double latti = location.getLatitude();
            double longi = location.getLongitude();;
        } else {
            Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();

        }
    }
}

如何在latti方法中将位置方法中的longilongitude的值分配给latitudeoncreate

java android location
3个回答
0
投票

如何在位置方法中将latti和longi的值分配给oncreate方法中的经度和纬度。

我建议你将纬度和经度声明为全局变量(超出onCreate方法)

final double latitude = 53.3498;
final double longitude = -6.2603;

然后在getLocation功能

  if (location != null){
      double latti = latitude ;
      double longi = longitude;
   } else {
      Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
  }

或者你可以将这两个变量传递给getLocation方法。

//Doublin Co-ordinates
final double latitude = 53.3498;
final double longitude = -6.2603;
getLocation(latitude,longitude);

以这种方式修改getLocation函数

void getLocation(double latitude,double longitude) {
    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(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    } else {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null){
            double latti = latitude;
            double longi = longitude;
        } else {
            Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
        }
    }
}

0
投票
  1. 最好的方法是将字段声明为全局,然后将值分配给字段,然后在onCreate()或您想要的位置使用它。
  2. getLocation()返回一对实例 onCreate() { .... Pair<Double, Double> locationInfo = getLocation(); final double latitude = locationInfo.first; final double longitude = locationInfo.second; } Pair<Double, Double> getLocation(){ .... double latti = location.getLatitude(); double longi = location.getLongitude(); return new Pair<Double, Double>(latti, longi); }
  3. 你可以做的是从getLocation()方法的位置实例并获取onCreate()中的latti和longi

0
投票
  1. 在MainActivity中声明latti和longi。就像我在这段代码中所做的那样

2.在调用设置getLocation()latti值的方法longi之后,设置latitudelongitude的值。请看代码

请试试这段代码:

public class MainActivity extends Activity 

{
        double latti; //new code
        double longi; //new code

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

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

    //Doublin Co-ordinates
    final double latitude = latti; //i changed this line
    final double longitude = longi;//i changed this line

    //Getting th data
    getForecast(latitude, longitude);
    Log.d(Tag,"MAIN UI code running");
} //end of onCreate

void getLocation() {
    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(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

    } else {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null){
             latti = location.getLatitude();//i changed this line, (drooped the double)
             longi = location.getLongitude();//i changed this line, (drooped the double)
        } else {
            Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();

        }
    }
}

    } //end of MainActvity

另一种解决方案是吸气剂和孵化器。 (旁注:你不需要纬度和经度,你可以只使用latti和longi)

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