gpt4 book ai didi

android - 我的 Android 程序上的 GPS 坐标不准确

转载 作者:太空狗 更新时间:2023-10-29 15:20:22 24 4
gpt4 key购买 nike

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

问题是,要么坐标不准确(只有度数),要么没有给出位置。我现在编写了我的第三个程序,每个程序都有不同的教程,但它没有正常工作,因为它应该在那里工作。

已设置使用权限,所以这不是答案;)

这是我的代码:

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
}
}
}

感谢您的回答!

雷内

最佳答案

您收到的信号可能很弱。您可以使用 android location API 获取大概位置。

确保您站在室外并且可以自由地看到天空以获得“良好”信号。不要站在建筑物旁边等。但是,即使这样做,您也不会得到准确的信号。手机 GPS 针对节能而非准确性进行了优化。他们也不会在您的手机上安装太昂贵的芯片...

关于android - 我的 Android 程序上的 GPS 坐标不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com