更新Android版GPS定位源代码

2011年我写的这个程序做了一些简单的改动,增加了卫星数量和定位卫星数量的显示,修改了定位精度,使手机通AGPS定位能更快一点

新的下载地址:http://download.csdn.net/detail/alimyso/5256097

同时感谢mars和安卓巴士开发网对我软件青睐


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,以下是一个简单的 Android GPS 定位的完整代码示例: 1. 在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> ``` 2. 创建一个新的 Activity 类,并在其中添加以下代码: ```java public class MainActivity extends AppCompatActivity implements LocationListener { private LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 LocationManager 实例 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 检查是否已经授权了位置访问权限 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 如果没有授权,向用户请求授权 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); return; } // 注册位置监听器 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Override public void onLocationChanged(Location location) { // 当位置发生变化时,更新 UI 显示 TextView latitudeTextView = findViewById(R.id.latitudeTextView); TextView longitudeTextView = findViewById(R.id.longitudeTextView); latitudeTextView.setText(String.valueOf(location.getLatitude())); longitudeTextView.setText(String.valueOf(location.getLongitude())); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 如果用户授权了位置访问权限,注册位置监听器 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } } } } ``` 3. 在布局文件中添加两个 TextView 控件,用于显示经纬度信息: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/latitudeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Latitude: " /> <TextView android:id="@+id/longitudeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Longitude: " /> </LinearLayout> ``` 这样就完成了一个简单的 Android GPS 定位的完整代码示例。在运行应用程序时,用户首先需要授权位置访问权限,然后应用程序就会启动 GPS 定位,并在位置发生变化时更新经纬度信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值