GPSツール類(オフライン位置決めが可能)
ツールクラス
/**
* Created by chenzhi on 2017/12/13 0013.
*
* 6.0
*/
@SuppressLint("MissingPermission")
public class GPSUtils {
private static LocationManager mLocationManager;
private static final String TAG = "GPSUtils";
private static Location mLocation = null;
private static Activity mContext;
public GPSUtils(Activity context) {
this.mContext = context;
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// GPS
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(context, " GPS ...", Toast.LENGTH_SHORT).show();
// GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivityForResult(intent, 0);
return;
}
//
String bestProvider = mLocationManager.getBestProvider(getCriteria(), true);
//
// ,getLastKnownLocation LocationManager.GPS_PROVIDER
Location location = mLocationManager.getLastKnownLocation(bestProvider);
// getLocationData(location);
mLocation = location;
//
// mLocationManager.addGpsStatusListener(listener);
// , 4
// 1, : GPS_PROVIDER NETWORK_PROVIDER
// 2, ,
// 3, : ,
// 4,
// : 2 3, 3 0, 3 ; 3 0, ; 0,
// 1 , 1 ;
// : , service Thread, run sleep(10000); handler.sendMessage(),
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
}
/**
*
*
* @return
*/
private static Criteria getCriteria() {
Criteria criteria = new Criteria();
// Criteria.ACCURACY_COARSE ,Criteria.ACCURACY_FINE
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//
criteria.setSpeedRequired(false);
//
criteria.setCostAllowed(false);
//
criteria.setBearingRequired(false);
//
criteria.setAltitudeRequired(false);
//
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
/**
* @return Location--->getLongitude() /getLatitude()
*/
public static Location getLocation() {
if (mLocation == null) {
Log.e("GPSUtils", "setLocationData: ");
return null;
}
return mLocation;
}
public static String getLocalCity(){
if (mLocation==null){
Log.e("GPSUtils", "getLocalCity: ");
return "";
}
List
result = getAddress(mLocation);
String city = "";
if (result != null && result.size() > 0) {
city = result.get(0).getLocality();//
}
return city;
}
public static String getAddressStr(){
if (mLocation==null){
Log.e("GPSUtils", "getAddressStr: ");
return "";
}
List result = getAddress(mLocation);
String address = "";
if (result != null && result.size() > 0) {
address = result.get(0).getAddressLine(0);//
}
return address;
}
//
private static LocationListener locationListener = new LocationListener() {
//
public void onLocationChanged(Location location) {
mLocation = location;
Log.i(TAG, " :" + location.getTime());
Log.i(TAG, " :" + location.getLongitude());
Log.i(TAG, " :" + location.getLatitude());
Log.i(TAG, " :" + location.getAltitude());
}
//GPS
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
// GPS
case LocationProvider.AVAILABLE:
Log.i(TAG, " GPS ");
break;
// GPS
case LocationProvider.OUT_OF_SERVICE:
Log.i(TAG, " GPS ");
break;
// GPS
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.i(TAG, " GPS ");
break;
}
}
//GPS
public void onProviderEnabled(String provider) {
Location location = mLocationManager.getLastKnownLocation(provider);
mLocation = location;
}
//GPS
public void onProviderDisabled(String provider) {
mLocation = null;
}
};
//
private static List getAddress(Location location) {
List result = null;
try {
if (location != null) {
Geocoder gc = new Geocoder(mContext, Locale.getDefault());
result = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//
GpsStatus.Listener listener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch (event) {
//
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.i(TAG, " ");
break;
//
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.i(TAG, " ");
GpsStatus gpsStatus = mLocationManager.getGpsStatus(null);
//
int maxSatellites = gpsStatus.getMaxSatellites();
//
Iterator iters = gpsStatus.getSatellites()
.iterator();
int count = 0;
while (iters.hasNext() && count <= maxSatellites) {
GpsSatellite s = iters.next();
count++;
}
System.out.println(" :" + count + " ");
break;
//
case GpsStatus.GPS_EVENT_STARTED:
Log.i(TAG, " ");
break;
//
case GpsStatus.GPS_EVENT_STOPPED:
Log.i(TAG, " ");
break;
}
}
};
}
Activityでの呼び出し
private void initLocationGPS() {
gpsUtils = new GPSUtils(MainActivity.this);// GPS
handler.postDelayed(runnable, 0);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
location = gpsUtils.getLocation();//
if (location != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updateView(location);
}
});
handler.removeCallbacks(runnable);
} else {
handler.postDelayed(this, 1000);
}
}
};
/**
*
*
* @param location
*/
private void updateView(Location location) {
if (location != null) {
tvLongitude.setText(String.valueOf(location.getLongitude()));
tvLatitude.setText(String.valueOf(location.getLatitude()));
tvAddress.setText(String.valueOf(gpsUtils.getAddressStr()));
} else {
}
}
これで1つのGPS測位が完了したので、簡単ではないでしょうか.
文章はとても短くて、道はまだ长くて、皆さん、こんにちは、私は玖玖君で、1人のハンサムで才能と并存する男で、私达は次号でまた会います.