/*
* GpS
*
*/
//***获取当前的 GPS 坐标
// AndroidManifest.xml must have the following permission:
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the provider status changes. Possible status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or AVAILABLE.
}
public void onProviderEnabled(String provider) {
// called when the provider is enabled by the user
}
public void onProviderDisabled(String provider) {
// called when the provider is disabled by the user, if it's already disabled, it's called immediately after requestLocationUpdates
}
public void onLocationChanged(Location location) {
double latitute = location.getLatitude();
double longitude = location.getLongitude();
// do whatever you want with the coordinates
}
});
//***获取最近一次已知的 GPS 坐标
// AndroidManifest.xml must have the following permission:
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitute, longitude = 0;
if(location != null){
latitute = location.getLatitude();
longitude = location.getLongitude();
}
//***GPS 坐标之间的距离
Location originLocation = new Location("gps");
Location destinationLocation = new Location("gps");
originLocation.setLatitude(originLatitude);
originLocation.setLongitude(originLongitude);
destinationLocation.setLatitude(originLatitude);
destinationLocation.setLongitude(originLongitude);
float distance = originLocation.distanceTo(destinationLocation);
//***注册监听GPS 状态变化
// AndroidManifest.xml must have the following permission:
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(new GpsStatus.Listener(){
public void onGpsStatusChanged(int event) {
switch(event){
// Event sent when the GPS system has started
case GpsStatus.GPS_EVENT_STARTED:
// put your code here
break;
// Event sent when the GPS system has stopped
case GpsStatus.GPS_EVENT_STOPPED:
// put your code here
break;
// Event sent when the GPS system has received its first fix since starting
case GpsStatus.GPS_EVENT_FIRST_FIX:
// put your code here
break;
// Event sent periodically to report GPS satellite status
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// put your code here
break;
}
}
});
//***注册监听邻近提示
// AndroidManifest.xml must have the following permission:
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
// Use PendingIntent.getActivity(Context, int, Intent, int), PendingIntent.getBroadcast(Context, int, Intent, int) or PendingIntent.getService(Context, int, Intent, int) to create the PendingIntent, which will be used to generate an Intent to fire when the proximity condition is satisfied.
PendingIntent pendingIntent;
// latitude the latitude of the central point of the alert region
// longitude the longitude of the central point of the alert region
// radius the radius of the central point of the alert region, in meters
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(latitude, longitude, radius, -1, pendingIntent);