Android 输入纬度和经度并发送到其他活动
请帮帮我, 我想通过onclick按钮将此活动的纬度和经度坐标发送到另一个活动 如何将此活动发送Lat和Long,发送到另一个活动,另一个活动接收Lat和Long 对不起,我是新手( 这是我的课Android 输入纬度和经度并发送到其他活动,android,Android,请帮帮我, 我想通过onclick按钮将此活动的纬度和经度坐标发送到另一个活动 如何将此活动发送Lat和Long,发送到另一个活动,另一个活动接收Lat和Long 对不起,我是新手( 这是我的课 public class Maps extends FragmentActivity { GoogleMap googleMap; MarkerOptions markerOptions; Marker gpin = null; LatLng latLng; S
public class Maps extends FragmentActivity {
GoogleMap googleMap;
MarkerOptions markerOptions;
Marker gpin = null;
LatLng latLng;
String lat = "", lng = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps_layout);
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
googleMap = supportMapFragment.getMap();
// Getting reference to btn_find of the layout activity_main
Button btn_find = (Button) findViewById(R.id.btn_find);
// Defining button click event listener for the find button
OnClickListener findClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Getting reference to EditText to get the user input location
EditText etLocation = (EditText) findViewById(R.id.et_location);
// Getting user input location
String location = etLocation.getText().toString();
if(location!=null && !location.equals("")){
new GeocoderTask().execute(location);
}
}
};
// Setting button click event listener for the find button
btn_find.setOnClickListener(findClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
public void onMapClick(LatLng point) {
// // TODO Auto-generated method stub
if (gpin != null) {
gpin.remove();
}
gpin = googleMap.addMarker(new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).icon(
BitmapDescriptorFactory
.fromResource(R.drawable.ic_pin)));
lat = point.latitude + "";
lng = point.longitude + "";
}
});
}
}
}
}
公共类映射扩展了碎片活动{
谷歌地图谷歌地图;
标记选项标记选项;
标记gpin=null;
LatLng LatLng;
字符串lat=“”,lng=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.maps\u布局);
SupportMapFragment SupportMapFragment=(SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
//获取对地图的引用
googleMap=supportMapFragment.getMap();
//获取对布局活动\u main的btn\u查找的引用
按钮btn\u find=(按钮)findviewbyd(R.id.btn\u find);
//为“查找”按钮定义按钮单击事件侦听器
OnClickListener findClickListener=新建OnClickListener(){
@凌驾
公共void onClick(视图v){
//获取对EditText的引用以获取用户输入位置
EditText etLocation=(EditText)findViewById(R.id.et_位置);
//获取用户输入位置
字符串位置=etLocation.getText().toString();
if(location!=null&&!location.equals(“”){
新的GeocoderTask().execute(位置);
}
}
};
//设置按钮单击“查找”按钮的事件侦听器
btn_find.setOnClickListener(findClickListener);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单.主菜单,菜单);
返回true;
}
//用于访问地理编码Web服务的AsyncTask类
私有类GeocoderTask扩展异步任务{
@凌驾
受保护列表doInBackground(字符串…位置名称){
//创建Geocoder类的实例
Geocoder Geocoder=新的Geocoder(getBaseContext());
列表地址=空;
试一试{
//获取最多3个与输入文本匹配的地址
addresses=geocoder.getFromLocationName(locationName[0],3);
}捕获(IOE异常){
e、 printStackTrace();
}
返回地址;
}
@凌驾
受保护的void onPostExecute(列表地址){
if(addresses==null | | addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到位置”,Toast.LENGTH_SHORT.show();
}
//清除地图上的所有现有标记
googleMap.clear();
//在谷歌地图上为每个匹配地址添加标记
对于(int i=0;i 0?地址。getAddressLine(0):“”,
address.getCountryName());
markerOptions=新的markerOptions();
标记选项位置(板条);
标记选项。标题(地址文本);
googleMap.addMarker(markerOptions);
//找到第一个位置
如果(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
setOnMapClickListener(新的googleMap.OnMapClickListener(){
公共空区(停车点){
////TODO自动生成的方法存根
如果(gpin!=null){
gpin.remove();
}
gpin=googleMap.addMarker(新的MarkerOptions().position(
新车床(点.纬度,点.经度))图标(
位图描述符工厂
.fromResource(R.drawable.ic_-pin));
纬度=点纬度+“”;
lng=点经度+“”;
}
});
}
}
}
}
上述方法用于发送数据
然后在活动中使用这些值
Bundle extras = getIntent().getExtras();
if (extras != null) {
double value = extras.getDouble("LATITUDE_ID");
//The key argument here must match that used in the other activity
}
为了发送字符串或int值,可以使用putExtra(),但如果要发送LatLng,则需要使用bundle:
Bundle args = new Bundle();
args.putParcelable("customerLatLng", customerLatLng);
Intent intent=new Intent(OldActivity.this,NewActivity.class);
intent.putExtras(args);
startActivity(intent);
在NewActivity.class中:
Intent i = getIntent();
final Bundle extras = getIntent().getExtras();
LatLng customerLatLng = i.getParcelableExtra("customerLatLng");
我想把纬度和经度分为不同的部分,因为活动发送了坐标来执行这样的代码
public void run() {
// TODO Auto-generated method stub
String handleInserUrl = getResources().getString(
R.string.estate_json_url)
+ "estates";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(handleInserUrl);
MultipartEntity reqEntity = new MultipartEntity();
String titleText = title.getText().toString();
String priceText = price.getText().toString();
String contentText = content.getText().toString();
String bathroomText = bathroom.getText().toString();
String bedroomText = bedroom.getText().toString();
String areaText = area.getText().toString();
String lantaiText = lantai.getText().toString();
String luasbangunanText = luasbangunan.getText().toString();
String addressText = address.getText().toString();
reqEntity.addPart("address", new StringBody(
addressText));
reqEntity.addPart("user_id",
new StringBody(user.getId() + ""));
reqEntity.addPart("title",
new StringBody(titleText));
reqEntity.addPart("price",
new StringBody(priceText));
reqEntity.addPart("content", new StringBody(
contentText));
reqEntity.addPart("cities", new StringBody(
city_selected + ""));
reqEntity.addPart("types", new StringBody(
type_selected + ""));
reqEntity.addPart("county", new StringBody(
county_selected + ""));
reqEntity.addPart("purpose", new StringBody(
purpose_selected + ""));
reqEntity.addPart("statusproperti", new StringBody(
statusselected + ""));
reqEntity.addPart("area", new StringBody(areaText));
reqEntity.addPart("lantai", new StringBody(lantaiText));
reqEntity.addPart("luasbangunan", new StringBody(luasbangunanText));
reqEntity.addPart("bathrooms", new StringBody(
bathroomText));
reqEntity.addPart("bedrooms", new StringBody(
bedroomText));
reqEntity.addPart("time_rate", new StringBody(
time_rate_selected + ""));
reqEntity.addPart("marker", new StringBody(
marker_selected + ""));
reqEntity.addPart("lat", new StringBody(lat));
reqEntity.addPart("lng", new StringBody(lng));
在您的侦听器中,使用
startActivity()
并向其传递带有相关信息的Intent
public void run() {
// TODO Auto-generated method stub
String handleInserUrl = getResources().getString(
R.string.estate_json_url)
+ "estates";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(handleInserUrl);
MultipartEntity reqEntity = new MultipartEntity();
String titleText = title.getText().toString();
String priceText = price.getText().toString();
String contentText = content.getText().toString();
String bathroomText = bathroom.getText().toString();
String bedroomText = bedroom.getText().toString();
String areaText = area.getText().toString();
String lantaiText = lantai.getText().toString();
String luasbangunanText = luasbangunan.getText().toString();
String addressText = address.getText().toString();
reqEntity.addPart("address", new StringBody(
addressText));
reqEntity.addPart("user_id",
new StringBody(user.getId() + ""));
reqEntity.addPart("title",
new StringBody(titleText));
reqEntity.addPart("price",
new StringBody(priceText));
reqEntity.addPart("content", new StringBody(
contentText));
reqEntity.addPart("cities", new StringBody(
city_selected + ""));
reqEntity.addPart("types", new StringBody(
type_selected + ""));
reqEntity.addPart("county", new StringBody(
county_selected + ""));
reqEntity.addPart("purpose", new StringBody(
purpose_selected + ""));
reqEntity.addPart("statusproperti", new StringBody(
statusselected + ""));
reqEntity.addPart("area", new StringBody(areaText));
reqEntity.addPart("lantai", new StringBody(lantaiText));
reqEntity.addPart("luasbangunan", new StringBody(luasbangunanText));
reqEntity.addPart("bathrooms", new StringBody(
bathroomText));
reqEntity.addPart("bedrooms", new StringBody(
bedroomText));
reqEntity.addPart("time_rate", new StringBody(
time_rate_selected + ""));
reqEntity.addPart("marker", new StringBody(
marker_selected + ""));
reqEntity.addPart("lat", new StringBody(lat));
reqEntity.addPart("lng", new StringBody(lng));