搜索服务
百度地图移动版API集成搜索服务包括:位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索,通过初始化MKSearch类,注册搜索结果的监听对象MKSearchListener,实现异步搜索服务。首先自定义MySearchListener实现MKSearchListener接口,通过不同的回调方法,获得搜索结果:
-
public
class MySearchListener
implements MKSearchListener
{
-
@Override
-
public
void onGetAddrResult
(MKAddrInfo result,
int iError
)
{
-
}
-
-
@Override
-
public
void onGetDrivingRouteResult
(MKDrivingRouteResult result,
int iError
)
{
-
}
-
-
@Override
-
public
void onGetPoiResult
(MKPoiResult result,
int type,
int iError
)
{
-
}
-
-
@Override
-
public
void onGetTransitRouteResult
(MKTransitRouteResult result,
int iError
)
{
-
}
-
-
@Override
-
public
void onGetWalkingRouteResult
(MKWalkingRouteResult result,
int iError
)
{
-
}
-
-
@Override
-
public
void onGetBusDetailResult
(MKBusLineResult result,
int iError
)
{
-
}
-
-
@Override
-
public
void onGetSuggestionResult
(MKSuggestionResult result,
int iError
)
{
-
}
-
}
然后初始化MKSearch类:
-
mMKSearch
=
new MKSearch
(
)
;
-
mMKSearch.
init
(mBMapMan,
new MySearchListener
(
)
)
;
//注意,MKSearchListener只支持一个,以最后一次设置为准
POI搜索及PoiOverlay
POI搜索有三种方式,根据范围和检索词发起范围检索poiSearchInbounds,城市poi检索poiSearchInCity,周边检索poiSearchNearBy,以下以周边检索为例介绍如何进行检索并显示覆盖物PoiOverlay:
检索天安门周边5000米之内的KFC餐厅:
-
mMKSearch.
poiSearchNearBy
(
"KFC",
new GeoPoint
(
(
int
)
(
39.915
* 1E6
),
(
int
)
(
116.404
* 1E6
)
),
5000
)
;
实现MySearchListener的onGetPoiResult,并展示检索结果:
-
@Override
-
public
void onGetPoiResult
(MKPoiResult result,
int type,
int iError
)
{
-
if
(result
==
null
)
{
-
return
;
-
}
-
PoiOverlay poioverlay
=
new PoiOverlay
(MyMapActivity.
this, mMapView
)
;
-
poioverlay.
setData
(result.
getAllPoi
(
)
)
;
-
mMapView.
getOverlays
(
).
add
(poioverlay
)
;
-
mMapView.
invalidate
(
)
;
//刷新地图
-
}
运行结果如下:
place详情页面使用方法
当执行完poi检索后,我们会得到一个poi的列表,每个poi节点都有个uid属性,我们可以根据这个uid获取关于这个poi的一些更详细的信息.比如:评论、图片、商户描述等。
在AndroidManifest.xml中设置如下信息:
<activity android:name="com.baidu.mapapi.PlaceCaterActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar"> </activity>
发起请求:
mSearch.poiDetailSearch(poi.uid);
详情页运行结果:
具体实现详见相关下载提供的Demo(PoiSearch.java)
驾车路线搜索及RouteOverlay
检索从天安门到百度大厦的驾车路线:
-
MKPlanNode start
=
new MKPlanNode
(
)
;
-
start.
pt
=
new GeoPoint
(
(
int
)
(
39.915
* 1E6
),
(
int
)
(
116.404
* 1E6
)
)
;
-
MKPlanNode end
=
new MKPlanNode
(
)
;
-
end.
pt
=
new GeoPoint
(
40057031,
116307852
)
;
-
// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
-
mMKSearch.
setDrivingPolicy
(MKSearch.
ECAR_TIME_FIRST
)
;
-
mMKSearch.
drivingSearch
(
null, start,
null, end
)
;
实现MySearchListener的onGetDrivingRouteResult,并展示检索结果:
-
@Override
-
public
void onGetDrivingRouteResult
(MKDrivingRouteResult result,
int iError
)
{
-
if
(result
==
null
)
{
-
return
;
-
}
-
RouteOverlay routeOverlay
=
new RouteOverlay
(MyMapActivity.
this, mMapView
)
;
-
// 此处仅展示一个方案作为示例
-
routeOverlay.
setData
(result.
getPlan
(
0
).
getRoute
(
0
)
)
;
-
mMapView.
getOverlays
(
).
add
(routeOverlay
)
;
-
}
运行结果如下:
步行路线搜索及RouteOverlay
方式与驾车路线搜索类似,只需将mMKSearch.drivingSearch(null, start, null, end)修改为mMKSearch.walkingSearch(null, start, null, end),实现的方法改为onGetWalkingRouteResult即可,不再赘述。
公交换乘路线搜索及TransitOverlay
检索从天安门到百度大厦的公交换乘路线:
-
MKPlanNode start
=
new MKPlanNode
(
)
;
-
start.
pt
=
new GeoPoint
(
(
int
)
(
39.915
* 1E6
),
(
int
)
(
116.404
* 1E6
)
)
;
-
MKPlanNode end
=
new MKPlanNode
(
)
;
-
end.
pt
=
new GeoPoint
(
40057031,
116307852
)
;
-
// 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁
-
mMKSearch.
setTransitPolicy
(MKSearch.
EBUS_TRANSFER_FIRST
)
;
-
mMKSearch.
transitSearch
(
"北京", start, end
)
;
// 必须设置城市名
实现MySearchListener的onGetTransitRouteResult(MKTransitRouteResult, int),并展示检索结果:
-
@Override
-
public
void onGetTransitRouteResult
(MKTransitRouteResult result,
int iError
)
{
-
if
(result
==
null
)
{
-
return
;
-
}
-
TransitOverlay transitOverlay
=
new TransitOverlay
(MyMapActivity.
this, mMapView
)
;
-
// 此处仅展示一个方案作为示例
-
transitOverlay.
setData
(result.
getPlan
(
0
)
)
;
-
mMapView.
getOverlays
(
).
add
(transitOverlay
)
;
-
mMapView.
invalidate
(
)
;
//刷新地图
-
}
公交路线详情搜索
检索北京市公交路线717的poi,获取公交路线的uid:
-
mSearch.
poiSearchInCity
(
"北京",
"717"
)
;
实现MySearchListener的onGetPoiResult (MKPoiResult res, int type, int error),获得公交线路poi的uid,并根据此uid发起公交线路详情的检索:
-
@Override
-
public
void onGetPoiResult
(MKPoiResult res,
int type,
int error
)
{
-
// 错误号可参考MKEvent中的定义
-
if
(error
!=
0
|| res
==
null
)
{
-
Toast.
makeText
(BusLineSearch.
this,
"抱歉,未找到结果", Toast.
LENGTH_LONG
).
show
(
)
;
-
return
;
-
}
-
-
// 找到公交路线poi node
-
MKPoiInfo curPoi
=
null
;
-
int totalPoiNum
= res.
getNumPois
(
)
;
-
for
(
int idx
=
0
; idx
< totalPoiNum
; idx
++
)
{
-
curPoi
= res.
getPoi
(idx
)
;
-
if
(
2
== curPoi.
ePoiType
)
{
-
break
;
-
}
-
}
-
-
mSearch.
busLineSearch
(mCityName, curPoi.
uid
)
;
-
}
实现MySearchListener的onGetBusDetailResult(MKBusLineResult result, int iError),并展示搜索结果:
-
public
void onGetBusDetailResult
(MKBusLineResult result,
int iError
)
{
-
if
(iError
!=
0
|| result
==
null
)
{
-
Toast.
makeText
(BusLineSearch.
this,
"抱歉,未找到结果", Toast.
LENGTH_LONG
).
show
(
)
;
-
return
;
-
}
-
-
RouteOverlay routeOverlay
=
new RouteOverlay
(BusLineSearch.
this, mMapView
)
;
-
// 此处仅展示一个方案作为示例
-
routeOverlay.
setData
(result.
getBusRoute
(
)
)
;
-
mMapView.
getOverlays
(
).
clear
(
)
;
-
mMapView.
getOverlays
(
).
add
(routeOverlay
)
;
-
mMapView.
invalidate
(
)
;
-
-
mMapView.
getController
(
).
animateTo
(result.
getBusRoute
(
).
getStart
(
)
)
;
-
}
运行结果如下:
地址信息查询
根据地理坐标查询地址信息:
-
mMKSearch.
reverseGeocode
(
new GeoPoint
(
40057031,
116307852
)
)
;
//逆地址解析
-
mMKSearch.
geocode
(key, city
)
;
//地址解析
geocode返回结果在MKSearchListener里的onGetAddrResult方法。
reverseGeocode返回结果在MKSearchListener里的onGetPoiResult方法。
-
public
void onGetAddrResult
(MKAddrInfo res,
int error
)
{
-
if
(error
!=
0
)
{
-
-
Toast.
makeText
(GeoCoder.
this, str, Toast.
LENGTH_LONG
).
show
(
)
;
-
return
;
-
}
-
mMapView.
getController
(
).
animateTo
(res.
geoPt
)
;
-
-
Toast.
makeText
(GeoCoder.
this, strInfo, Toast.
LENGTH_LONG
).
show
(
)
;
-
Drawable marker
= getResources
(
).
getDrawable
(R.
drawable.
iconmarka
)
;
//得到需要标在地图上的资源
-
marker.
setBounds
(
0,
0, marker.
getIntrinsicWidth
(
), marker.
getIntrinsicHeight
(
)
)
;
//为maker定义位置和边界
-
mMapView.
getOverlays
(
).
clear
(
)
;
-
mMapView.
getOverlays
(
).
add
(
new OverItemT
(marker, GeoCoder.
this, res.
geoPt, res.
strAddr
)
)
;
-
}
-
public
void onGetPoiResult
(MKPoiResult res,
int type,
int error
)
{
-
if
(error
!=
0
|| res
==
null
)
{
-
Toast.
makeText
(GeoCoder.
this,
"解析失败", Toast.
LENGTH_LONG
).
show
(
)
;
-
return
;
-
}
-
if
(res
!=
null
&& res.
getCurrentNumPois
(
)
>
0
)
{
-
GeoPoint ptGeo
= res.
getAllPoi
(
).
get
(
0
).
pt
;
-
// 移动地图到该点:
-
mMapView.
getController
(
).
animateTo
(ptGeo
)
;
-
-
strInfo
+=
"\r\n附近有:"
;
-
for
(
int i
=
0
; i
< res.
getAllPoi
(
).
size
(
)
; i
++
)
{
-
strInfo
+=
(res.
getAllPoi
(
).
get
(i
).
name
+
";"
)
;
-
}
-
Toast.
makeText
(GeoCoder.
this, strInfo, Toast.
LENGTH_LONG
).
show
(
)
;
-
}
-
}
在线建议查询
根据关键词查询在线建议词:
-
mMKSearch.
suggestionSearch
(
"天安门"
)
;
//输入关键词
实现MySearchListener的onGetSuggestionResult,得到查询结果:
-
-
@Override
-
public
void onGetSuggestionResult
(MKSuggestionResult res,
int iError
)
-
{
-
if
(iError
!=
0
|| res
==
null
)
{
-
Toast.
makeText
(PoiSearch.
this,
"抱歉,未找到结果", Toast.
LENGTH_LONG
).
show
(
)
;
-
return
;
-
}
-
int nSize
= res.
getSuggestionNum
(
)
;
-
-
for
(
int i
=
0
; i
< nSize
; i
++
)
-
{
-
mStrSuggestions
[i
]
= res.
getSuggestion
(i
).
city
+ res.
getSuggestion
(i
).
key
;
-
}
-
ArrayAdapter
<String
> suggestionString
=
new ArrayAdapter
<String
>
(PoiSearch.
this, android.
R.
layout.
simple_list_item_1,mStrSuggestions
)
;
-
mSuggestionList.
setAdapter
(suggestionString
)
;
-
-
}
云检索模块的使用
首先实例化GeoSearchListener接口:
-
@Override
-
public
void onGetGeoDetailsResult
(DetailResult result,
int type,
int iError
)
-
{
-
if
(result
!=
null
)
{
-
if
(result.content
!=
null
)
{
-
Toast.
makeText
(CloudSearchDemo.
this, result.content.name, Toast.
LENGTH_LONG
).
show
(
)
;
-
}
-
else
{
-
Toast.
makeText
(CloudSearchDemo.
this, "status:" + result.status, Toast.
LENGTH_SHORT
).
show
(
)
;
-
}
-
}
-
}
-
@Override
-
public
void onGetGeoResult
(GeoSearchResult result,
int type,
int iError
)
-
{
-
if
(result
!=
null
&& result.poiList
!=
null
&& result.poiList.size()
>
0
)
{
-
CloudOverlay poiOverlay =
new CloudOverlay
(
this
)
;
-
poiOverlay.setData
(result.poiList
)
;
-
mMapView.getOverlays
().clear
()
;
-
mMapView.getOverlays
().add
(poiOverlay
)
;
-
mMapView.invalidate
()
;
-
mMapView.getController
().animateTo
(new GeoPoint((int)(result.poiList.get(0).latitude * 1e6), (int)(result.poiList.get(0).longitude * 1e6))
)
;
-
}
-
}
发起云检索请求:
-
BoundsSearchInfo r =
new BoundsSearchInfo
()
;
-
r.queryWords = "五中"
;
-
r.ak = "输入你的ak"
;
-
r.bounds =
new Bounds
(39843895, 116402214, 40956948, 116431457)
;
-
r.filter.put
("databox", 848)
;
-
r.scope = 2
;
-
GeoSearchManager.getInstance
().searchBounds
(r)
;
-
// 当请求成功时,会回调onGetGeoResult函数,执行打点操作。
-
CloudOverlay poiOverlay =
new CloudOverlay
(this)
;
-
poiOverlay.setData
(result.poiList)
;
-
mMapView.getOverlays
().clear
()
;
-
mMapView.getOverlays
().add
(poiOverlay)
;
-
mMapView.invalidate
()
;
运行结果如下: