安卓开发----->GPS定位

本文介绍了一种在Android应用中实现GPS定位的方法。首先在配置清单文件中添加使用GPS的权限,然后在布局文件中添加Button和TextView用于触发定位并显示结果。通过LocationManager获取位置信息,并设置监听器实时更新经纬度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安卓开发中需要用到GPS定位功能,我自己写了一些代码基本实现了这个功能,下面和大家分享一下

1、添加用户权限:在配置清单文件中添加用户权限使用系统的GPS功能

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2、在布局文件中添加一个Button和一个TextView(用来显示结果)

<Button 
        android:id="@+id/Dingwei"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Dingwei"/>
    <TextView
        android:id="@+id/txtIfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Dingwei"/>


3、在主代码中添加相关的代码块

public class MainActivity extends Activity {
	//获取Button对象
	Button btnGps;
	//获取TextView对象
	TextView tv;
	//获得系统服务的LocatinManager对象
	LocationManager locationManager;
	//获得系统服务
	String contextServer=Context.LOCATION_SERVICE;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取Button对象实例
		btnGps=(Button)findViewById(R.id.Dingwei);
		//获取tv对象实例
		tv=(TextView)findViewById(R.id.txtIfo);
		//设置BtnGps的单击事件
		
		btnGps.setOnClickListener(new  myClickListener());
		
	}

	//定位按钮btnGps 的单击事件 (implements继承了View.OnClickListener)
	private class myClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			//通过系统服务,取得LocationManager对象
			locationManager=(LocationManager)getSystemService(contextServer);
			//通过GPS位置提供器获得位置
			//String provider=LocationManager.GPS_PROVIDER;
			//Location location =locationManager.getLastKnownLocation(provider);
			Criteria criteria = new Criteria();
			criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
			criteria.setAltitudeRequired(false);//不要求海拔
			criteria.setBearingRequired(false);//不要求方位
			criteria.setCostAllowed(true);//允许有花费
			criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
			//从可用的位置提供器中,匹配以上标准的最佳提供器
			String provider = locationManager.getBestProvider(criteria, true);
			//获得最后一次变化的位置
			Location location = locationManager.getLastKnownLocation(provider);

			//获取经度和纬度
			double alt=location.getLatitude();
			double lng=location.getLongitude();
			StringBuilder builder=new StringBuilder();
			builder.append("您的位置是:\n");
			builder.append("经度:\n");
			builder.append(alt+"\n");
			builder.append("纬度\n");
			builder.append(lng);
			tv.setText(builder);
			//监听位置变化,2秒钟一次,距离10米以上
			locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);	
		}
		
	}
	private final LocationListener locationListener=new LocationListener() {
		//位置状态发生改变时调用
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO 自动生成的方法存根
			
		}
		//当位置提供器可用时调用
		@Override
		public void onProviderEnabled(String provider) {
			
		}
		//当位置提供器不可用时调用
		@Override
		public void onProviderDisabled(String provider) {
			
		}
		//当位置发生变化时调用
		@Override
		public void onLocationChanged(Location location) {
			updataWithNewLocation(location);
		}
		private void updataWithNewLocation(Location location) {
			double alt=location.getLatitude();
			double lng=location.getLongitude();
			StringBuilder builder=new StringBuilder();
			builder.append("您的位置是:\n");
			builder.append("经度:\n");
			builder.append(alt+"\n");
			builder.append("纬度\n");
			builder.append(lng);
			tv.setText(builder);
		}
	};
	

}

4、将应用布置到安卓模拟器上得到最终效果:



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值