獲取到位置服務以後,同時請求網络和gps定位更新,然後就會同時上報網络和gps的Location 信息。在沒有gps信號的時候,會自動獲取網络定位的位置信息,如果有gps信號,則優先獲取gps提供的位置信息.isBetterLocation 根據 時間、准確性、定位方式等判斷是否更新當前位置信息,該方法來源於開發指南的Obtaining User Location 下。 


package cncit.gps;


import java.text.SimpleDateFormat;
import java.util.Date;


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.util.Log;
import android.widget.TextView;


public class UploadgpsActivity extends Activity
{
LocationManager lm = null;
Location myLocation = null;
TextView loc, timeText;


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss.SSSZ");


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


loc = (TextView) findViewById(R.id.loc);
timeText = (TextView) findViewById(R.id.time);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}


@Override
protected void onResume()
{
super.onResume();


lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
listener);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
listener);


Log.e("onResume", "onResume");
}


@Override
protected void onPause()
{
super.onPause();


Log.e("onPause", "onPause");


lm.removeUpdates(listener);
}


 


LocationListener listener = new LocationListener()
{


@Override
public void onLocationChanged(Location location)
{
// 實際上報時間
//String time = sdf.format(new Date(location.getTime()));
//timeText.setText("實際上報時間:" + time);
 
if (isBetterLocation(location, myLocation))
{
//獲取緯度
double lat = location.getLatitude();
//獲取經度
double lon = location.getLongitude();
                                //位置提供者
String provider = location.getProvider();
                                //位置的准確性
float accuracy = location.getAccuracy();
//高度信息
double altitude = location.getAltitude();
//方向角
float bearing = location.getBearing();
//速度 米/秒
float speed = location.getSpeed();

String locationTime = sdf.format(new Date(location.getTime()));
String currentTime = null;
 
if (myLocation != null)

currentTime = sdf.format(new Date(myLocation.getTime()));
myLocation =location; 

}
else 
{
myLocation =location; 


loc.setText("經度:" + lon 
+ "\n緯度:" + lat 
+ "\n服務商:"+ provider
+ "\n准確性:"+ accuracy
+ "\n高度:"+ altitude
+ "\n方向角:"+ bearing
+ "\n速度:"+ speed 
+ "\n上次上報時間:"+currentTime
+ "\n最新上報時間:"+locationTime);  


}





}


@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e("onStatusChanged", "onStatusChanged: " + provider);


}


@Override
public void onProviderEnabled(String provider)
{
Log.e("onProviderEnabled", "onProviderEnabled: " + provider);
}


@Override
public void onProviderDisabled(String provider)
{
Log.e("onProviderDisabled", "onProviderDisabled: " + provider);
}


};


private static final int TWO_MINUTES = 1000 * 1 * 2;


/**
* Determines whether one Location reading is better than the current
* Location fix

* @param location
*            The new Location that you want to evaluate
* @param currentBestLocation
*            The current Location fix, to which you want to compare the new
*            one
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation)
{
if (currentBestLocation == null)
{
// A new location is always better than no location
return true;
}


// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
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;
// If the new location is more than two minutes older, it must be
// worse
}
else if (isSignificantlyOlder)
{
return false;
}


// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;


// Check if the old and new location are from the same provider
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;
}


/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2)
{
if (provider1 == null)
{
return provider2 == null;
}
return provider1.equals(provider2);
}


}
From:CSDN

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()