android - 如何将公共(public)方法变量的值设置为 protected 方法中的变量?

标签 android variables location oncreate

我有一种获取设备当前位置的方法,我想将其值设置为分配给 onCreate 中的 LatLng 变量。我确信这是一个简单的修复,但我想不出解决方案.有人可以为此提供解决方案吗?

在下面的代码中更容易解释这一点:

protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.journey_planner);

  //Get direction between from and to positions
  //want to set fromposition to point to the values of lat and long in below method onCreate

  LatLng fromPosition = new LatLng(13.687140112679154, 100.53525868803263);
  LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);   
}

@Override
public void onLocationChanged(Location location) {
  int lat = (int) (location.getLatitude());
  int lng = (int) (location.getLongitude());
}

最佳答案

如果我对这个问题的理解正确,你会希望将你的 lat 和 lng 设为像@Clay 所说的类变量

int lat;
int lng;

然后你可以设置你的fromPosition

protected void onCreate(Bundle savedInstanceState) {
...
LatLng fromPosition = new LatLng(lat, lng);
...
}

关于android - 如何将公共(public)方法变量的值设置为 protected 方法中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21268778/

相关文章:

C++:变量与引用?

android - 未应用更改 CircularProgressIndicator 宽度或高度

android - 如何从在线数据库获取最新的最后一行并每 10 秒刷新一次(Kinvey)

java - 如何让我的应用程序作为后台进程运行

javascript - 使用 JavaScript 使用 for 循环声明变量

php - 在 javascript 文件错误中使用 php var

wpf - Bing map - 图钉的自动更新位置绑定(bind)

PHP 纬度经度地址

android - GPS 关闭时 Kotlin allCellInfo 返回 null

android - RecyclerView 是否需要 Gradle 依赖项?