我开发了一款应用程序,在使用Google API进行定位时工作正常,但当我只在智能手机中选择GPS时,它说没有位置。我的下面的代码有什么问题:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(theButtonListener);
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
boolean enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(enabled)
{
//Log.d(TAG,"YES enabled");
Toast.makeText(MainActivity.this, "Enabled", 2000).show();
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Toast.makeText(MainActivity.this, provider.toString(), 5000).show();
Location location = locationManager.getLastKnownLocation(provider);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
//Log.d(TAG, location.toString());
Toast.makeText(MainActivity.this, location.toString(), 10000).show();
onLocationChanged(location); //<6>
}
else
Toast.makeText(MainActivity.this, "No location", 5000).show();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//locationManager.requestLocationUpdates(provider, 400, 1, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d(TAG, "onLocationChanged with location " + location.toString());
String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
location.getLongitude(), location.getAltitude(), location.getBearing());
Toast.makeText(MainActivity.this, text, 10000).show();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Toast.makeText(MainActivity.this, provider.toString(), 5000).show();
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
//Log.d(TAG, location.toString());
Toast.makeText(MainActivity.this, location.toString(), 10000).show();
onLocationChanged(location); //<6>
}
else
Toast.makeText(MainActivity.this, "No location", 5000).show();
}
};
顺便说一句,谷歌API如何在内部运作,它如何找到我的LAT和LON?
答案 0 :(得分:0)
onLocationChanged是一个回调,意味着当位置发生变化时系统会调用它。它是事件驱动的,你不应该明确地调用它。你必须等到GPS得到修复,你不能立即得到一个。当应用程序首次运行时,可能没有“最后一个位置”,因此它将返回null。