AndroidはGPSとネットワーク測位情報を取得
11296 ワード
位置サービスを取得した後、ネットワークとGPS測位の更新を同時に要求する.GPS信号がない場合は、ネットワーク測位の位置情報(ネットワーク測位情報には高値がない)を使用します.
activity_main.xml
MainActivity.java
AndroidManifest.xml
参照先:http://www.2cto.com/kf/201208/145033.html
activity_main.xml
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import java.text.SimpleDateFormat;
public class MainActivity extends Activity {
private LocationManager locationManager;
/**
*
*/
private Location lastLocation;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
private TextView tvLatitude;
private TextView tvLongitude;
private TextView tvProvider;
private TextView tvAccuracy;
private TextView tvAltitude;
private TextView tvBearing;
private TextView tvSpeed;
private TextView tvLastTime;
private TextView tvNewTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLatitude = (TextView) findViewById(R.id.tv_latitude);
tvLongitude = (TextView) findViewById(R.id.tv_longitude);
tvProvider = (TextView) findViewById(R.id.tv_provider);
tvAccuracy = (TextView) findViewById(R.id.tv_accuracy);
tvAltitude = (TextView) findViewById(R.id.tv_altitude);
tvBearing = (TextView) findViewById(R.id.tv_bearing);
tvSpeed = (TextView) findViewById(R.id.tv_speed);
tvLastTime = (TextView) findViewById(R.id.tv_last_time);
tvNewTime = (TextView) findViewById(R.id.tv_new_time);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(listener);
}
private LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
updateLocation();
}
@Override
public void onProviderEnabled(String s) {
updateLocation();
}
@Override
public void onProviderDisabled(String s) {
}
};
private void updateLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
updateLocation(location);
}
private void updateLocation(Location location) {
if (location == null) {
return;
}
//
if (isBetterLocation(location, lastLocation)) {
updateUI(location);
lastLocation = location;
}
}
/**
* UI
* @param location
*/
private void updateUI(final Location location) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvLatitude.setText(location.getLatitude() + "");
tvLongitude.setText(location.getLongitude() + "");
tvProvider.setText(location.getProvider());
tvAccuracy.setText(location.getAccuracy() + "");
tvAltitude.setText(location.getAltitude() + " ");
tvBearing.setText(location.getBearing() + "");
tvSpeed.setText(location.getSpeed() + " / ");
tvNewTime.setText(dateFormat.format(location.getTime()));
if (lastLocation != null) {
tvLastTime.setText(dateFormat.format(lastLocation.getTime()));
}
}
});
}
/**
*
* @param location
* @param currentBestLocation
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
return true;
}
// GPS
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > 2000;
boolean isSignificantlyOlder = timeDelta < -2000;
boolean isNewer = timeDelta > 0;//
// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer)
{
return true;
}
else if (isSignificantlyOlder)
{
return false;
}
// GPS
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
//
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate)
{
return true;
}
else if (isNewer && !isLessAccurate)
{
return true;
}
else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)
{
return true;
}
return false;
}
/**
*
* @param provider1
* @param provider2
* @return
*/
private boolean isSameProvider(String provider1, String provider2)
{
if (provider1 == null)
{
return provider2 == null;
}
return provider1.equals(provider2);
}
}
AndroidManifest.xml
参照先:http://www.2cto.com/kf/201208/145033.html