700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

时间:2022-10-27 00:52:44

相关推荐

Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

文章目录

Android定位(经纬度+当前位置信息)申请权限LocationManage位置管理器完整代码

Android定位(经纬度+当前位置信息)

我相信大家在Android开发中应该都有遇到过需要获取经纬度和当前位置信息的情况!

本人目前也还处于学习Android阶段,想做一下关于Android定位的笔记。

以下是本人通过一个下午的时间找到最新可以获取定位信息的方法。希望对大家也有帮助。

申请权限

<!-- 网络权限 --><uses-permission android:name="android.permission.INTERNET" /><!-- 用于进行网络定位 --><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!-- 用于访问GPS定位 --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><!-- 用于获取wifi的获取权限,wifi信息会用来进行网络定位 --><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

LocationManage位置管理器

通过LocatioManage去获取到当前的位置管理器

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

使用locationManager.requestLocationUpdates()方法注意

在方法中使用方法头部带上@SuppressLint("MissingPermission")或者放在判断是否有使用定位的权限语句的后面if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){}locationManager.requestLocationUpdates(选中定位模式, 多久获取一次(毫秒为单位), 移动距离多远获取一次(米为单位), 传一个LocationListener监听);

// 表示移除LocationManage(位置管理器)locationManager.removeUpdates(传入实现过LocationListener的类);

完整代码

MainActivity类的代码

import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.ActivityCompat;import android.Manifest;import android.annotation.SuppressLint;import android.content.Context;import android.content.pm.PackageManager;import android.location.Address;import android.location.Geocoder;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import java.io.IOException;import java.util.List;import java.util.Locale;public class MainActivity extends AppCompatActivity implements LocationListener {// 定义TextViewprivate TextView nowAddress;private TextView lat;private TextView lon;// 定义双精度类型的经纬度private Double longitude,latitude;// 定义位置管理器private LocationManager locationManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 得到对应视图IDnowAddress = findViewById(R.id.tv_nowAddress);lat = findViewById(R.id.tv_latitude);lon = findViewById(R.id.tv_longitude);// 判断当前是否拥有使用GPS的权限if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){// 申请权限ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 100);}getLocation();/*或者这样子也是可以的// 获取当前位置管理器locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);// 启动位置请求locationManager.requestLocationUpdates(WORK_PROVIDER, 0, 0, MainActivity.this);*/}@SuppressLint("MissingPermission")private void getLocation() {// 获取当前位置管理器locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);// 启动位置请求// LocationManager.GPS_PROVIDER GPS定位// WORK_PROVIDER 网络定位// LocationManager.PASSIVE_PROVIDER 被动接受定位信息locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, MainActivity.this);}// 当位置改变时执行,除了移动设置距离为 0时@Overridepublic void onLocationChanged(@NonNull Location location) {// 获取当前纬度latitude = location.getLatitude();// 获取当前经度longitude = location.getLongitude();lat.setText("纬度:" + latitude);lon.setText("经度:" + longitude);// 定义位置解析Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());try {// 获取经纬度对于的位置// getFromLocation(纬度, 经度, 最多获取的位置数量)List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);// 得到第一个经纬度位置解析信息Address address = addresses.get(0);// 获取到详细的当前位置// Address里面还有很多方法你们可以自行实现去尝试。比如具体省的名称、市的名称...String info = address.getAddressLine(0) + // 获取国家名称address.getAddressLine(1) + // 获取省市县(区)address.getAddressLine(2); // 获取镇号(地址名称)// 赋值nowAddress.setText(info);} catch (IOException e) {e.printStackTrace();}// 移除位置管理器// 需要一直获取位置信息可以去掉这个locationManager.removeUpdates(this);}// 当前定位提供者状态@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {Log.e("onStatusChanged", provider);}// 任意定位提高者启动执行@Overridepublic void onProviderEnabled(@NonNull String provider) {Log.e("onProviderEnabled", provider);}// 任意定位提高者关闭执行@Overridepublic void onProviderDisabled(@NonNull String provider) {Log.e("onProviderDisabled", provider);}}

Layout布局的代码

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/tv_latitude"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="纬度"app:layout_constraintBottom_toTopOf="@+id/tv_longitude"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_longitude"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="经度"app:layout_constraintBottom_toTopOf="@+id/tv_nowAddress"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@id/tv_latitude" /><TextViewandroid:id="@+id/tv_nowAddress"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="当前位置"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。