700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 通过GPS获取位置信息

通过GPS获取位置信息

时间:2019-08-01 11:24:53

相关推荐

通过GPS获取位置信息

通过GPS获取位置信息

自己封装了一个单例工具类,用来获取GPS位置信息

package com.e7wifi.colourmedia.Util;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import .Uri;import android.os.Bundle;/*** Created by 135 on /7/8.*/public class GpsUtils {private static GpsUtils instance = null;Context context;LocationManager locationManager;String locationProvider;FinishLocationListener finishLocationListener;public double[] locations=new double[2];private GpsUtils(Context context){this.context=context;}public static GpsUtils getInstance(Context context) {if (instance == null) {synchronized (GpsUtils.class) {if (instance == null) {instance = new GpsUtils(context);}}}return instance;}/*** 开始定位手机位置*/public void startGetGps(){setBestGps();//获取LocationLocation location = locationManager.getLastKnownLocation(locationProvider);if(location!=null) {locations[0] = location.getLatitude();locations[1] = location.getLongitude();}locationManager.requestLocationUpdates(locationProvider, 2000, 10, locationListener);// if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))// {//toggleGPS();// }}//获取做好的定位方式并设置private void setBestGps() {Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度criteria.setAltitudeRequired(false);//无海拔要求criteria.setBearingRequired(false);//无方位要求criteria.setCostAllowed(true);//允许产生资费criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);locationProvider=locationManager.getBestProvider(criteria,false);}/*** 移除位置监听*/public void removeGpsListener(){locationManager.removeUpdates(locationListener);}/*** 开启位置监听*/public void openUpdateListener(int minTime,int minDistance){locationManager.requestLocationUpdates(locationProvider, minTime, minDistance, locationListener);}//打开GPS,这个API已经无法使用了。现在的权限机制,这个方法已经无法代码打开GPSprivate 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(context, 0, gpsIntent, 0).send();} catch (PendingIntent.CanceledException e) {e.printStackTrace();locationManager.requestLocationUpdates(WORK_PROVIDER, 1000, 0, locationListener);Location location1 = locationManager.getLastKnownLocation(WORK_PROVIDER);if (location1 != null) {locations[0] = location1.getLatitude(); // 经度locations[1] = location1.getLongitude(); // 纬度}}}/*** 获取位置经纬度坐标* @return 经纬度坐标latitude,longitude;*/public double[] getLocations(){return locations;}private double[] updateWithNewLocation(Location location) {double[] locations =new double[2] ;if (location != null) {double lat = location.getLatitude();double lng = location.getLongitude();locations[0]=lat;locations[1]=lng;}return locations;}public void setfinishLocationListener(FinishLocationListener listener){finishLocationListener=listener;}//位置信息更新时的回调接口public interface FinishLocationListener{void alreadyFinishLocation(Location location);}private LocationListener locationListener = new LocationListener() {public void onLocationChanged(Location location) {locations=updateWithNewLocation(location);if(finishLocationListener!=null) {finishLocationListener.alreadyFinishLocation(location);finishLocationListener=null;}}public void onProviderDisabled(String provider) {setBestGps();}public void onProviderEnabled(String provider) {}public void onStatusChanged(String provider, int status, Bundle extras) {setBestGps();}};}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。