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

应用场景:

在主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,有错误还望指正,谢谢。

时间: 2024-10-20 18:52:15

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

高德地图-搜索服务-POI搜索

高德地图-搜索服务-POI搜索 之前公司项目收货地址仿饿了么的收货地址,结果发现自己实现的关键字搜索和周边搜索,搜索到的poi列表跟饿了么的并不完全一样,后来考虑了下,应该是搜索的范围.类型之类的设置的不一样.后来搜索了下,也的确是这样,,现在记录下来,留给自己也留给大家. 关键字搜索 周边搜索 区域搜索 高德地图 Android SDK 提供了千万级别的POI(Point Of Interesting,兴趣点)的搜索. 目前支持关键字搜索.周边搜索.指定区域搜索的同步.异步方法.搜索返回 PO

高德地图-展示多个信息窗口

1.问题背景 高德地图,设置小图标,并点击图标显示信息 2.实现源码 <!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="init

如何把高德地图搜索结果导出成excel里?

前段时间,根据朋友的要求,开发了百度地图里的商家采集工具(博客地址:http://www.cnblogs.com/mymapw/p/5596122.html),又开发了腾讯地图里的商家采集工具(博客地址:http://www.cnblogs.com/mymapw/p/5622077.html),来了兴致,干脆把高德地图的采集工具也一并搞定!:) 原理应该差不多,我就查阅了下高德地图的采集规则,编写了专门针对高德地图的采集软件. 不过界面上和百度地图.腾讯地图的采集软件有了一些不同 粗略采集了下,

如何把高德地图搜索商家电话资料导出成excel里?

有很多人问我,怎么样能够快速的把高德地图左边的搜索列表里的商家地图,电话,导出到EXCEL里. 如何快速地将高德地图里的商家电话资料导出EXCEL? 操作步骤: 1. 选择你要采集的省份, 城市列表里就会有相应的省份的城市列表. 2. 选择要采集的城市,比如: 济南 3. "同时保存为Excel"  选项, 采集完毕后,程序首先会保存一份access数据库文件, 如果这里的对勾打上,则会在相应的文件夹下再产生一个EXCEL文件. 4. "打开文件位置", 采集完毕后

高德地图 定位和位置信息获取

- (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBarHidden = YES; _mapView = [[MAMapView alloc] init]; _mapView.frame  =CGRectZero; [self.view addSubview:_mapView]; _mapView.showsUserLocation = YES; } -(void)mapView:(MAM

高德地图——搜索

地图SDK提供了一系列的接口来方便用户获取高德LBS数据,目前主要支持的检索有: ? 兴趣点搜索: 1. 关键字搜索. 2. 周边搜索. 3. 指定区域搜索. 4. 团购/优惠搜索. 5. POI深度信息. ? 输入提示搜索. ? 路径规划 ? 公交线路查询. ? 坐标/地址查询. 关键字搜索 搜索条件设置 您需要通过 PoiSearch.Query(String query, String ctgr, String city) 设置搜索条件. 参数" query" 为搜索的关键字,&

高德地图点击查询信息定位到点+异步加载点+点击点显示信息窗口

一:配置地图 根据官方步骤申请key 可以异步或者同步调用地图,此处Wie同步 html <div id="container" tabindex="0"></div> js //设置中心位置,显示当前城市的中心点 var map = new AMap.Map('container', { resizeEnable: true, //center: [117.031479, 36.66314],//定位的济南//center缺省则根据IP自动

高德地图搜索 用tablevie列表显示

#import "ViewController2.h" #import <AMapSearchKit/AMapSearchAPI.h> @interface ViewController2 ()<AMapSearchDelegate,UITableViewDataSource,UITableViewDelegate> { UITextField *_textFile; AMapSearchAPI *_search; AMapPlaceSearchRequest

高德地图返回地址详细信息

个人习惯,上图 关于高德地图自动定位返回地址详细信息一直没写,一方面太忙了(也可以说太懒了),另一方面这个地方的内容太少,因为项目后面会用到快速搜索提示,往地图中添加marker.以及导航以及语音提示等等 本来想等项目上线在好好总结一下,算了不想拖了, 上代码: package com.example.mydemo; import android.app.Activity;import android.location.Location;import android.os.Bundle;impo