相关文章推荐
仗义的山羊  ·  quill-blot-formatter ...·  1 年前    · 
讲道义的茴香  ·  android - Getting ...·  3 年前    · 
痴情的凉面  ·  javascript - ...·  3 年前    · 
首页 > 软件编程 > Android > Android GPS获取当前经纬度坐标

Android GPS获取当前经纬度坐标

作者:小小小青年

这篇文章主要为大家详细介绍了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请猛戳

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • Windows下搭建Flutter开发环境
    Windows下搭建Flutter开发环境
    2021-11-11
  • Android room数据库使用详解
    Android room数据库使用详解
    2021-11-11
  • Android性能优化及性能优化工具
    Android性能优化及性能优化工具
    2021-11-11
  • Kotlin入门学习教程之可见性修饰符
    Kotlin入门学习教程之可见性修饰符
    2021-11-11
  • 通俗易通讲解Android蓝牙键值适配
    通俗易通讲解Android蓝牙键值适配
    2021-11-11
  • Windows下Flutter+Idea环境搭建及配置
    Windows下Flutter+Idea环境搭建及配置
    2021-11-11
  • 图文详解Flutter单例的实现
    图文详解Flutter单例的实现
    2021-11-11
  • Android CameraX结合LibYUV和GPUImage自定义相机滤镜
    Android CameraX结合LibYUV和GPUImage自
    2021-11-11
  • 美国设下计谋,用娘炮文化重塑日本,已影响至中国
    美国设下计谋,用娘炮文化重塑日本,已影响至中国
    2021-11-19
  • 时空伴随者是什么意思?时空伴随者介绍
    时空伴随者是什么意思?时空伴随者介绍
    2021-11-09
  • 工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    2021-11-05
  • 2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2021-10-26
  • 电脑版 - 返回首页

    2006-2023 脚本之家 JB51.Net , All Rights Reserved.
    苏ICP备14036222号