android gps定位工具类,Android原生GPS和网络定位工具类

在应用开发中我们常常需要网络获取位置的方法,这里做一个工具类的封装,使用的是Android原生定位,包含两种方式,一种是原生GPS一种是网络定位的封装实现:

工具类代码如下所示:

package com.vondear.tools.tools;

import android.Manifest;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.SharedPreferences;

import android.content.pm.PackageManager;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Bundle;

import android.support.v4.app.ActivityCompat;

import android.app.Activity;

import android.support.v4.content.LocalBroadcastManager;

import android.view.accessibility.AccessibilityManager;

import android.widget.Toast;

import com.ta.utdid2.android.utils.SharedPreferenceHelper;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

/**

*

* Created by AndyYuan on time at 2019/6/20.

*/

public class GetGPSUtil {

static GetGPSUtil getGPSUtil;

public static GetGPSUtil getInstance() {

if (getGPSUtil == null) getGPSUtil = new GetGPSUtil();

return getGPSUtil;

}

/**

* 获取经纬度

*

* @param context

* @return

*/

private String getLngAndLat(Context context) {

double latitude = 0.0;

double longitude = 0.0;

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

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { //从gps获取经纬度

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, 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 "没有GPS权限";

}

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

} else {//当GPS信号弱没获取到位置的时候又从网络获取

Toast.makeText(context, "GPS信号弱获取不到", Toast.LENGTH_SHORT).show();

return getLngAndLatWithNetwork(context);

}

} else { //从网络获取经纬度

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);

//通过查阅资料好像我们的Android手机要开启过GPS(第一次获取须GPS获取或者其他第三方应用开启使用过GPS)后然后getLastKnownLocation(LoactionManager.NETWORK_PROVIDER)这个方法才不会返回空。

Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

}

}

return longitude + "," + latitude;

}

//从网络获取经纬度

public String getLngAndLatWithNetwork(Context context) {

double latitude = 0.0;

double longitude = 0.0;

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

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, 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 "没有网络Internet获取权限";

}

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);

Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

}

return longitude + "," + latitude;

}

LocationListener locationListener = new LocationListener() {

// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {

}

// Provider被enable时触发此函数,比如GPS被打开

@Override

public void onProviderEnabled(String provider) {

}

// Provider被disable时触发此函数,比如GPS被关闭

@Override

public void onProviderDisabled(String provider) {

}

//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发

@Override

public void onLocationChanged(Location location) {

}

};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 提供了两种方式进行定位,一种是使用 GPS 进行定位,另一种是使用网络进行定位。其中,GPS 定位需要设备具备 GPS 硬件,而网络定位则需要设备连接到互联网。 使用 GPS 进行定位: 1. 添加权限 在 AndroidManifest.xml 文件中添加 ACCESS_FINE_LOCATION 权限。 2. 获取位置提供器 使用 LocationManager 类获取位置提供器,如下所示: ```java LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String provider = LocationManager.GPS_PROVIDER; ``` 其中,provider 参数指定使用 GPS 进行定位。 3. 设置定位参数 设置定位参数,如下所示: ```java LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(1000); locationRequest.setFastestInterval(500); ``` 其中,setPriority 方法指定定位的优先级,PRIORITY_HIGH_ACCURACY 表示使用 GPS 进行定位,setInterval 和 setFastestInterval 方法分别指定定位的时间间隔和最快定位时间间隔。 4. 注册监听器 注册监听器,如下所示: ```java locationManager.requestLocationUpdates(provider, 0, 0, locationListener); ``` 其中,locationListener 参数是一个 LocationListener 对象,用于监听位置变化事件。 5. 获取位置信息 在 LocationListener 的 onLocationChanged 方法中获取位置信息,如下所示: ```java @Override public void onLocationChanged(Location location) { double longitude = location.getLongitude(); double latitude = location.getLatitude(); } ``` 使用网络进行定位: 1. 添加权限 在 AndroidManifest.xml 文件中添加 ACCESS_COARSE_LOCATION 权限。 2. 获取位置提供器 使用 LocationManager 类获取位置提供器,如下所示: ```java LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String provider = LocationManager.NETWORK_PROVIDER; ``` 其中,provider 参数指定使用网络进行定位。 3. 注册监听器 注册监听器,如下所示: ```java locationManager.requestLocationUpdates(provider, 0, 0, locationListener); ``` 其中,locationListener 参数是一个 LocationListener 对象,用于监听位置变化事件。 4. 获取位置信息 在 LocationListener 的 onLocationChanged 方法中获取位置信息,如下所示: ```java @Override public void onLocationChanged(Location location) { double longitude = location.getLongitude(); double latitude = location.getLatitude(); } ``` 需要注意的是,使用网络进行定位可能会存在一定的误差,但相对来说较为稳定,并且不需要设备具备 GPS 硬件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值