博客
关于我
Android 原生定位
阅读量:211 次
发布时间:2019-02-28

本文共 7896 字,大约阅读时间需要 26 分钟。

有两种定位的方式: 

1.基于GPS定位; 
2.基于网络定位。

GPS定位的 好处 :精确度高; 坏处 :仅能在户外使用,获取定位信息速度慢,耗费电池。 

网络定位的 好处 :户内户外都能使用,定位速度快,电量耗费低; 坏处 :精确度不太高。

android原生定位框架的核心组件是 

 
系统服务,获取这个组件并不需要实例化,通常是通过函数 
 
获得,该函数会返回一个LocationManager的实例,示例代码如下:

// Acquire a reference to the system Location Manager

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

LocationListener

通过回调(callback)来获取用户定位信息

// Define a listener that responds to location updatesLocationListener locationListener = new LocationListener() {    public void onLocationChanged(Location location) {      // Called when a new location is found by the network location provider.      // 这个函数中的location就是获取到的位置信息    }    public void onStatusChanged(String provider, int status, Bundle extras) {}    public void onProviderEnabled(String provider) {}    public void onProviderDisabled(String provider) {}  };
同时我们需要调用函数 
requestLocationUpdates
 绑定
// Register the listener with the Location Manager to receive location updateslocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

定位权限

定位功能的使用需要获取定位权限 

如果使用 NETWORK_PROVIDER ,那么需要请求

android.permission.ACCESS_COARSE_LOCATION

如果使用 GPS_PROVIDER 或者同时使用上述两种定位方式,那么需要请求

android.permission.ACCESS_FINE_LOCATION

同时,如果你的app的运行环境是Android 5.0(API Level 21)或更高(If your app targets Android 5.0 (API level 21) or higher),那么你还需要显式请求

android.hardware.location.network 

或者 
android.hardware.location.gps

Geocoder可以将一个地址转变为经纬度,或者将经纬度转变为地址。
package com.example.demo;import android.Manifest;import android.app.Activity;import android.content.Context;import android.content.pm.PackageManager;import android.location.Address;import android.location.Criteria;import android.location.Geocoder;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.support.v4.content.ContextCompat;import android.text.TextUtils;import android.text.method.ScrollingMovementMethod;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import java.util.Locale;public class LocationActivity extends Activity implements View.OnClickListener {    private static final String TAG = LocationActivity.class.getSimpleName();    private LocationManager locationManager;    private LocationListener locationListener;    private TextView tv;    private Button btnStart;    private Button btnEnd;    private Button btnClean;    private StringBuilder stringBuilder = new StringBuilder();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_location);        setTitle("原生定位");        tv = findViewById(R.id.tv);        btnStart = findViewById(R.id.btnStart);        btnEnd = findViewById(R.id.btnEnd);        btnClean = findViewById(R.id.btnClean);        btnStart.setOnClickListener(this);        btnEnd.setOnClickListener(this);        btnClean.setOnClickListener(this);        //设置文字太长时TextView可以上下滑动        tv.setMovementMethod(ScrollingMovementMethod.getInstance());        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        locationListener = new LocationListener() {            @Override            public void onLocationChanged(Location location) {                stringBuilder.append(getTime()).append("\t定位信息:\n");                stringBuilder.append("\t").append(location.getLatitude()).append("\n");                stringBuilder.append("\t").append(location.getLongitude()).append("\n");                stringBuilder.append("\t").append(location.getProvider()).append("\n");                tv.setText(stringBuilder);                Geocoder gc = new Geocoder(LocationActivity.this, Locale.getDefault());                List
locationList = null; try { locationList = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); } Address address = locationList.get(0);//得到Address实例 if (address == null) { return; } stringBuilder.append(getTime()).append("\t地址信息:\n"); String countryName = address.getCountryName();//得到国家名称 if (!TextUtils.isEmpty(countryName)) { stringBuilder.append(countryName); } String adminArea = address.getAdminArea();//省 if (!TextUtils.isEmpty(adminArea)) { stringBuilder.append(adminArea); } String locality = address.getLocality();//得到城市名称 if (!TextUtils.isEmpty(locality)) { stringBuilder.append(locality); } for (int i = 0; address.getAddressLine(i) != null; i++) { String addressLine = address.getAddressLine(i); if(!TextUtils.isEmpty(addressLine)) { if(addressLine.contains(countryName)){ addressLine = addressLine.replace(countryName,""); } if(addressLine.contains(adminArea)){ addressLine = addressLine.replace(adminArea,""); } if(addressLine.contains(locality)){ addressLine = addressLine.replace(locality,""); } if(!TextUtils.isEmpty(addressLine)) { stringBuilder.append(addressLine); } } } String featureName = address.getFeatureName();//得到周边信息 if(!TextUtils.isEmpty(featureName)) { stringBuilder.append(featureName).append("\n"); } tv.setText(stringBuilder); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnStart: stringBuilder.append(getTime()).append("\t开始定位\n"); tv.setText(stringBuilder); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) { return; } if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) { return; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); break; case R.id.btnEnd: locationManager.removeUpdates(locationListener); stringBuilder.append(getTime()).append("\t结束定位\n\n"); tv.setText(stringBuilder); break; case R.id.btnClean: stringBuilder.delete(0, stringBuilder.length()); tv.setText(stringBuilder); break; default: break; } } private String getTime() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()); return simpleDateFormat.format(new Date(System.currentTimeMillis())); }}

转载地址:http://dles.baihongyu.com/

你可能感兴趣的文章
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>