百度地图应用开发(二)

今天看了下百度地图的官方示例和api文档,玩玩搜索。

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>  2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  3  package="com.BaiduSearch"  4  android:versionCode="1"  5  android:versionName="1.0">  6  <uses-sdk android:minSdkVersion="7" />  7   8  <application android:icon="@drawable/icon" android:label="@string/app_name">  9  <activity android:name=".MainActivity" 10  android:label="@string/app_name" android:screenOrientation="sensor" 11  android:configChanges="orientation|keyboardHidden"> 12  <intent-filter> 13  <action android:name="android.intent.action.MAIN" /> 14  <category android:name="android.intent.category.LAUNCHER" /> 15  </intent-filter> 16  </activity> 17  18  </application> 19  20  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 21 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 22 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 23 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 24 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 25 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 26 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 27  28 <supports-screens android:largeScreens="true" 29  android:normalScreens="true" android:smallScreens="true" 30  android:resizeable="true" android:anyDensity="true"/> 31 <uses-sdk android:minSdkVersion="3"></uses-sdk> 32 </manifest>
复制代码


复制代码
 1 <?xml version="1.0" encoding="utf-8"?>  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  3  android:orientation="vertical"  4  android:layout_width="fill_parent"  5  android:layout_height="fill_parent"  6  >  7  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  8  android:orientation="horizontal"  9  android:layout_width="fill_parent" 10  android:layout_height="wrap_content"> 11  12  <EditText 13  android:id="@+id/city" 14  android:layout_width="wrap_content" 15  android:layout_height="wrap_content" 16  android:hint="输入城市" 17  /> 18  <TextView android:text="市内找" 19  android:layout_width="wrap_content" 20  android:layout_height="wrap_content"></TextView> 21  <EditText 22  android:id="@+id/searchkey" 23  android:layout_width="wrap_content" 24  android:layout_height="wrap_content" 25  android:hint="输入地点" 26  /> 27  <Button 28  android:id="@+id/search" 29  android:text="搜索" 30  android:layout_width="fill_parent" 31  android:layout_height="wrap_content" 32  /> 33 </LinearLayout> 34  <com.baidu.mapapi.MapView android:id="@+id/bmapView" 35  android:layout_width="fill_parent" android:layout_height="fill_parent" 36  android:clickable="true" 37  /> 38 </LinearLayout>
复制代码


复制代码
 1 package com.BaiduSearch;  2   3 import com.baidu.mapapi.BMapManager;  4 import com.baidu.mapapi.MKAddrInfo;  5 import com.baidu.mapapi.MKDrivingRouteResult;  6 import com.baidu.mapapi.MKPoiResult;  7 import com.baidu.mapapi.MKSearch;  8 import com.baidu.mapapi.MKSearchListener;  9 import com.baidu.mapapi.MKTransitRouteResult;  10 import com.baidu.mapapi.MKWalkingRouteResult;  11 import com.baidu.mapapi.MapActivity;  12 import com.baidu.mapapi.MapView;  13 import com.baidu.mapapi.PoiOverlay;  14 import android.os.Bundle;  15 import android.view.View;  16 import android.view.View.OnClickListener;  17 import android.widget.Button;  18 import android.widget.EditText;  19 import android.widget.Toast;  20   21 public class MainActivity extends MapActivity {  22  // 地图引擎管理类  23  BMapManager mBMapMan;  24  // 搜索按钮  25  Button mBtnSearch = null;  26  // 地图view  27  MapView mMapView = null;  28  // 搜索模块,也可去掉地图模块独立使用  29  MKSearch mSearch = null;  30   31  @Override  32  public void onCreate(Bundle savedInstanceState) {  33  super.onCreate(savedInstanceState);  34  setContentView(R.layout.main);  35   36  // 初始化,填写自己申请的Key  37  mBMapMan = new BMapManager(getApplication());  38  mBMapMan.init("AC8235886C920052E91B6E6463E8886EA6E6B51E", null);  39  super.initMapActivity(mBMapMan);  40   41  // 如果使用地图SDK,请初始化地图Activity  42   43  mMapView = (MapView) findViewById(R.id.bmapView);  44  // 设置启用内置的缩放控件  45  mMapView.setBuiltInZoomControls(true);  46  // 设置在缩放动画过程中也显示overlay,默认为不绘制  47  mMapView.setDrawOverlayWhenZooming(true);  48   49  // 初始化搜索模块,注册事件监听  50  mSearch = new MKSearch();  51  mSearch.init(mBMapMan, new MKSearchListener() {  52   53  public void onGetPoiResult(MKPoiResult res, int type, int error) {  54  // 错误号可参考MKEvent中的定义  55  if (error != 0 || res == null) {  56  Toast.makeText(MainActivity.this, "抱歉,未找到结果",  57  Toast.LENGTH_LONG).show();  58  return;  59  }  60  // 将地图移动到第一个POI中心点  61  if (res.getCurrentNumPois() > 0) {  62  // 将poi结果显示到地图上  63  PoiOverlay poiOverlay = new PoiOverlay(MainActivity.this,  64  mMapView);  65  poiOverlay.setData(res.getAllPoi());  66  mMapView.getOverlays().clear();  67  mMapView.getOverlays().add(poiOverlay);  68  mMapView.invalidate();  69  mMapView.getController().animateTo(res.getPoi(0).pt);  70  } else if (res.getCityListNum() > 0) {  71  String strInfo = "";  72  for (int i = 0; i < res.getCityListNum(); i++) {  73  strInfo += res.getCityListInfo(i).city;  74  strInfo += ",";  75  }  76  strInfo += "找到结果";  77  Toast.makeText(MainActivity.this, strInfo,  78  Toast.LENGTH_LONG).show();  79  }  80  }  81   82  public void onGetDrivingRouteResult(MKDrivingRouteResult res,  83  int error) {  84  }  85   86  public void onGetTransitRouteResult(MKTransitRouteResult res,  87  int error) {  88  }  89   90  public void onGetWalkingRouteResult(MKWalkingRouteResult res,  91  int error) {  92  }  93   94  public void onGetAddrResult(MKAddrInfo res, int error) {  95  }  96   97  });  98   99  // 设定搜索按钮的响应 100  mBtnSearch = (Button) findViewById(R.id.search); 101  102  OnClickListener clickListener = new OnClickListener() { 103  public void onClick(View v) { 104  SearchButtonProcess(v); 105  } 106  }; 107  108  mBtnSearch.setOnClickListener(clickListener); 109  } 110  111  void SearchButtonProcess(View v) { 112  if (mBtnSearch.equals(v)) { 113  EditText editCity = (EditText) findViewById(R.id.city); 114  EditText editSearchKey = (EditText) findViewById(R.id.searchkey); 115  mSearch.poiSearchInCity(editCity.getText().toString(), 116  editSearchKey.getText().toString()); 117  } 118  } 119  120  // Override以下方法,管理API 121  @Override 122  protected void onDestroy() { 123  if (mBMapMan != null) { 124  mBMapMan.destroy(); 125  mBMapMan = null; 126  } 127  super.onDestroy(); 128  } 129  130  @Override 131  protected void onPause() { 132  if (mBMapMan != null) { 133  mBMapMan.stop(); 134  } 135  super.onPause(); 136  } 137  138  @Override 139  protected void onResume() { 140  if (mBMapMan != null) { 141  mBMapMan.start(); 142  } 143  super.onResume(); 144  } 145  146  @Override 147  protected boolean isRouteDisplayed() { 148  // TODO Auto-generated method stub 149  return false; 150  } 151 }
复制代码


运行结果如下:

百度地图应用开发(二)

你可能感兴趣的:(百度地图应用开发(二))