安卓开发入门gps获取定位经纬度海拔速度
layout xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="68dp"android:paddingLeft="68dp"android:paddingRight="68dp"android:paddingTop="68dp"tools:context="com.example.location2.MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium"android:text="经纬度"android:id="@+id/tvLag"android:layout_alignParentTop="true"android:layout_alignParentStart="true"android:layout_alignParentLeft="true" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium"android:text="海拔"android:id="@+id/tvAlt"android:layout_below="@+id/tvLag"android:layout_alignParentStart="true"android:layout_marginTop="10dp"android:layout_alignParentLeft="true" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium"android:text="速度"android:id="@+id/tvSpeed"android:layout_below="@+id/tvAlt"android:layout_alignParentStart="true"android:layout_marginTop="10dp"android:layout_alignParentLeft="true" /></RelativeLayout>
MainActivity类代码
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import android.content.pm.PackageManager;public class MainActivity extends AppCompatActivity {private String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION};private final int PERMS_REQUEST_CODE = 200;private TextView tvLag,tvAlt,tvSpeed;private LocationManager lm = null;private Location mLocation;private MyLocationListner mLocationListner;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvLag = (TextView) findViewById(R.id.tvLag);tvAlt = (TextView) findViewById(R.id.tvAlt);tvSpeed = (TextView) findViewById(R.id.tvSpeed);lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);//Android 6.0以上版本需要临时获取权限if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1&&PackageManager.PERMISSION_GRANTED!=checkSelfPermission(perms[0])) {requestPermissions(perms,PERMS_REQUEST_CODE);}else{initLocation();}}private void initLocation(){//判断GPS是否正常启动if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){Toast.makeText(MainActivity.this, "请开启GPS...",Toast.LENGTH_SHORT);//返回开启GPS导航设置界面Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);startActivityForResult(intent,0);return;}if (mLocationListner == null){mLocationListner = new MyLocationListner();}try{mLocation = lm.getLastKnownLocation(lm.GPS_PROVIDER);updateView(mLocation);}catch (SecurityException se){}try{/*** 开启定位监听变化* 参数1,定位方式:主要有GPS_PROVIDER和NETWORK_PROVIDER,前者是GPS,后者是GPRS以及WIFI定位* 参数2,位置信息更新周期.单位是毫秒* 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息* 参数4,监听* 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新*/lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, mLocationListner);}catch (SecurityException se){}}private class MyLocationListner implements LocationListener{@Overridepublic void onLocationChanged(Location location){updateView(location);}@Overridepublic void onProviderDisabled(String provider){updateView(null);}@Overridepublic void onProviderEnabled(String provider){try{updateView(lm.getLastKnownLocation(provider));}catch (SecurityException e){}}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras){}}private void updateView(Location location){if (location!=null) {tvLag.setText("经度:"+location.getLongitude()+"\n"+"纬度:"+location.getLatitude());tvAlt.setText("当前海拔:" + location.getAltitude() + "m");tvSpeed.setText("当前速度:" + location.getSpeed() + "m/s");}else{tvLag.setText("经度:"+"纬度:");tvAlt.setText("当前海拔:" );tvSpeed.setText("当前速度:");}}@Overridepublic void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){switch(permsRequestCode){case PERMS_REQUEST_CODE:boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;if(storageAccepted){initLocation();}break;}}}
getFromLocation类
address.getAddressLine(0)详细地址
address.getFeatureName()所在小区街道
address.getCountryName()所在国家
address.getLocality()所在城市
Address[addressLines=[0:"贵州省xx市xx区xx小区"],feature=xx小区,admin=贵州省,sub-admin=xx街道,locality=xx市,thoroughfare=xx路,postalCode=null,countryCode=CN,countryName=中国,hasLatitude=true,latitude=xx,hasLongitude=true,longitude=xx,phone=null,url=null,extras=Bundle[mParcelledData.dataSize=92]]
安卓开发入门gps获取定位经纬度海拔速度相关推荐
- 【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)
需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.获取定位信息 开启定位相关功能只是将定位的前提条件准备好,若想获得手机当前所处的位置信息,还要依靠下列的3种定位工具. (1)定位条件器Crite ...
- vue 微信公众号获取定位经纬度 腾讯地图逆地址解析为具体地址
最近做项目需要用到 微信公众号获取定位,并将定位转换为具体地址的需求,找了挺多,最后整理下,分享给大家~ 我这里使用的是腾讯地图,也可以使用其他的如百度.高德等. 思路是,先使用微信开放文档的获取定位 ...
- 安卓开发入门教程-UI控件_EditText
什么是EditText EditText是用于进行文本输入的UI控件. 基础样例 1.普通输入 效果图 代码 <EditTextandroid:layout_width="wrap_c ...
- android安卓开发入门视频教程资料百度网盘下载
android安卓开发入门视频教程资料讲解安卓核心基础,包含视频+笔记,适合新手入门学习. 百度网盘:https://pan.baidu.com/s/1uciMAAa97nm5RSLILtdPdg&a ...
- Android开发,GPS获取实时时间并转为北京时间,定位信息,海拔高度,并进行显示
在转载的基础上增加一些内容,主要是时间的转换所以这里直接先把重点列出来: (GPS时间转为本地时间): @SuppressLint("SimpleDateFormat")priva ...
- Android获取GPS网络定位经纬度信息
定位一般分为是:GPS定位,WIFI定位,基站定位 和 AGPS定位 GPS定位 GPS定位需要手机GPS模块硬件支持.GPS走的是卫星通信的通道,在没有网络连接的情况下也能使用,并且通过GPS方式准 ...
- STM32+安信可GP-01定位模块实现获取定位经纬度信息
文章目录 GP-01模组简介 1.硬件准备 1.1 GP-01-Kit 1.2 STM32F103C8T6核心板或者最小开发板 1.3 接线方式 2.软件准备 2.1 MDK(Keil v5) 2.2 ...
- 【Unity】中如何通过GPS获取设备经纬度(测试脚本)
在游戏开发中需要使用gps获取经纬度坐标定位玩家的当前位置,那么在开发过程中这个功能容不容易实现呢?下面就给大家介绍下Unity中获取设备经纬度的方法,一起来看看吧. Unity使用GPS 的API ...
- Android中获取定位经纬度信息
场景 根据GPS获取经纬度效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实 ...
最新文章
- docsify and awesome
- offsetLeft 解析
- Java overview JVM
- JS 数组 各项操作
- 26. 左旋转字符串
- Guava、Spring 如何抽象观察者模式?
- 看动画轻松理解「链表」实现「 LRU 缓存淘汰算法」
- Ubuntu12.04和12.10下H3C iNode客户端安装时出现找不到库的解决办法
- 你了解HTTPS,但你可能不了解X.509
- 无敌打印(适用各种浏览器自带打印功能)
- URL跳转与webview安全浅谈
- 【程序源代码】万岳在线教育系统WEB开源版
- Arduino通过usbasp编程器烧录程序或者bootloaders时出现以下错误解决方法
- DSP2812和28335的区别
- BetaFlight开源代码框架简介
- 线性分类器三种最优准则
- 【渗透测试】编辑器漏洞
- win10用户账号密码重置
- Linux 启动管理(详细版)
- PADA: Example-based Prompt Learning for on-the-fly Adaptation to Unseen Domains