Java 如何将地址从geocoder类发送到另一个活动?
我在我的应用程序中有一个特别的ChooseFromMapActivity。在这种情况下,地址是通过使用反向地理编码获取的,因此当我们移动地图时,位置会在下面的文本视图“使用位置布局”中更改。 如下图所示: 现在,我想在另一个活动的“编辑文本”视图中显示此特定的当前位置。如下图所示: 在这种情况下,在geocoder类中,位置存储在StringBuilder str变量中。所以我尝试从str中检索字符串并在文本视图中显示它。但它并不是文本视图中显示的完美地址。str变量只显示本地区域和邮政编码。我想在编辑文本视图中显示完美的地理编码值。所以使用StringBuilder变量是否正确??或者有没有其他方法来实现它 在这种情况下,纬度和经度存储在中心变量中。该中心变量为latlng变量。我们可以将latlng变量传递给另一个活动吗??我们如何在编辑文本视图中显示latlng变量地址?? 请帮帮我 从映射活动中选择Java 如何将地址从geocoder类发送到另一个活动?,java,android,dictionary,android-activity,reverse-geocoding,Java,Android,Dictionary,Android Activity,Reverse Geocoding,我在我的应用程序中有一个特别的ChooseFromMapActivity。在这种情况下,地址是通过使用反向地理编码获取的,因此当我们移动地图时,位置会在下面的文本视图“使用位置布局”中更改。 如下图所示: 现在,我想在另一个活动的“编辑文本”视图中显示此特定的当前位置。如下图所示: 在这种情况下,在geocoder类中,位置存储在StringBuilder str变量中。所以我尝试从str中检索字符串并在文本视图中显示它。但它并不是文本视图中显示的完美地址。str变量只显示本地区域和邮政编码
public class ChooseFromMapActivity extends AppCompatActivity implements
LocationListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private LocationRequest mLocationRequest;
GoogleMap mGoogleMap;
private GoogleApiClient mGoogleApiClient;
boolean mUpdatesRequested = false;
private LatLng center;
private LinearLayout markerLayout;
private Geocoder geocoder;
private List<Address> addresses;
private TextView Address;
double latitude;
double longitude;
private GPSTracker gps;
private LatLng curentpoint;
private LinearLayout useLocation;
Intent intent;
double x, y;
StringBuilder str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_from_map);
Address = (TextView) findViewById(R.id.textShowAddress);
markerLayout = (LinearLayout) findViewById(R.id.locationMarker);
useLocation = (LinearLayout)findViewById(R.id.LinearUseLoc);
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
if (status != ConnectionResult.SUCCESS) { // Google Play Services are
// not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();
} else { // Google Play Services are available
// Getting reference to the SupportMapFragment
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
/*
* Set the update interval
*/
mLocationRequest.setInterval(GData.UPDATE_INTERVAL_IN_MILLISECONDS);
// Use high accuracy
mLocationRequest
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest
.setFastestInterval(GData.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
// Note that location updates are off until the user turns them on
mUpdatesRequested = false;
/*
* Create a new location client, using the enclosing class to handle
* callbacks.
*/
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
}
useLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Intent intent = new Intent(ChooseFromMapActivity.this ,GoSend.class);
intent.putExtra("latitude",latitude);
intent.putExtra("Longitude",longitude);
startActivity(intent);*/
/// Bundle bundle = new Bundle();
// bundle.putSerializable("value",str);
// intent.putExtras(bundle);
intent = new Intent(ChooseFromMapActivity.this,GoSend.class);
String value=str.toString();
// intent.putExtra("value",value);
intent.putExtra("x",x);
intent.putExtra("y",y);
startActivity(intent);
// intent.putExtra("string", (Serializable) str);
// Bundle args = new Bundle();
//args.putParcelable("from_position",curentpoint);
// intent.putExtra("bundle", args);
//Bundle bundle = new Bundle();
}
});
}
private void stupMap() {
try {
mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// Enabling MyLocation in Google Map
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setCompassEnabled(true);
mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
gps = new GPSTracker(this);
gps.canGetLocation();
latitude = gps.getLatitude();
longitude = gps.getLongitude();
curentpoint = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(curentpoint).zoom(19f).tilt(70).build();
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Clears all the existing markers
mGoogleMap.clear();
mGoogleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
// TODO Auto-generated method stub
center = mGoogleMap.getCameraPosition().target;
mGoogleMap.clear();
markerLayout.setVisibility(View.VISIBLE);
try {
new GetLocationAsync(center.latitude, center.longitude)
.execute();
} catch (Exception e) {
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
stupMap();
}
private class GetLocationAsync extends AsyncTask<String, Void, String> {
// boolean duplicateResponse;
public GetLocationAsync(double latitude, double longitude) {
// TODO Auto-generated constructor stub
x = latitude;
y = longitude;
}
@Override
protected String doInBackground(String... params) {
try {
geocoder = new Geocoder(ChooseFromMapActivity.this, Locale.ENGLISH);
addresses = geocoder.getFromLocation(x, y, 1);
str = new StringBuilder();
if (Geocoder.isPresent()) {
if ((addresses != null) && (addresses.size() > 0)) {
Address returnAddress = addresses.get(0);
String localityString = returnAddress.getLocality();
String city = returnAddress.getCountryName();
String region_code = returnAddress.getCountryCode();
String zipcode = returnAddress.getPostalCode();
str.append(localityString + "");
str.append(city + "" + region_code + "");
str.append(zipcode + "");
}
} else {
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
Address.setText(addresses.get(0).getAddressLine(0)
+ addresses.get(0).getAddressLine(1) + " ");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
}
您可以将地址作为字符串从一个活动传递到另一个活动,方法是将该地址设置为用于启动其他活动的目的,如
Intent intent = new Intent(ChooseFromMapActivity.this, GoSend.class);
intent.putExtra("addressTag", stringAddress);
startActivity(intent);
另一方面,在接受活动中,你可以这样得到它
Bundle bundle = getIntent().getExtras();
if(bundle != null)
String stringAddress = bundle.getString("addressTag");
更简单的方法是创建一个名为Address的模型类
class Address implements Serializable{
String Street;
String City;
//...Fileds
}
在此类中填写信息,并将其作为可序列化对象发送到intent。我已经为SringBuilder变量完成了此操作。。我的coe是:intent=newintent(选择fromMapActivity.this,GoSend.class);字符串值=str.toString();意向。额外(“价值”,价值);在另一个活动上:Intent-Intent=this.getIntent();字符串数据=intent.getStringExtra(“值”);edtxt_来自.setText(数据);星触觉(意向);如果它不工作,用我提供的代码替换它,并更新我的地址,如MiraBhyndarIndialNull。。它在文本视图中显示为c-47,swaminarayan rdSector 3,Mira road east@Zubair-Akbermake确保您通过的值与设置到文本视图的值相同我不确定文本视图的值。。您能看一下代码并告诉我值吗???@祖贝尔·阿克伯
class Address implements Serializable{
String Street;
String City;
//...Fileds
}