欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android百度地图poi范围搜索

程序员文章站 2024-02-17 09:20:46
我想大家可能都有过这样的经历:兜里揣着一张银行卡,在街上到处找自动取款机(atm)。在这个场景中,atm就是的兴趣点,我们想做的事情就是找到离自己较近的一些atm然后取款,...

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

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

先给大家展示下效果图:

Android百度地图poi范围搜索

详细界面:

Android百度地图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类同之前。