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

时间:2017-12-16 13:58:17

标签: java android location

我正在创建一个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();

        }
    }
}

如何在位置方法中将lattilongi的值分配给longitude方法中的latitudeoncreate

3 个答案:

答案 0 :(得分:0)

  

如何在该位置分配latti和longi的值   方法,WebView方法中的经度和纬度。

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

SMWebView

然后在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

答案 1 :(得分: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()

  4. 中获取latti和longi

答案 2 :(得分:0)

  1. 在MainActivity中声明latti和longi。就像我在这做的那样 代码。
  2. 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
    

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