1
我想從最好的供應商那裏得到位置。我已啓用GPS。另外,當我打印經緯度時,我從網絡提供商處獲得。如何從GPS提供者而不是網絡提供者訪問位置?
我的問題是:
如果啓用了GPS,那麼我要搜索30秒,通過GPS的位置。之後,如果我的精度低於200米,那我就用它。如果精度超過200米,那麼我再次搜索並從網絡提供商開始。
之後,我比較兩個準確性,並採取更準確的供應商的數據。
這裏是我的代碼:
LocationUtil.java
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
public class LocationUtil
{
Activity activity;
Location location;
public LocationUtil(Activity activity)
{
this.activity = activity;
}
public int getLogitudeE6()
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
int lg = (int) (((double) location.getLongitude()) * 1E6);
System.out.println("Longitude :: " + lg);
return lg;
}
public int getLatitudeE6()
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
int lt = (int) (((double) location.getLatitude()) * 1E6);
System.out.println("Latitude :: " + lt);
return lt;
}
public double getLogitude(Location location)
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
this.location = location;
}
return location.getLongitude();
}
public double getLatitude(Location location)
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
this.location = location;
}
return location.getLatitude();
}
public double getAccuracy(Location location)
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
this.location = location;
}
return location.getAccuracy();
}
public double getLogitude()
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
this.location = location;
}
return location.getLongitude();
}
public double getLatitude()
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
this.location = location;
}
return location.getLatitude();
}
}
CaptureMain.java
import java.util.Timer;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
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 CaptureMain extends Activity implements LocationListener {
/** Called when the activity is first created. */
TextView txtNewLocation;
String strLatBack, strLongBack, strLatitude, strLongitude,strAccuracy;
LocationUtil locationUtil;
Location location;
LocationManager lm;
String bestProvider;
Timer timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtNewLocation = (TextView) findViewById(R.id.txtLocation);
locationUtil = new LocationUtil(this);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
bestProvider = lm.getBestProvider(criteria, false);
Log.i("Log", "Best provider is : "+bestProvider);
strLatitude = String.valueOf(locationUtil.getLatitude(location));
strLongitude = String.valueOf(locationUtil.getLogitude(location));
strAccuracy = String.valueOf(locationUtil.getAccuracy(location));
txtNewLocation.setText("Latitude :" + strLatitude + ", Longitude :"
+ strLongitude + " , Accuracy "+ strAccuracy);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
System.out.println("On Location change called:: ");
if (location != null) {
this.location = location;
}
strLatitude = String.valueOf(locationUtil.getLatitude(location));
strLongitude = String.valueOf(locationUtil.getLogitude(location));
strAccuracy = String.valueOf(locationUtil.getAccuracy(location));
if (strLatitude.equals("") || strLongitude.equals("")) {
txtNewLocation.setText("Locating current position..");
} else {
txtNewLocation.setText("Latitude :" + strLatitude + ", Longitude :"
+ strLongitude + " , Accuracy "+ strAccuracy);
}
}
@Override
protected void onStart() {
super.onStart();
startListening();
}
@Override
protected void onResume() {
super.onResume();
startListening();
}
@Override
protected void onPause() {
super.onPause();
stopListening();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopListening();
finish();
}
private void stopListening() {
if (lm != null)
lm.removeUpdates(this);
}
private void startListening() {
lm.requestLocationUpdates(bestProvider, 0, 0, this);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
新增許可清單中:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />