高德地图搜索提示获取信息回传activity刷新ui(二)

高德地图搜索提示获取信息回传activity刷新ui(二)_第1张图片

应用场景:

在主activity中点击进入到另一个activity搜索提示,获取经纬度,点确定返回到主activity,虽然说需求很奇葩,但是遇到了没办法。。

主要包含两部分,搜索提示+activity回退携带信息刷新主acitivity.

 

1>搜索提示 代码中贴到

2>activity进入到另一个activity,在另一个activity进行操作获取数据之后,回到到activity刷新页面:

主activity里面点击: 

  Intent intent = new Intent(MainActivity.this,
  SearchActivity.class);
  startActivityForResult(intent, 0);//相当于addFlag

  根据对生命周期的理解,重写

  

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
       try {
      if (data != null) {//获取子activity put的数据
      String str = data.getStringExtra("addressName");
      mAddress.setText(str);
    }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

=======

另一个activity里面:

  

/**
* 向主activity携带的地址信息
*/
  Intent intent = new Intent(SearchActivity.this,
                 MainActivity.class);
  intent.putExtra("addressName", str);//自己放数据

  setResult(RESULT_OK, intent);
  finish();

上完整代码:

MainActivity:

package com.onebelowzero2012;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeResult;

public class MainActivity extends Activity implements
OnGeocodeSearchListener, AMapLocationListener {

// 定位
private LocationManagerProxy mLocationManger;
private TextView mTxt,mAddress;
private GeocodeSearch geocoderSearch;
//声明mLocationOption对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initLocation();
}
private void init() {
// TODO Auto-generated method stub
mTxt = (TextView) findViewById(R.id.tv_txt1);//自动定位的
mAddress = (TextView) findViewById(R.id.tv_txt2);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
SearchActivity.class);
startActivityForResult(intent, 0);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

try {
if (data != null) {//获取子activity put的数据
String str = data.getStringExtra("addressName");
mAddress.setText(str);
double longitude = data.getDoubleExtra("longitude", 0);
double latitude = data.getDoubleExtra("latitude", 0);
JSONObject location_info = new JSONObject();
location_info.put("__type", "GeoPoint");
location_info.put("latitude", latitude);
location_info.put("longitude", longitude);
Data.putData("location_info", location_info);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 初始化定位
*/
private void initLocation() {
// TODO Auto-generated method stub
mLocationManger = LocationManagerProxy.getInstance(getApplicationContext());
mLocationManger.requestLocationData(LocationProviderProxy.AMapNetwork,
60 * 1000, 15, this);
mHandler.postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
stopLocation();
}
}, 15000);// 设置超过15秒还没有定位到就停止定位

geocoderSearch=new GeocodeSearch(getApplicationContext());
geocoderSearch.setOnGeocodeSearchListener(this);
}

/**
* 重写方法
*/
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

}
@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

}
@Override
public void onLocationChanged(AMapLocation amapLocation) {
// TODO Auto-generated method stub
if (amapLocation != null) {
Double Latitude = amapLocation.getLatitude();
Double Longitude = amapLocation.getLongitude();

Bundle bundle = amapLocation.getExtras();
String desc = "";
String cityCode = "";
if(null!=bundle){
desc = bundle.getString("desc");
}
String str = ("定位成功:(" + Longitude + "," + Latitude + ")"
+ "\n精 度 :" + amapLocation.getAccuracy() + "米"
+ "\n定位方式:" + amapLocation.getProvider() + "\n定位时间:"
+ AMapUtil.convertToTime(amapLocation.getTime()) + "\n城市编码:"
+ cityCode + "\n位置描述:" + desc + "\n省:"
+ amapLocation.getProvince() + "\n市:" + amapLocation.getCity()
+ "\n区(县):" + amapLocation.getDistrict() + "\n区域编码:" + amapLocation
.getAdCode());
String address="位置描述:" + desc + "省:"
+ amapLocation.getProvince() + "市:" + amapLocation.getCity()
+ "区(县):" + amapLocation.getDistrict() + "区域编码:" + amapLocation
.getAdCode();

String wk= amapLocation.getDistrict();
Log.i("打印的数据是是是是", address);
Log.i("打印的数据是是是是", address);
Log.i("打印的数据是是是是", address);
Log.i("打印的数据是是是是", address);

mTxt.setText(amapLocation.getCity());
}
}
@Override
public void onGeocodeSearched(GeocodeResult arg0, int arg1) {
// TODO Auto-generated method stub

}
@Override
public void onRegeocodeSearched(RegeocodeResult arg0, int arg1) {
// TODO Auto-generated method stub

}

private Handler mHandler=new Handler(){};
private Button btn;
/**
* 销毁定位
*/
private void stopLocation() {
if (mLocationManger != null) {
mLocationManger.removeUpdates(this);
mLocationManger.destory();
}
mLocationManger = null;
}
}

searchActivity:

package com.onebelowzero2012;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Inputtips.InputtipsListener;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiSearch;

public class SearchActivity extends Activity implements OnItemClickListener,
TextWatcher, OnGeocodeSearchListener, OnClickListener {

private int mCount;
private ListView lv;
private Drawable mDelete;
private EditText txtFind;
private int currentPage = 0;// 当前页面,从0开始计数
private Inputtips inputTips;
private SearchAdapter adapter;
private PoiSearch poiSearch;// POI搜索
private String keyWord = "";// 要输入的poi搜索关键字
ArrayList<Map<String, Object>> mList;
private static GeocodeSearch geocoderSearch;// 经纬度搜索
private ImageView ivBack, ivDelete, ivSearch;
private PoiSearch.Query query;// Poi查询条件类
// key和id
public static final String KEY[] = new String[] { "ivDaoH", "txtFind",
"ivCome" };

private final int ID[] = new int[] { R.id.iv_daoh, R.id.txtfind,
R.id.iv_come };

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

initView();
}

/**
* 搜索
*/
private void doSearch() {
final String str = (String) mList.get(0).get(SearchActivity.KEY[1]);
Log.v("天安门广场吃泡面", str);
getLatlon(str);

mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
/**
* 向主activity携带的地址信息
*/
Intent intent = new Intent(SearchActivity.this,
MainActivity.class);
intent.putExtra("addressName", str);
intent.putExtra("longitude",
(Double) Data.getData("longitude"));
intent.putExtra("latitude",
(Double) Data.getData("latitude"));
setResult(RESULT_OK, intent);
finish();
}
}, 200);
}

private Handler mHandler = new Handler() {
};

/**
* 初始化view
*/
private void initView() {
// TODO Auto-generated method stub
lv = (ListView) findViewById(R.id.lv);
lv.setOnItemClickListener(this);

txtFind = (EditText) findViewById(R.id.txtfind);
ivDelete = (ImageView) findViewById(R.id.iv_delete);
ivSearch = (ImageView) findViewById(R.id.iv_search);
ivBack = (ImageView) findViewById(R.id.iv_back);

txtFind.addTextChangedListener(this);
ivDelete.setOnClickListener(this);
ivSearch.setOnClickListener(this);
ivBack.setOnClickListener(this);

mList = new ArrayList<Map<String, Object>>();
mCount = mList.size();
// 在初始化方法中设置图片的颜色
ivDelete.setImageResource(R.drawable.delete_item);
mDelete = this.getResources().getDrawable(
R.drawable.delete_item_unselector);
// 初始化中设置GeocodeSearch侦听 请求获取经纬度
geocoderSearch = new GeocodeSearch(SearchActivity.this);
geocoderSearch.setOnGeocodeSearchListener(this);
}

/**
* 获取地理坐标
*/
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
// TODO Auto-generated method stub
if (rCode == 0) {
if (result != null && result.getGeocodeAddressList() != null
&& result.getGeocodeAddressList().size() > 0) {

GeocodeAddress address = result.getGeocodeAddressList().get(0);
LatLonPoint latLonPoint = address.getLatLonPoint();
try {
double latitude = latLonPoint.getLatitude();
double longitude = latLonPoint.getLongitude();

Data.putData("latitude", latitude);
Data.putData("longitude", longitude);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
ToastUtil.show(getApplicationContext(), "高德地图获取地理坐标出错!");
}
}

@Override
public void onRegeocodeSearched(RegeocodeResult arg0, int arg1) {
// TODO Auto-generated method stub

}

/**
* 文字输入提示
*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String newText = s.toString().trim();
if (newText != null) {
adapter = new SearchAdapter(SearchActivity.this);
mList.clear();
adapter.setDataSource(mList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
inputTips = new Inputtips(SearchActivity.this, new InputtipsListener() {

@Override
public void onGetInputtips(List<Tip> tipList, int rCode) {
if (rCode == 0) {// 正确返回
for (int i = 0; i < tipList.size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(KEY[0], R.drawable.iv_daoh);// 加入图片
map.put(KEY[1], tipList.get(i).getName());// 文字
map.put(KEY[2], R.drawable.iv_come);
mList.add(map);
}

adapter = new SearchAdapter(SearchActivity.this);
adapter.setDataSource(mList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
});
try {
inputTips.requestInputtips(newText, "北京");// 第一个参数表示提示关键字,第二个参数默认代表全国,也可以为城市区号
} catch (AMapException e) {
e.printStackTrace();
}
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
final String str = (String) mList.get(position).get(
SearchActivity.KEY[1]);
getLatlon(str);

view.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
/**
* 向主activity携带的地址信息
*/
Intent intent = new Intent(SearchActivity.this,
MainActivity.class);
intent.putExtra("addressName", str);
intent.putExtra("longitude", (Double) Data.getData("longitude"));
intent.putExtra("latitude", (Double) Data.getData("latitude"));

setResult(RESULT_OK, intent);
finish();
}

}, 200);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_search:
searchButton();
break;
case R.id.iv_delete:// 删除时清空数据
txtFind.setText("");
adapter = new SearchAdapter(SearchActivity.this);
mList.clear();
adapter.setDataSource(mList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();

break;
case R.id.iv_back:// 点击回退到 fragment
this.finish();
break;
default:
break;
}
}

/**
* 点击搜索按钮
*/
public void searchButton() {
keyWord = ToastUtil.checkEditText(txtFind);
if ("".equals(keyWord)) {
ToastUtil.show(this, "请输入搜索关键字");
return;
} else {
// doSearchQuery();
doSearch();
}
}

/**
* 响应地理编码
*/
// 定于搜素城市编码 北京 010
public static String beijing = "010";

public static void getLatlon(final String name) {
GeocodeQuery query = new GeocodeQuery(name, beijing);// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode,
geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
}
}

 

...

其他的就不贴了,见完整代码连接:

http://download.csdn.net/detail/onebelowzero2012/9374719

注意修改key,有错误还望指正,谢谢。

 

你可能感兴趣的:(高德地图搜索提示获取信息回传activity刷新ui(二))