赞
踩
在 AndroidManifest.xml 中加上位置信息权限按需要添加
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 如果需要精确定位的话请加上此权限 -->
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 如果需要粗略定位的话请加上此权限 -->
添加授权弹窗代码
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
- if (!isTaskRoot()) {
- // Android launched another instance of the root activity into an existing task
- // so just quietly finish and go away, dropping the user back into the activity
- // at the top of the stack (ie: the last state of this task)
- // Don't need to finish it again since it's finished in super.onCreate .
- return;
- }
- instance = this;
-
- //这里以ACCESS_COARSE_LOCATION为例
- if (AppActivity.instance.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- //申请WRITE_EXTERNAL_STORAGE权限
- AppActivity.instance.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
- GPSUtils.getInstance().LOCATION_CODE);//自定义的code 随便填一个数
- } else {
- // 用户以授权定位信息
-
- }
- }
-
-
-
-
- // 授权回调。询问是否同意授权的时候,系统会弹出对话框,我们选择之后,会进行回调。在回调里面进行判断。
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- switch (requestCode) {
- case GPSUtils.LOCATION_CODE: {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // 权限被用户同意。
- // 执形我们想要的操作
- GPSUtils.getInstance().province = GPSUtils.getInstance().getProvince();
- } else {
- Log.i("GPS", "未授权定位权限");
- // 用户授权定位信息
- }
- }
- case GPSUtils.OPEN_GPS_CODE: {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // 权限被用户同意。
- // 执形我们想要的操作
- GPSUtils.getInstance().province = GPSUtils.getInstance().getProvince();
- } else {
- Log.i("GPS", "未授权定位权限");
- // 用户授权定位信息
- }
- }
- }
- }
通过实例获取 Lacation 得到 经纬度
这边
- LocationManager locationManager = (LocationManager) AppActivity.instance.getSystemService(Context.LOCATION_SERVICE); // 默认Android GPS定位实例
-
- Location location = null;
- // 是否已经授权
- if (AppActivity.instance.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
- == PackageManager.PERMISSION_GRANTED)
- {
- //判断GPS是否开启,没有开启,则开启
- // if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
- // //跳转到手机打开GPS页面
- // Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- // //设置完成后返回原来的界面
- // AppActivity.instance.startActivityForResult(intent,OPEN_GPS_CODE);
- // }
- //
- // location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // GPS芯片定位 需要开启GPS
- // location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // 利用网络定位 需要开启GPS
- location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); // 其他应用使用定位更新了定位信息 需要开启GPS
- }
-
- String p = "";
- if(location != null)
- {
- Log.i("GPS: ", "获取位置信息成功");
- Log.i("GPS: ","经度:" + location.getLatitude());
- Log.i("GPS: ","纬度:" + location.getLongitude());
- }
- else
- {
- Log.i("GPS: ", "获取位置信息失败");
- }
-
- /*
- * 根据经度纬度 获取国家,省份
- * */
- public String getAddress(double latitude, double longitude) {
- String cityName = "";
- List<Address> addList = null;
- Geocoder ge = new Geocoder(AppActivity.instance);
- try {
- addList = ge.getFromLocation(latitude, longitude, 1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (addList != null && addList.size() > 0) {
- for (int i = 0; i < addList.size(); i++) {
- Address ad = addList.get(i);
- Log.i("GPS 国家: ", ad.getCountryName());
- Log.i("GPS: 省份", ad.getAdminArea());
- Log.i("GPS: 详细地址", ad.getFeatureName());
-
- cityName += ad.getCountryName() + " " + ad.getLocality();
- }
- }
- return cityName;
- }
搞定。
下面是我写了个工具类
- package org.cocos2dx.javascript;
-
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.location.Address;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Build;
- import android.util.Log;
-
- import java.io.IOException;
- import java.util.List;
-
- import android.Manifest;
- import android.content.pm.PackageManager;
-
- /**
- * Created by Administrator on 2018/4/17.
- * 获取用户的地理位置
- */
- public class GPSUtils {
-
- private static GPSUtils instance;
- private LocationManager locationManager;
- public static final int LOCATION_CODE = 1000;
- public static final int OPEN_GPS_CODE = 1001;
-
- public String province = "";
-
- public static GPSUtils getInstance() {
- if (instance == null) {
- instance = new GPSUtils();
- }
- return instance;
- }
-
- @TargetApi(Build.VERSION_CODES.M)
- public String getProvince() {
- Log.i("GPS: ", "getProvince");
- locationManager = (LocationManager) AppActivity.instance.getSystemService(Context.LOCATION_SERVICE); // 默认Android GPS定位实例
-
- Location location = null;
- // 是否已经授权
- if (AppActivity.instance.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
- == PackageManager.PERMISSION_GRANTED)
- {
- //判断GPS是否开启,没有开启,则开启
- // if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
- // //跳转到手机打开GPS页面
- // Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- // //设置完成后返回原来的界面
- // AppActivity.instance.startActivityForResult(intent,OPEN_GPS_CODE);
- // }
- //
- // location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // GPS芯片定位 需要开启GPS
- // location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // 利用网络定位 需要开启GPS
- location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); // 其他应用使用定位更新了定位信息 需要开启GPS
- }
-
- String p = "";
- if(location != null)
- {
- Log.i("GPS: ", "获取位置信息成功");
- Log.i("GPS: ","经度:" + location.getLatitude());
- Log.i("GPS: ","纬度:" + location.getLongitude());
-
- // 获取地址信息
- p = getAddress(location.getLatitude(),location.getLongitude());
- Log.i("GPS: ","location:" + p);
- }
- else
- {
- Log.i("GPS: ", "获取位置信息失败,请检查是够开启GPS,是否授权");
- }
-
-
- return p;
- }
-
- /*
- * 根据经度纬度 获取国家,省份
- * */
- public String getAddress(double latitude, double longitude) {
- String cityName = "";
- List<Address> addList = null;
- Geocoder ge = new Geocoder(AppActivity.instance);
- try {
- addList = ge.getFromLocation(latitude, longitude, 1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (addList != null && addList.size() > 0) {
- for (int i = 0; i < addList.size(); i++) {
- Address ad = addList.get(i);
- cityName += ad.getCountryName() + " " + ad.getLocality();
- }
- }
- return cityName;
- }
- }
调用 getProvince() 运行结果
选择Location Provider:
Android系统存在多种provider,分别是
GPS_PROVIDER:
这个就是手机里有GPS芯片,然后利用该芯片就能利用卫星获得自己的位置信息。但是在室内,GPS定位基本没用,很难定位的到。
NETWORK_PROVIDER:
这个就是利用网络定位,通常是利用手机基站和WIFI节点的地址来大致定位位置,这种定位方式取决于服务器,即取决于将基站或WIF节点信息翻译成位置信息的服务器的能力。由于目前大部分Android手机没有安装google官方的location manager库,大陆网络也不允许,即没有服务器来做这个事情,自然该方法基本上没法实现定位。
PASSIVE_PROVIDER:
被动定位方式,这个意思也比较明显,就是用现成的,当其他应用使用定位更新了定位信息,系统会保存下来,该应用接收到消息后直接读取就可以了。比如如果系统中已经安装了百度地图,高德地图(室内可以实现精确定位),你只要使用它们定位过后,再使用这种方法在你的程序肯定是可以拿到比较精确的定位信息。用户可以直接指定某一个provider
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。