我正试图通过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
}
}
}
感谢您的回答!
雷
答案 0 :(得分:0)
你可能只是一个微弱的信号。您可以使用android location API获取大致位置。
确保你站在外面,可以自由地看到天空,以获得“良好”的信号。不要站在建筑物等旁边。但即使这样做,也不会得到准确的信号。手机GPS'针对节能而非精确度进行了优化。他们也不会在你的手机上放一个太贵的芯片......