BMapApiDemoApp.java
package com.example.textdemo4;import android.app.Application;import android.util.Log;import android.widget.TextView;import android.widget.Toast;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.MKEvent;import com.baidu.mapapi.MKGeneralListener;public class BMapApiDemoApp extends Application { public LocationClient mLocationClient = null; public String mData; public String address; public MyLocationListenner myListener = new MyLocationListenner(); public TextView mTv; public TextView mAddress; /*private static LocationApplication instance; public static LocationApplication getInstance() { return instance; }*/ static BMapApiDemoApp mDemoApp; private static BMapApiDemoApp instance; public static BMapApiDemoApp getInstance() { return instance; } BMapManager mBMapMan = null; //百度MapAPI的管理类 // 授权Key // 申请地址:http://developer.baidu.com/map/android-mobile-apply-key.htm String mStrKey = "F7A0CB2F6BA513A57031EEBEBE99BEC0B9A36434"; boolean m_bKeyRight = true; // 授权Key正确,验证通过 // 常用事件监听,用来处理通常的网络错误,授权验证错误等 static class MyGeneralListener implements MKGeneralListener { @Override public void onGetNetworkState(int iError) { Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError); Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } @Override public void onGetPermissionState(int iError) { Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError); if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授权Key错误: Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "请在BMapApiDemoApp.java文件输入正确的授权Key!", Toast.LENGTH_LONG).show(); BMapApiDemoApp.mDemoApp.m_bKeyRight = false; } } } @Override public void onCreate() { Log.v("BMapApiDemoApp", "onCreate"); mDemoApp = this; mBMapMan = new BMapManager(this); boolean isSuccess = mBMapMan.init(this.mStrKey, new MyGeneralListener()); // 初始化地图sdk成功,设置定位监听时间 if (isSuccess) { mBMapMan.getLocationManager().setNotifyInternal(10, 5); } else { // 地图sdk初始化失败,不能使用sdk } //---------------------- instance = this; mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(myListener); setLocationOption(); //---------------------- super.onCreate(); } @Override //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗 public void onTerminate() { if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onTerminate(); } // 设置相关参数 public void setLocationOption() { LocationClientOption option = new LocationClientOption(); option.setProdName("Compass"); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); option.setAddrType("all"); option.setScanSpan(5 * 60 * 1000); option.setPriority(LocationClientOption.NetWorkFirst); mLocationClient.setLocOption(option); } /** *监听函数,又新位置的时候,格式化成字符串,输出到屏幕中 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // TODO Auto-generated method stub if (location == null) return; StringBuffer sb = new StringBuffer(256); // sb.append("时间: "); // sb.append(location.getTime()); sb.append("纬度 : "); sb.append(location.getLatitude() + "°"); sb.append(", 经度 : "); sb.append(location.getLongitude() + "°");// sb.append(", 精度 : ");// sb.append(location.getRadius() + " 米"); mData = sb.toString(); if(mTv != null) mTv.setText(sb); if (location.getLocType() == BDLocation.TypeGpsLocation) { address = "速度 : " + location.getSpeed(); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { address = "地址 : " + location.getAddrStr(); } if(mAddress !=null) mAddress.setText(address); } @Override public void onReceivePoi(BDLocation poiLocation) { } } }
MainActivity.java
package com.example.textdemo4;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.LocationListener;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapView;import com.baidu.mapapi.MyLocationOverlay;import android.location.Location;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends MapActivity { BMapApiDemoApp application; TextView mLatitudeTV;// 纬度 TextView mLongitudeTV;// 经度 MapView mMapView = null; LocationListener mLocationListener = null; MyLocationOverlay mLocationOverlay = null; //定位图层 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); application = (BMapApiDemoApp) getApplication(); setContentView(R.layout.activity_main); mLongitudeTV = (TextView) findViewById(R.id.textview_location_longitude_degree); mLatitudeTV = (TextView) findViewById(R.id.textview_location_latitude_degree); application.mTv = mLatitudeTV; application.mAddress = mLongitudeTV; application.mLocationClient.start(); //======================== BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication(); if (app.mBMapMan == null) { app.mBMapMan = new BMapManager(getApplication()); app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener()); } app.mBMapMan.start(); // 如果使用地图SDK,请初始化地图Activity this.initMapActivity(app.mBMapMan); mMapView = (MapView)findViewById(R.id.bmapView); mMapView.setBuiltInZoomControls(true); //设置在缩放动画过程中也显示overlay,默认为不绘制 mMapView.setDrawOverlayWhenZooming(true); // 添加定位图层 mLocationOverlay = new MyLocationOverlay(this, mMapView); mMapView.getOverlays().add(mLocationOverlay); // 注册定位事件 mLocationListener = new LocationListener(){ @Override public void onLocationChanged(Location location) { if (location != null){ GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6), (int)(location.getLongitude()*1e6)); mMapView.getController().animateTo(pt); } } }; } @Override protected void onPause() { BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication(); app.mBMapMan.getLocationManager().removeUpdates(mLocationListener); mLocationOverlay.disableMyLocation(); mLocationOverlay.disableCompass(); // 关闭指南针 app.mBMapMan.stop(); super.onPause(); } @Override protected void onResume() { BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication(); // 注册定位事件,定位后将地图移动到定位点 app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener); mLocationOverlay.enableMyLocation(); mLocationOverlay.enableCompass(); // 打开指南针 app.mBMapMan.start(); super.onResume(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
AndroidManifest.xml
完整DEMO下载地址: