如何在子类中使用 Geocoder
How Geocoder is used in subclass
如何在 subclass 中使用 Geocoder class 我应该在其上下文中使用什么值。以下是我的代码,
parent class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.distance_foam);
setAllAttributes();
dataSource = new PlacesDataSource(this);
dataSource.open();
}
private void setAllAttributes() {
backButton = (TextView) findViewById(R.id.textViewBackButton);
milesEditText = (EditText) findViewById(R.id.saveDistance);
minsEditText = (EditText) findViewById(R.id.saveMinutes);
phoneEditText = (EditText) findViewById(R.id.phoneNo);
msgEditText = (EditText) findViewById(R.id.textMsg);
storeDetailButton = (Button) findViewById(R.id.storeData);
storeDetailButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
Long.parseLong(mins),
Float.parseFloat(miles),
locationListener);
}
subclass
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
String title = null;
//error
Geocoder geo = new Geocoder(this);
Places place = new Places();
StringBuffer description = new StringBuffer();
description.append("http://maps.google.com?q=");
description.append(location.getLatitude());
description.append(",");
description.append(location.getLongitude());
try {
List<Address> list = geo.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
Address add = list.get(0);
title = add.getLocality();
} catch (IOException e) {
Log.i(LOGTAG, "does not find the current location");
e.printStackTrace();
place.setLatitude(location.getLatitude());
place.setLongitude(location.getLongitude());
place.setZoom(DEFAULT_ZOOM);
place.setTitle(title);
place.setDescription(description.toString());
place = dataSource.updateCreate(place);
Log.i(LOGTAG, "data is created with id: " + place.getId());
}
}
@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
}
通过将 "this" 放入 Geocoder 的初始化中,它给我一个明显的错误,我还构建了构造函数,但它也给了我一个 运行 时间错误。请告诉我应该用地理编码器做什么。谢谢
改变这个:
Geocoder geo = new Geocoder(this);
对此:
Geocoder geo = new Geocoder(getApplicationContext());
如何在 subclass 中使用 Geocoder class 我应该在其上下文中使用什么值。以下是我的代码,
parent class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.distance_foam);
setAllAttributes();
dataSource = new PlacesDataSource(this);
dataSource.open();
}
private void setAllAttributes() {
backButton = (TextView) findViewById(R.id.textViewBackButton);
milesEditText = (EditText) findViewById(R.id.saveDistance);
minsEditText = (EditText) findViewById(R.id.saveMinutes);
phoneEditText = (EditText) findViewById(R.id.phoneNo);
msgEditText = (EditText) findViewById(R.id.textMsg);
storeDetailButton = (Button) findViewById(R.id.storeData);
storeDetailButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
Long.parseLong(mins),
Float.parseFloat(miles),
locationListener);
}
subclass
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
String title = null;
//error
Geocoder geo = new Geocoder(this);
Places place = new Places();
StringBuffer description = new StringBuffer();
description.append("http://maps.google.com?q=");
description.append(location.getLatitude());
description.append(",");
description.append(location.getLongitude());
try {
List<Address> list = geo.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
Address add = list.get(0);
title = add.getLocality();
} catch (IOException e) {
Log.i(LOGTAG, "does not find the current location");
e.printStackTrace();
place.setLatitude(location.getLatitude());
place.setLongitude(location.getLongitude());
place.setZoom(DEFAULT_ZOOM);
place.setTitle(title);
place.setDescription(description.toString());
place = dataSource.updateCreate(place);
Log.i(LOGTAG, "data is created with id: " + place.getId());
}
}
@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
}
通过将 "this" 放入 Geocoder 的初始化中,它给我一个明显的错误,我还构建了构造函数,但它也给了我一个 运行 时间错误。请告诉我应该用地理编码器做什么。谢谢
改变这个:
Geocoder geo = new Geocoder(this);
对此:
Geocoder geo = new Geocoder(getApplicationContext());