【问题标题】:How to use updated gps location data in main android activity?如何在主要的 android 活动中使用更新的 gps 位置数据?
【发布时间】:2011-07-27 15:12:53
【问题描述】:

我在谷歌上搜索了很多问题和论坛,但找不到答案,所以我希望这个问题不要重复。如果是这样,请引导我回答主要问题。

好吧,我不知道如何在 android 中更新 gps 位置,然后在主要活动中实际使用这个值。所以我有一个由两个 TextView 组成的布局,一个显示经度,另一个显示纬度。我已经设置了一个 LocationManager、LocationListener(用于更新位置),然后按照本教程中的说明在我的 LocationManager 上调用 requestLocationUpdates() http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

我使用与上述教程相同的 onCreate 和 LocationListener 方法。正如您所看到的 onLocationChanged() 在本教程中祝酒,但我想更改我的 TextViews 的文本。您能否告诉我如何在我的主要活动中使用 getLongitude()(和类似方法)中的值。

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = “My current location is: “ +

“Latitud = “ + loc.getLatitude() +

“Longitud = “ + loc.getLongitude();

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();

}

【问题讨论】:

    标签: android gps location


    【解决方案1】:

    在您的主要活动中,在您需要 onCreatesetContentView 之后

    EditText lat = new EditText findViewById(R.id.lat_view);
    EditText lon = new EditText findViewById(R.id.lon_view);
    

    (或您在 xml 中调用的任何经纬度文本视图)

    然后在你的onLocationChanged 添加

    lat.setText(loc.getLatitude);
    lon.setText(loc.getLongitude);
    

    您可能需要将EditTexts 设为静态,我不在 eclipse atm 前面。

    【讨论】:

      【解决方案2】:

      从您的主要活动中传递 MyLocationListener 构造函数中的文本视图(参考您使用的教程)。所以基本上您必须在 MyLocationListener 中定义一个构造函数

      public class MyLocationListener implements LocationListener {
         Context context;
         TextView tv;      
      
         public MyLocationListener(Context context,TextView tv){
          this.context=context;
          this.tv=tv;
         }
      
        public void onLocationChanged(Location loc) {
            tv.setText("your text comes here");
        }      
      
      
      } 
      

      在主要活动中

      TextView tv = (TextView) findViewById(R.id.yourTextViewId);
      LocationListener mlocListener = new MyLocationListener(getApplicationContext(),tv);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-09
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        • 2013-07-25
        • 2016-03-12
        相关资源
        最近更新 更多