Android百度地图poi范围搜索

 更新时间:2016年02月05日 10:47:42   投稿:mrr  
这篇文章主要介绍了Android百度地图poi范围搜索的相关资料,需要的朋友可以参考下

我想大家可能都有过这样的经历:兜里揣着一张银行卡,在街上到处找自动取款机(ATM)。在这个场景中,ATM就是的兴趣点,我们想做的事情就是找到离自己较近的一些ATM然后取款,此时我们并不关心附近有哪些超市、酒吧,因为这些地方没办法取钱!

说了这么多,一方面是加深大家对POI这个词的认识,另一方面也是为了让大家明白我们接下来要做的事情。理论性的东西就不再多讲了,直接来看例子。

先给大家展示下效果图:

详细界面:

该示例主要介绍关键词查询、suggestion查询和查看餐饮类Place详情页功能,尤其搜索某个地方的餐厅、理发店等等比较有实际意义,百度Demo代码如下:

Activity:

package com.home;
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;
/**
* 演示poi搜索功能
*/
public class PoiSearchActivity 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);
// 初始化搜索模块,注册搜索事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapManager, new MKSearchListener() {
// 在此处理详情页结果
@Override
public void onGetPoiDetailSearchResult(int type, int error) {
if (error != 0) {
Toast.makeText(PoiSearchActivity.this, "抱歉,未找到结果",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PoiSearchActivity.this, "成功,查看详情页面",
Toast.LENGTH_SHORT).show();
}
}
/**
* 在此处理poi搜索结果
*/
public void onGetPoiResult(MKPoiResult res, int type, int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(PoiSearchActivity.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
}
// 将地图移动到第一个POI中心点
if (res.getCurrentNumPois() > 0) {
// 将poi结果显示到地图上
MyPoiOverlay poiOverlay = new MyPoiOverlay(
PoiSearchActivity.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(PoiSearchActivity.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) {
}
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) {
}
});
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();
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);
}
/**
* 影响搜索按钮点击事件
*
* @param v
*/
public void searchButtonProcess(View v) {
EditText editCity = (EditText) findViewById(R.id.city);
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
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(PoiSearchActivity.this, "先搜索开始,然后再搜索下一组数据",
Toast.LENGTH_SHORT).show();
}
}
}

布局XML(activity_poisearch):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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="北京" />
<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_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:background="@drawable/button_style"
android:onClick="searchButtonProcess"
android:padding="10dip"
android:text="开始" />
<Button
android:id="@+id/map_next_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:background="@drawable/button_style"
android:onClick="goToNextPage"
android:padding="10dip"
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>

MyPoiOverlay类

package com.home;
import android.app.Activity;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKSearch;
public class MyPoiOverlay extends PoiOverlay {
MKSearch mSearch;
public MyPoiOverlay(Activity activity, MapView mapView, MKSearch search) {
super(activity, mapView);
mSearch = search;
}
@Override
protected boolean onTap(int i) {
super.onTap(i);
MKPoiInfo info = getPoi(i);
if (info.hasCaterDetails) {
mSearch.poiDetailSearch(info.uid);
}
return true;
}
}

在配置文件中要比之前多配置一个activity,不然没法查看详细界面,这是百度SDK jar中提供的类:

<activity
android:name="com.baidu.mapapi.search.PlaceCaterActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>

Application类同之前。

相关文章

  • WebView设置WebViewClient的方法

    WebView设置WebViewClient的方法

    这篇文章主要介绍了 WebView设置WebViewClient的方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • Android Gradle 三方依赖管理详解

    Android Gradle 三方依赖管理详解

    这篇文章主要介绍了Android Gradle 三方依赖管理详解,Gradle的依赖管理是一个从开始接触Android开发就一直伴随着我们的问题,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • Android获得当前正在显示的activity类名的方法

    Android获得当前正在显示的activity类名的方法

    这篇文章主要介绍了Android获得当前正在显示的activity类名的方法,分析了权限的修改与Java代码的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • Android游戏之数独游戏开发

    Android游戏之数独游戏开发

    这篇文章主要为大家详细介绍了Android游戏之数独游戏开发的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 详谈Android编译命令

    详谈Android编译命令

    本文给大家详细介绍了Android中常用的编译命令、用法以及注意事项,非常的详细,有需要的小伙伴可以参考下
    2016-03-03
  • Kotlin 编程三分钟入门

    Kotlin 编程三分钟入门

    一个多月以来Kotlin从入门到现在,坚持用来开发的切身感受。因为语法与Java的区别挺大的一开始很想放弃,如果不是因为项目在使用,想必很少人会尝试这样一门小众语言,但是习惯后会发现这些年究竟浪费多少时间在写无用的Java代码了
    2017-05-05
  • android图片圆角、图片去色处理示例

    android图片圆角、图片去色处理示例

    这篇文章主要介绍了android图片圆角、图片去色处理示例,需要的朋友可以参考下
    2014-05-05
  • Flutter 仿微信支付界面

    Flutter 仿微信支付界面

    网传微信支付页面的第三方链接一个格子需要广告费1一个亿,微信支付页非常适合做功能导航,本篇使用 ListView和 GridView 模仿了微信支付的页面,同时介绍了如何装饰一个组件的背景和边缘样式。
    2021-05-05
  • Android如何创建自定义ActionBar

    Android如何创建自定义ActionBar

    这篇文章主要教大家如何创建自定义的ActionBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • Android代码实现AdapterViews和RecyclerView无限滚动

    Android代码实现AdapterViews和RecyclerView无限滚动

    这篇文章主要为大家详细介绍了Android代码实现AdapterViews和RecyclerView无限滚动的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07

最新评论