一旦LocationManger获得它,就显示纬度和经度

时间:2013-10-22 06:38:03

标签: android geolocation locationmanager

我正在开发紧急短信,无论何时访问活动,GPS都将启用,用户的位置将以文本视图显示,然后每当用户按下发送时,它将被发送到目标设备。现在,如果我正在使用Network_Provider,Textview将获得与我访问活动时一样快的纬度和经度,并且消息将显示在textview中。但是,GPS将显示空文本视图和空位置。

这是我的代码:

public class SMSmyLocation extends Activity implements OnClickListener, LocationListener{

LocationManager locationManager;
LocationListener locationListner;

Context ctx = SMSmyLocation.this;

TextView tv1, tv2;
Button b1;
EditText ed;

Geocoder geo;
List<Address> addresses;

String address = "";

boolean visibility = false;

double Long = 0,Lad = 0;

SharedPreferences sp;


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


    tv1 = (TextView) findViewById(R.id.ESMS);
    tv2 = (TextView) findViewById(R.id.currentLocation);


    b1 = (Button) findViewById(R.id.SendSMS);



    turnGPSOn();

    geo = new Geocoder(this, Locale.getDefault());

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


    if (addresses != null)
    {
        address = addresses.get(0).getAddressLine(0) + " " + addresses.get(0).getAddressLine(1) + " " + addresses.get(0).getAddressLine(2);
        tv1.setText(address);
    }
    else
        tv1.setText("fail");



    b1.setOnClickListener(this);
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    Log.e("LM", "Not null");

    double Lad = location.getLatitude();
    double Long = location.getLongitude();

    try {
        addresses = geo.getFromLocation(Lad, Long, 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
Log.e("Long", String.valueOf(Long));

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

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

}    
}

如何让系统等到LocationManger获取位置坐标后,如果可用,请以文本形式显示。线程是一个好主意,它是如何实现的?请提供您的答案的教程或代码。感谢您的时间和考虑。

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

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

    tv1 = (TextView) findViewById(R.id.ESMS);
    tv2 = (TextView) findViewById(R.id.currentLocation);

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    double Lad = location.getLatitude();
    double Long = location.getLongitude();

    tv2.setText(Lad+", "+Long);
}

主要是在onLocationChanged函数中设置要更新的textView。因此,当GPS位置进来时(是的,它可能需要很长时间),它将触发此功能并更新您的textView