使用百度地图有很长一段时间了,每次用的时候都是看官方例子,很少自己作总结,这样每次用的时候都会花时间去理解,今天就抽点时间总结一下:
1.废话不多说,先看效果:
2.基于百度地图版本:Android SDK v2.1.2
下方有官方Demo下载
3.项目中加入所需权限,demo中权限很多,我从中筛选出我们这个覆盖物所需要的权限,如果不放心的话,也可以直接copy demo中的所有权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4.继承Application,在子类中初始化一些百度地图引擎设置
public class MyApplication extends Application
{
private static MyApplication baseApplication = null;
public boolean m_bKeyRight = true;
public BMapManager mBMapManager = null;
//百度API_KEY
public static String BMAP_API_KEY = "957503D42459F015B0BBF70C041B3D6D488A323F";
@Override
public void onCreate()
{
super.onCreate();
baseApplication = this;
initEngineManager(this);
}
public static MyApplication getInstance()
{
return baseApplication;
}
// 初始化百度地图引擎
public void initEngineManager(Context context)
{
if (mBMapManager == null)
{
mBMapManager = new BMapManager(context);
}
if (!mBMapManager.init(BMAP_API_KEY, new MyGeneralListener()))
{
Toast.makeText(MyApplication.getInstance().getApplicationContext(),
"BMapManager 初始化错误!", Toast.LENGTH_LONG).show();
}
}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
public static class MyGeneralListener implements MKGeneralListener
{
@Override
public void onGetNetworkState(int iError)
{
if (iError == MKEvent.ERROR_NETWORK_CONNECT)
{
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"您的网络出错啦!", Toast.LENGTH_LONG).show();
} else if (iError == MKEvent.ERROR_NETWORK_DATA)
{
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"输入正确的检索条件!", Toast.LENGTH_LONG).show();
}
// ...
}
@Override
public void onGetPermissionState(int iError)
{
if (iError == MKEvent.ERROR_PERMISSION_DENIED)
{
// 授权Key错误:
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"请在 DemoApplication.java文件输入正确的授权Key!",
Toast.LENGTH_LONG).show();
MyApplication.getInstance().m_bKeyRight = false;
}
}
}
}
不要忘记在manifiest里面引用MyApplication
<application
android:name="com.example.baidumaptest.MyApplication"
5.接下来来看看Activity代码:
public class MainActivity extends Activity
{
private MapView mapView = null;
private MapController mMapController = null;
private MyOverlay mOverlay = null;
private PopupOverlay pop = null;
// 经纬度,用来确定覆盖物的位置.这里就写死一个位置
private double lat = 23.12916;
private double lng = 113.379379;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initClass();
initBdMapViews();
}
// 初始化类
private void initClass()
{
MyApplication app = (MyApplication) this.getApplication();
if (app.mBMapManager == null)
{
app.mBMapManager = new BMapManager(this);
app.mBMapManager.init(MyApplication.BMAP_API_KEY,
new MyApplication.MyGeneralListener());
}
}
// 初始化地图View
private void initBdMapViews()
{
mapView = (MapView) findViewById(R.id.bmapView);
// 设置相关参数
mMapController = mapView.getController();
mMapController.enableClick(true);
mMapController.setZoom(17);
mapView.setBuiltInZoomControls(true);
// 初始化地图覆盖物
initOverlay();
// 调整地图中心点
mMapController.setCenter(transformToGeoP(lat, lng));
}
//初始化覆盖物
private void initOverlay()
{
mOverlay = new MyOverlay(getResources().getDrawable(
R.drawable.icon_gcoding), mapView);
//一个覆盖物
OverlayItem item1 = new OverlayItem(transformToGeoP(lat, lng), "覆盖物", "");
item1.setMarker(getResources().getDrawable(R.drawable.icon_gcoding));
mOverlay.addItem(item1);
mapView.getOverlays().add(mOverlay);
mapView.refresh();
//默认弹出
showPopupOverlay(transformToGeoP(lat, lng));
}
// 自定义覆盖物
public class MyOverlay extends ItemizedOverlay
{
public MyOverlay(Drawable arg0, MapView arg1)
{
super(arg0, arg1);
}
@Override
protected boolean onTap(int arg0)
{
//弹出覆盖物
showPopupOverlay(getItem(arg0).getPoint());
return true;
}
@Override
public boolean onTap(GeoPoint arg0, MapView arg1)
{
if (pop != null)
{
//隐藏覆盖物
pop.hidePop();
}
return false;
}
}
// 弹出覆盖物
public void showPopupOverlay(GeoPoint p1)
{
pop = new PopupOverlay(mapView, null);
View view = getLayoutInflater().inflate(R.layout.pop_layout, null);
View pop_layout = view.findViewById(R.id.pop_layout);
//将View转化成用于显示的bitmap
Bitmap[] bitMaps = { BMapUtil.getBitmapFromView(pop_layout) };
pop.showPopup(bitMaps, p1, 32);
}
// 将经纬度转换成GeoPoint
private GeoPoint transformToGeoP(double lat, double lng)
{
return new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
}
//生命周期,必不可少,不全则影响百度性能,严重会报错
@Override
protected void onPause()
{
mapView.onPause();
super.onPause();
}
@Override
protected void onResume()
{
mapView.onResume();
super.onResume();
}
@Override
protected void onDestroy()
{
mapView.destroy();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mapView.onRestoreInstanceState(savedInstanceState);
}
}
相比demo中代码,这代码相对简洁,把重要部分以最简单的方式展示出来,便于大家理解,有什么疑问或者代码有错误,欢迎指正
以上项目源码下载地址,欢迎大家下载指正:
http://download.csdn.net/detail/qq15989177612/6462621