我的安卓程序中GPS坐标不准确。

3

我正在尝试在我的程序中通过GPS获取手机的当前位置。

问题是,要么坐标太不准确(只有度数),要么没有位置信息。我现在已经编写了第三个程序,并使用了不同的教程,但它并没有像应该那样工作。

uses-permission已设置,所以这不是答案;)

这是我的代码:

public class GpsActivity extends Activity{

private TextView latituteField;
private TextView longitudeField;
private TextView dateField;
private TextView timeField;
private LocationManager locationManager;
private String provider;
private LocationListener listener; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    dateField = (TextView) findViewById(R.id.tvdate);
    timeField = (TextView) findViewById(R.id.tvtime);
    GregorianCalendar g = new GregorianCalendar();
    g.setTimeInMillis(System.currentTimeMillis());

    DateFormat df = DateFormat.getInstance();
    Date d = df.getCalendar().getTime();
    dateField.setText(""+d.getDate()+"."+(d.getMonth()+1)+"."+(1980+d.getYear()));
    timeField.setText(""+g.getTime().getHours()+"h "+g.getTime().getMinutes()+"m "+g.getTime().getSeconds()+"s");


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);

    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());

        latituteField.setText(""+lat+"//"+location.getAccuracy());
        longitudeField.setText(""+lng+"//"+location.getProvider());
    } else {
        latituteField.setText("Provider not available");
        longitudeField.setText("Provider not available");
    }
    listener = new MyLocationListener(); 
    locationManager.requestLocationUpdates("gps" ,10000L, 10.0f, listener); 

}

class MyLocationListener implements LocationListener{ 
    public void onLocationChanged(Location location) { 
            // TODO Auto-generated method stub 
            if (location != null){ 
                    latituteField.setText(("Lat: " + location.getLatitude())); 
                    longitudeField.setText("LLong: " + location.getLongitude());
            } 
    } 
    public void onProviderDisabled(String provider) { 

    } 
    public void onProviderEnabled(String provider) { 
            // TODO Auto-generated method stub 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
            // TODO Auto-generated method stub 
    } 
}
}

感谢您的回答!
Rene
1个回答

0

你可能只收到了一个微弱的信号。你可以使用安卓定位API获取大致位置。

确保你站在室外,有自由视野以获取“好”的信号。不要站在建筑物旁边等地方。即使这样做,你也无法得到准确的信号。手机GPS 优化是为了节省能源而非准确性。他们也不会在你的手机上安装价格太昂贵的芯片...


除了Udo的回答之外,不要忘记您可以查询位置修复的准确性...有关准确性的讨论在这里:https://dev59.com/5HA75IYBdhLWcg3w4tR7 - Basic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接