您的位置:首页 > 移动开发 > Android开发

Android上通过百度地图API实现选择城市/地点/距离/选择类型 来选择一定半径内的兴趣点

2014-03-06 19:22 337 查看


package baidumapsdk.demo;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionInfo;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;

/**
* 演示poi搜索功能
*/
public class PoiSearchDemo extends Activity {

private MapView mMapView = null;
private MKSearch mSearch = null;   // 搜索模块,也可去掉地图模块独立使用
/**
* 搜索关键字输入窗口
*/
private AutoCompleteTextView keyWorldsView = null;
private ArrayAdapter<String> sugAdapter = null;
private int load_Index;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoApplication app = (DemoApplication)this.getApplication();
if (app.mBMapManager == null) {
app.mBMapManager = new BMapManager(this);
app.mBMapManager.init(DemoApplication.strKey,new DemoApplication.MyGeneralListener());
}
setContentView(R.layout.activity_poisearch);
mMapView = (MapView)findViewById(R.id.bmapView);
mMapView.getController().enableClick(true);
mMapView.getController().setZoom(12);
mMapView.setBuiltInZoomControls(true);   //设置启用内置的缩放控件

// 初始化搜索模块,注册搜索事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapManager, new MKSearchListener(){
//在此处理详情页结果
@Override
public void onGetPoiDetailSearchResult(int type, int error) {
if (error != 0) {
Toast.makeText(PoiSearchDemo.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(PoiSearchDemo.this, "成功,查看详情页面", Toast.LENGTH_SHORT).show();
}
}
/**
* 在此处理poi搜索结果
*/

public void onGetPoiResult(MKPoiResult res, int type, int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(PoiSearchDemo.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
return;
}
// 将地图移动到第一个POI中心点
if (res.getCurrentNumPois() > 0) {
// 将poi结果显示到地图上
MyPoiOverlay poiOverlay = new MyPoiOverlay(PoiSearchDemo.this, mMapView, mSearch);
poiOverlay.setData(res.getAllPoi());
mMapView.getOverlays().clear();
mMapView.getOverlays().add(poiOverlay);
mMapView.refresh();
//当ePoiType为2(公交线路)或4(地铁线路)时, poi坐标为空
for( MKPoiInfo info : res.getAllPoi() ){
if ( info.pt != null ){
mMapView.getController().animateTo(info.pt);
break;
}
}
} else if (res.getCityListNum() > 0) {
//当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = "在";
for (int i = 0; i < res.getCityListNum(); i++) {
strInfo += res.getCityListInfo(i).city;
strInfo += ",";
}
strInfo += "找到结果";
Toast.makeText(PoiSearchDemo.this, strInfo, Toast.LENGTH_LONG).show();
}
}
public void onGetDrivingRouteResult(MKDrivingRouteResult res,
int error) {
}
public void onGetTransitRouteResult(MKTransitRouteResult res,
int error) {
}
public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int error) {
}
public void onGetAddrResult(MKAddrInfo res, int error) {
if (error != 0) {
String str = String.format("错误号:%d", error);
Toast.makeText(PoiSearchDemo.this, str, Toast.LENGTH_LONG).show();
return;
}
//地图移动到该点
mMapView.getController().animateTo(res.geoPt);	//得到geocode函数调用得到的经纬度信息
if (res.type == MKAddrInfo.MK_GEOCODE){
//地理编码:通过地址检索坐标点
GeoPoint position = new GeoPoint (res.geoPt.getLatitudeE6(),res.geoPt.getLongitudeE6());
// mSearch.poiSearchNearBy("KFC", position, 5000);//可用
EditText editSearchKey = (EditText)findViewById(R.id.searchkey); //搜索关键字
EditText editSearchDistance = (EditText)findViewById(R.id.distance); //搜索半径
int dis =Integer.parseInt(editSearchDistance.getText().toString());
mSearch.poiSearchNearBy(editSearchKey.getText().toString(), position, 5000);//
//String strInfo = String.format("纬度:%f 经度:%f", res.geoPt.getLatitudeE6()/1e6, res.geoPt.getLongitudeE6()/1e6);
//Toast.makeText(PoiSearchDemo.this, strInfo, Toast.LENGTH_LONG).show(); //这里注释了
//mSearch.poiSearchNearBy("餐厅",new GeoPoint(res.geoPt.getLatitudeE6(), res.geoPt.getLongitudeE6()) , 5000);//

//MyPoiOverlay poiOverlay = new MyPoiOverlay(GeoCoderDemo.this, mMapView, mSearch);

}
}

public void onGetBusDetailResult(MKBusLineResult result, int iError) {
}
/**
* 更新建议列表
*/
@Override
public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
if ( res == null || res.getAllSuggestions() == null){
return ;
}
sugAdapter.clear();
for ( MKSuggestionInfo info : res.getAllSuggestions()){
if ( info.key != null)
sugAdapter.add(info.key);
}
sugAdapter.notifyDataSetChanged();

}
@Override
public void onGetShareUrlResult(MKShareUrlResult result, int type,
int error) {
// TODO Auto-generated method stub

}
});

keyWorldsView = (AutoCompleteTextView) findViewById(R.id.searchkey);
sugAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);
keyWorldsView.setAdapter(sugAdapter);

/**
* 当输入关键字变化时,动态更新建议列表
*/
keyWorldsView.addTextChangedListener(new TextWatcher(){

@Override
public void afterTextChanged(Editable arg0) {

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {

}
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
if ( cs.length() <=0 ){
return ;
}
String city =  ((EditText)findViewById(R.id.city)).getText().toString();
/**
* 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新
*/
mSearch.suggestionSearch(cs.toString(), city);
}
});

}

@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}

@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}

@Override
protected void onDestroy(){
mMapView.destroy();
mSearch.destory();
super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}

private void initMapView() {
mMapView.setLongClickable(true);
mMapView.getController().setZoom(14);
mMapView.getController().enableClick(true);
mMapView.setBuiltInZoomControls(true);
}
/**
* 影响搜索按钮点击事件
* @param v
*/
public void searchButtonProcess(View v) {
EditText editCity = (EditText)findViewById(R.id.city); //搜索城市名

//从这里开始添加的
// EditText editGeoCodeKey = (EditText)findViewById(R.id.geocodekey);
// mBtnGeoCode.setOnClickListener(clickListener); //

EditText editSearchKey = (EditText)findViewById(R.id.searchkey);
EditText editGeoCodeKey = (EditText)findViewById(R.id.geocodekey); //把通过EditText控件(在布局文件中)输入的内容取出来,搜索的地点名
mSearch.geocode(editGeoCodeKey.getText().toString(), editCity.getText().toString());  //注意用.getText().toString()把输入的信息转化为string来调用,且调用这个函数得到的经纬度信息要通过调用onGetAddrResult得到
// mSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.920 * 1E6), (int) (116.410 * 1E6)), 5000);//
// mSearch.poiSearchNearBy("KFC", position, 5000);//
//mSearch.poiSearchInCity(editCity.getText().toString(),
// editSearchKey.getText().toString());
}
public void goToNextPage(View v) {
//搜索下一组poi
int flag = mSearch.goToPoiPage(++load_Index);
if (flag != 0) {
Toast.makeText(PoiSearchDemo.this, "先搜索开始,然后再搜索下一组数据", Toast.LENGTH_SHORT).show();
}
}
}


布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout

android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" >

<!--   <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在" >
</TextView>  -->

<EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="北京" />
<EditText
android:id="@+id/geocodekey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微软大厦" />
<EditText
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="找" >
</TextView>

<AutoCompleteTextView
android:id="@+id/searchkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.88"
android:text="餐厅" />
</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<Button
android:id="@+id/search"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="12"
android:padding="10dip"
android:background="@drawable/button_style"
android:onClick="searchButtonProcess"
android:text="开始" />

<!--  <Button
android:id="@+id/map_next_data"
android:layout_width="fill_parent"
android:layout_weight="12"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="@drawable/button_style"
android:onClick="goToNextPage"
android:text="下一组数据" /> -->
</LinearLayout>

<com.baidu.mapapi.map.MapView android:id="@+id/bmapView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:clickable="true"
/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: