android 定位的两种方式:GPS_PROVIDER and NETWORK_PROVIDER

定位的可以借助LocationManager来实现

MainActivity代码

 static final String TAG = "MainActivity";

    private TextView locationTV;
private LocationManager locationManager;
private String provider; ArrayList<ContactModel> dataList = new ArrayList<ContactModel>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initView(); locationTV = (TextView) findViewById(R.id.locaiton_tv); locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
// 获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
// 当没有可用的位置提供器时,弹出Toast提示用户
Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// 显示当前设备的位置信息
showLocation(location);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 5000, 1, locationListener); }
private void showLocation(Location location) {
String currentPosition = "latitude is " + location.getLatitude() + "\n"+ "longitude is " + location.getLongitude();
locationTV.setText(currentPosition);
} @Override
protected void onDestroy() {
super.onDestroy(); if (locationManager != null) { if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
} // 关闭程序时将监听器移除
locationManager.removeUpdates(locationListener);
}
} LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) { Toast.makeText(MainActivity.this,"onLocationChanged",Toast.LENGTH_SHORT).show();
} @Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_SHORT).show();
} @Override
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_SHORT).show();
} @Override
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_SHORT).show();
}
};

android 获取经纬度的更多相关文章

  1. Android 获取地理位置信息 封装好了 直接用

    前言:花了一个早上研究了以下android获取经纬度,然后网上的参考资料都是杂七杂八,基本上都是过去几年的,现在我用 android6.0参照别人的结果发生好多错误,我的内心几乎是崩溃的.后来,不断百 ...

  2. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  3. Android获取定位权限,获取设备所在的经纬度

    转载请标明出处:http://www.cnblogs.com/tangZH/p/8969898.html  前言: 有时候我们仅仅是想要获取设备所在的经纬度,那么直接调用Android相关的api就可 ...

  4. Android开发:LocationManager获取经纬度及定位过程(附demo)

    在Android开发其中.常常须要用到定位功能,尤其是依赖于地理位置功能的应用.非常多人喜欢使用百度地图,高德地图提供的sdk.开放API,可是在只须要经纬度,或者城市,街道地址等信息.并不须要提供预 ...

  5. Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等

    extends:http://blog.csdn.net/h7870181/article/details/12505883 Google Maps API 网络服务 官网地址 : https://d ...

  6. Android笔记之使用LocationManager获取经纬度

    LocationManager.getLastKnownLocation(String provider)有可能返回null,概率还挺高 findViewById(R.id.llMain).setOn ...

  7. android获取位置location为null的问题

      12:38:542016-12-23 很多人经常遇到这种问题,主要是获取到位置的信息为null,第一个主要要有权限 <uses-permission android:name="a ...

  8. 获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)

    如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)  目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度的定位,计算行驶公里数并实时刷新界面显示.大家 ...

  9. android 获取GPS定位

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

随机推荐

  1. DWORD类型

    INT 代表int,DWORD 代表 unsigned longint会随着机器位数的不同而发生变化,比如在16位机上为16为,在32位机上为32位,在64位机上为64位.看看最原始的定义就知道了. ...

  2. c# 以二进制读取文本文件

    using System; using System.IO; public class FileApp {     public static void Main()     {         // ...

  3. 墨卡托投影, GPS 坐标转像素, GPS 坐标转距离

    Before: 1. 研究的需要, 在 google map 上爬取了一些的静态卫星地图图片,每张图片的像素为 256*256 2. 通过 photshop 将这些地图碎片手动拼成了地图, 地图只是覆 ...

  4. PHP-006

    Q:应用程序执行时的路径 "D:\*******\protected\runtime" 是无效的. 请确定它是一个可被 Web server process 写入资料的目录. A: ...

  5. python2.0 s12 day2

    s12 day2 视频每节的内容 05 python s12 day2 python编码   1.第一句python代码 python 执行代码的过程 文件读到内存 分析内容 编译字节码  转换机器码 ...

  6. laravel 使用 session

    配置方面的不写了,请参考学院君的文章:http://laravelacademy.org/post/5898.html 在开始之前先说一下,使用 request 对象的 session() 方法,和直 ...

  7. 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决

    在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...

  8. surfaceView和View的区别

    概念:view在UI线程去更新自己:而SurfaceView则在一个子线程中去更新自己 surfaceView是在一个新起的单独线程中可以重新绘制画面,而View必须在UI的主线程中更新画面 在UI的 ...

  9. hadoop错误各种原因

    NoRouteToHostException 错误描述: INFO hdfs.DFSClient: Exception in createBlockOutputStream java.net.NoRo ...

  10. LNMP 配置二级域名

    准备: 已备案的主域名,例如:www.test.com 拥有自己的服务器 服务器环境 LNMP 目标: 配置一个二级域名: bbs.test.com 1 登录域名后台(阿里为例) 记录类型: A 主机 ...