我正在为学校开发一个Android项目。此活动应该获取GPS位置,并能够保存它。然后,用户稍后可以返回应用程序并从当前位置获取到该位置的路线。
对于Android开发者来说还是新手,以下内容仍在进行中。我需要在退出应用程序后保存pinLat和pinLong,以便在应用程序再次打开时可用。
以下是当前的活动:
public class LocationActivity extends Activity implements LocationListener{
// declare variables
float locLat = 0;
float locLong = 0;
float pinLat = 0;
float pinLong = 0;
TextView txtLat;
TextView txtLong;
TextView txtPinLat;
TextView txtPinLong;
LocationManager locationManager;
LocationListener locationListener;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// get a handle on the text views
txtLat = (TextView) findViewById(R.id.txtLat);
txtLong = (TextView) findViewById(R.id.txtLong);
txtPinLat = (TextView) findViewById(R.id.txtPinLat);
txtPinLong = (TextView) findViewById(R.id.txtPinLong);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Why cant I instantiate the LocationListener?
locationListener = new LocationListener();
}
public void onLocationChanged(Location location) {
//if it finds the location
if (location != null){
// save the values to float variables
locLat = (float) location.getLatitude();
locLong = (float) location.getLongitude();
// set the text views to the lat and long values
txtLat.setText(String.valueOf(locLat));
txtLong.setText(String.valueOf(locLong));
}
}
//save the current location to be a pin for the map
public void pinLocation(View v) throws IOException {
// when the button is pressed, save the current location lat and long
pinLat = locLat;
pinLong = locLong;
// set the text views to the lat and long values
txtPinLat.setText(String.valueOf(pinLat));
txtPinLong.setText(String.valueOf(pinLong));
}
//launch google maps to navigate to the pin
public void navigate(View v) {
//Toast.makeText(getApplicationContext(), "Navigate Called!", Toast.LENGTH_LONG).show();
//build the google maps URL
String uri = ("geo:"+ pinLat + "," + pinLong);
//add the lat and long
String query = pinLat + "," + pinLong;
//encode the URI
String encodedQuery = Uri.encode(query);
//continue building the uri
uri += "?q=" + encodedQuery;
//start the intent with the Uri
Intent navigate = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
//start the activity
startActivity(navigate);
}
//when the app resumes, turn on the GPS locations
public void onResume(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5,5, locationListener);
super.onResume();
}
//on pause, stop the GPS
@Override
public void onPause() {
locationManager.removeUpdates(locationListener);
super.onPause();
}
//GENERATED CODE. Need to alert if disabled etc
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
在onCreate方法中,我在locationListener = new LocationListener()上得到“无法实例化类型LocationListener”错误;线。我见过其他人在例子中使用了这一行。我错过了什么?
我的AndroidManifest中有权限,布局不应该导致任何问题..
谢谢!
答案 0 :(得分:1)
由于您已经implements LocationListener
您的活动已经是LocationListener,因此您不必创建新的LocationListener。
删除LocationListener locationListener;
在onResume方法上尝试将请求更新行更改为:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
onLocationChanged
应该触发,你可以从中获取位置。
答案 1 :(得分:0)
这更像是一般的Java问题。 LocationListener
不是Java类,它是一个接口。简单来说,这意味着没有方法的实现(即onLocationChanged
,onProviderEnabled
等)。这意味着你必须定义调用这些方法时会发生什么。
如果您熟悉处理Button
点击事件,那么您已经知道如何使用匿名内部类编写实现。 (AIC就像实现界面'即时#。)。
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//TODO -- Barney says what should happen
// when the framework invokes onLocationChanged
}
//TODO -- Barney implements the other three interface methods
)}; //Close AIC declaration, implementation and terminate line.