1.新建项目
将地图API添加进classpath中:
2.在activity_main.xml中添加一个MapView,用来显示地图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 添加显示地图的控件 -->
<com.baidu.mapapi.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
3.修改AndroidManifest.xml文件,指定用户权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />
3.初始化地图引擎
bMapManager = new BMapManager(MainActivity.this);
bMapManager.init(keyString, new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
if (arg0 == 300) {
Toast.makeText(MainActivity.this, "输入的key有误,请核实!", 1)
.show();
}
}
});
initMapActivity(bMapManager); //初始化地图引擎
4.取得MapView组件
mapView = (MapView) findViewById(R.id.bmapView);
5.实例化search对象并添加监听
mkSearch = new MKSearch();
mkSearch.init(bMapManager, new MySearchListener());
监听类:
private class MySearchListener implements MKSearchListener {
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult result, int type, int isError) {
// TODO Auto-generated method stub
if(result == null){
return;
}
PoiOverlay poiOverlay = new PoiOverlay(MainActivity.this, mapView);
poiOverlay.setData(result.getAllPoi());
mapView.getOverlays().add(poiOverlay);
mapView.invalidate();
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
}
6.根据关键词、中心点、半径进行搜索。
// 关键词,中心点,范围半径
mkSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
7.恢复、暂停、停止地图时地图引擎的操作
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (bMapManager != null) {
bMapManager.destroy();
bMapManager = null;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (bMapManager != null) {
bMapManager.start();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (bMapManager != null) {
bMapManager.stop();
}
}
完整代码:
package com.example.baiduhellopoioverlay;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.PoiOverlay;
public class MainActivity extends MapActivity {
private MapView mapView;
private BMapManager bMapManager;
private String keyString = "43D220AAB0DEC976D2EABC83CFC9EEE52BCCCA7A";
private MKSearch mkSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bMapManager = new BMapManager(MainActivity.this);
bMapManager.init(keyString, new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
if (arg0 == 300) {
Toast.makeText(MainActivity.this, "输入的key有误,请核实!", 1)
.show();
}
}
});
initMapActivity(bMapManager); //初始化地图引擎
mapView = (MapView) findViewById(R.id.bmapView);
mkSearch = new MKSearch();
mkSearch.init(bMapManager, new MySearchListener());
// 关键词,中心点,范围半径
mkSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6)), 5000);
}
private class MySearchListener implements MKSearchListener {
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult result, int type, int isError) {
// TODO Auto-generated method stub
if(result == null){
return;
}
PoiOverlay poiOverlay = new PoiOverlay(MainActivity.this, mapView);
poiOverlay.setData(result.getAllPoi());
mapView.getOverlays().add(poiOverlay);
mapView.invalidate();
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (bMapManager != null) {
bMapManager.destroy();
bMapManager = null;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (bMapManager != null) {
bMapManager.start();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (bMapManager != null) {
bMapManager.stop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
运行结果: