Android GPS获取当前经纬度坐标
作者:小小小青年 发布时间:2021-10-31 12:09:40
标签:Android,GPS,坐标
APP中可能会遇到一种需求,就是将当前所在位置的坐标传到服务器上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,第二种通过百度地图API来实现,第三种通过天地图API来实现。
第一种方法(Android API实现),废话不多说,上代码。
MainActivity代码如下:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private double latitude = 0.0;
private double longitude = 0.0;
private TextView info;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
info = (TextView) findViewById(R.id.tv);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
//gps已打开
} else {
toggleGPS();
new Handler() {
}.postDelayed(new Runnable() {
@Override
public void run() {
getLocation();
}
}, 2000);
}
}
private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location1 != null) {
latitude = location1.getLatitude(); // 经度
longitude = location1.getLongitude(); // 纬度
}
}
}
private void getLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
}
info.setText("纬度:" + latitude + "\n" + "经度:" + longitude);
}
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, provider);
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, provider);
}
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
latitude = location.getLatitude(); // 经度
longitude = location.getLongitude(); // 纬度
}
}
};
/*
*
* 打开和关闭gps第二种方法
* private void openGPSSettings() {
//获取GPS现在的状态(打开或是关闭状态)
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);
if (gpsEnabled) {
//关闭GPS
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);
} else {
//打开GPS
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
}
}*/
}
main.xml布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经纬度信息:"
android:textColor="#660000"
android:textSize="20sp" />
</LinearLayout>
清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tqmapdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 连接互联网Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black" >
<activity
android:name="com.example.tqmapdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
运行结果如下
下载Demo请猛戳
第二种方法(百度地图API实现,注:需要自己申请apikey)
下载Demo请猛戳
第三种方法(天地图API实现)
下载Demo请猛戳
来源:https://blog.csdn.net/wuchuang127/article/details/39993901
0
投稿
猜你喜欢
- 利用Java连接MySQL做登陆界面,供大家参考,具体内容如下1、首先需要建立一个类,在这里,我命名为newLoginnewLogin类的代
- 前提最近我的的朋友浏览一些网站,看到好看的图片,问我有没有办法不用手动一张一张保存图片!我说用Jsoup丫!测试网站打开开发者模式(F12)
- static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概
- java 算法之归并排序详解一、思想 归并排序:将一个数组排序,可以先(递归地)将它分成两半部份分别排序,然后将结果归并起来; &
- Java中的我们可以利用split把字符串按照指定的分割符进行分割,然后返回字符串数组split 方法该方法的作用是:将一个字符串分割为子字
- 本文实例讲述了C#调用VB进行简繁转换的方法。分享给大家供大家参考。具体分析如下:首先在C#项目中引用Microsoft.VisualBas
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe
- 最近项目中需要实现定时执行任务,比如定时计算会员的积分、调用第三方接口等,由于项目采用spring框架,所以这里结合spring框架来介绍。
- 问题描述fastjson通过代码指定全局序列化返回时间格式,导致使用JSONField注解标注属性的特殊日期返回格式失效使用版本应用名称版本
- 本文实例为大家分享了Java实现在线聊天功能的具体代码,供大家参考,具体内容如下效果关键代码创建Client.javaimport java
- 介绍这里学习SpringSecurity,对SpringSecurity进行学习。基本用法添加依赖<dependency> &n
- CountDownLatch 是一个同步工具类,用来协调多个线程之间的同步,它能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行。
- 对于使用文件进行交换数据的应用来说,使用FTP 服务器是一个很不错的解决方案。关于FileZilla Server服务器的详细搭建配置过程,
- Jackson解析嵌套类(MismatchedInputException)具体报错如下问题描述:Jackson解析嵌套类问题 调
- 本文实例分析了C#中float的取值范围和精度。分享给大家供大家参考。具体分析如下:float类型的表现形式:默认情况下,赋值运算符右侧的实
- 本文介绍了Flutter 通过Clipper实现各种自定义形状的示例代码,分享给大家,具体如下:ClipOval 圆形裁剪ClipOval(
- Spring Boot如何实现配置文件的自动加载和刷新?在使用Spring Boot开发应用程序时,配置文件是非常重要的组成部分。在不同的环
- 本文介绍了android视频截屏&手机录屏实现代码,分享给大家,希望对大家有帮助问题在android中有时候我们需要对屏幕进行截屏操
- 本文实例讲述了C#采用OpenXml实现给word文档添加文字的方法,分享给大家供大家参考。具体方法如下:一般来说,使用OpenXml给wo
- Mybatis与JPA的优缺点JPA java持久层API可理解为一种规范,Hibernate就是其具体一个实现。它的实现应用是Spring