java 读gps_java – 如何获取当前的GPS位置?

经过一番研究后,我想出了这个:

public class UseGps extends Activity

{

Button gps_button;

TextView gps_text;

LocationManager mlocManager;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

gps_button = (Button) findViewById(R.id.GPSButton);

gps_text = (TextView) findViewById(R.id.GPSText);

gps_button.setOnClickListener(new OnClickListener() {

public void onClick(View viewParam) {

gps_text.append("\n\nSearching for current location. Please hold...");

gps_button.setEnabled(false);

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,mlocListener);

}

});

}

/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

double lon = loc.getLatitude();

double lat = loc.getLongitude();

gps_text.append("\nLongitude: "+lon+" - Latitude: "+lat);

UseGps.this.mlocManager.removeUpdates(this);

gps_button.setEnabled(true);

}

@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

}

}

}

这会使用按钮和textview设置活动.在启动位置管理器的按钮上设置监听器.

我已经设置了一个实现LocationListener的类MyLocationListener,然后我重写了onLocationChanged()方法,基本上告诉它它获取的第一个位置附加到textview然后它删除了位置管理器.

感谢那些帮助过的人,我希望这对其他人有用.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值