Android百度地图添加覆盖物(AndroidSDK_v2.4.1)
使用网络功能所需权限:
<!--使用网络功能所需权限 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission> <uses-permission android:name="android.permission.INTERNET"> </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"> </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"> </uses-permission> <!-- SDK离线地图和cache功能需要读写外部存储器 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission> <uses-permission android:name="android.permission.WRITE_SETTINGS"> </uses-permission> <!-- 获取设置信息和详情页直接拨打电话需要以下权限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission> <uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission> <!-- 使用定位功能所需权限,demo已集成百度定位SDK,不使用定位功能可去掉以下6项 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> </uses-permission> <permission android:name="android.permission.BAIDU_LOCATION_SERVICE"> </permission> <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"> </uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"> </uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"> </uses-permission> <uses-permission android:name="android.permission.ACCESS_GPS"/>
添加对应的开发密钥:
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="开发密钥"> </meta-data>
DemoApplication.java:
package org.lee.android; import android.app.Application; import android.content.Context; import android.widget.Toast; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.MKGeneralListener; import com.baidu.mapapi.map.MKEvent; public class DemoApplication extends Application { private static DemoApplication mInstance = null; public boolean m_bKeyRight = true; BMapManager mBMapManager = null; @Override public void onCreate() { super.onCreate(); mInstance = this; initEngineManager(this); } public void initEngineManager(Context context) { if (mBMapManager == null) { mBMapManager = new BMapManager(context); } if (!mBMapManager.init(new MyGeneralListener())) { Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "BMapManager 初始化错误!", Toast.LENGTH_LONG).show(); } } public static DemoApplication getInstance() { return mInstance; } // 常用事件监听,用来处理通常的网络错误,授权验证错误等 static class MyGeneralListener implements MKGeneralListener { @Override public void onGetNetworkState(int iError) { if (iError == MKEvent.ERROR_NETWORK_CONNECT) { Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } else if (iError == MKEvent.ERROR_NETWORK_DATA) { Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "输入正确的检索条件!", Toast.LENGTH_LONG).show(); } // ... } @Override public void onGetPermissionState(int iError) { //非零值表示key验证未通过 if (iError != 0) { //授权Key错误: Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "请在 DemoApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常!error: "+iError, Toast.LENGTH_LONG).show(); DemoApplication.getInstance().m_bKeyRight = false; } else{ DemoApplication.getInstance().m_bKeyRight = true; Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "key认证成功", Toast.LENGTH_LONG).show(); } } } }
BaiduMapActivity.java:
package org.lee.android; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.map.ItemizedOverlay; import com.baidu.mapapi.map.MapController; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.OverlayItem; import com.baidu.mapapi.map.PopupClickListener; import com.baidu.mapapi.map.PopupOverlay; import com.baidu.platform.comapi.basestruct.GeoPoint; public class BaiduMapActivity extends Activity { BMapManager mBMapMan = null; MapView mMapView = null; private MyOverlay mOverlay = null; private PopupOverlay pop = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.onCreate(savedInstanceState); // 初始化地图View initBdMapViews(); //准备要添加的Overlay addMyOverlay(); } // 初始化地图View private void initBdMapViews(){ mBMapMan = new BMapManager(getApplication()); mBMapMan.init(null); // 注意:请在试用setContentView前初始化BMapManager对象,否则会报错 setContentView(R.layout.baidumap); mMapView = (MapView) findViewById(R.id.bmapsView); mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件 MapController mMapController = mMapView.getController(); // 调整地图中心点 mMapController.setCenter(transformToGeoP(39.915, 116.404)); mMapController.setZoom(12);// 设置地图zoom级别 } //准备要添加的Overlay public void addMyOverlay(){ double mLat1 = 39.90923; double mLon1 = 116.397428; double mLat2 = 39.9022; double mLon2 = 116.3922; double mLat3 = 39.917723; double mLon3 = 116.3722; // 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6) GeoPoint p1 = transformToGeoP(mLat1, mLon1); GeoPoint p2 = transformToGeoP(mLat2, mLon2); GeoPoint p3 = transformToGeoP(mLat3, mLon3); //准备overlay图像数据,根据实情情况修复 Drawable mark= getResources().getDrawable(R.drawable.icon_gcoding); //用OverlayItem准备Overlay数据 OverlayItem item1 = new OverlayItem(p1,"item1","item1"); //使用setMarker()方法设置overlay图片,如果不设置则使用构建ItemizedOverlay时的默认设置 OverlayItem item2 = new OverlayItem(p2,"item2","item2"); item2.setMarker(mark); OverlayItem item3 = new OverlayItem(p3,"item3","item3"); //创建IteminizedOverlay mOverlay = new MyOverlay(mark, mMapView); //将IteminizedOverlay添加到MapView中 mMapView.getOverlays().clear(); mMapView.getOverlays().add(mOverlay); //现在所有准备工作已准备好,使用以下方法管理overlay. //添加overlay, 当批量添加Overlay时使用addItem(List<OverlayItem>)效率更高 mOverlay.addItem(item1); mOverlay.addItem(item2); mOverlay.addItem(item3); mMapView.refresh(); } // 弹出覆盖物 public void showPopupOverlay(GeoPoint p1) { //创建pop对象,注册点击事件监听接口 pop = new PopupOverlay(mMapView,new PopupClickListener() { @Override public void onClickedPopup(int index) { //在此处理pop点击事件,index为点击区域索引,点击区域最多可有三个 } }); 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,隐藏pop pop.showPopup(bitMaps, p1, 32); } // 将经纬度转换成GeoPoint private GeoPoint transformToGeoP(double lat, double lng) { return new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); } public class MyOverlay extends ItemizedOverlay<OverlayItem> { //用MapView构造ItemizedOverlay public MyOverlay(Drawable mark,MapView mapView){ super(mark,mapView); } protected boolean onTap(int index) { //在此处理item点击事件 System.out.println("item onTap: "+index); //弹出覆盖物 showPopupOverlay(getItem(index).getPoint()); return true; } public boolean onTap(GeoPoint pt, MapView mapView){ if (pop != null) { //隐藏覆盖物 pop.hidePop(); } return false; } } @Override protected void onDestroy() { mMapView.destroy(); if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { mMapView.onPause(); if (mBMapMan != null) { mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { mMapView.onResume(); if (mBMapMan != null) { mBMapMan.start(); } super.onResume(); } }
baidumap.xml:
<?xml version="1.0" encoding="utf-8"?> <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.map.MapView android:id="@+id/bmapsView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
pop_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="120dip" android:background="@drawable/pop_layout_bg" > <LinearLayout android:id="@+id/user_logo_layout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_margin="2dip" android:gravity="center" > <ImageView android:id="@+id/user_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toRightOf="@+id/user_logo_layout" android:gravity="center" android:orientation="vertical" > <LinearLayout android:id="@+id/time_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:text="时间:" android:textColor="#ffffff" android:textSize="15dip" /> <TextView android:id="@+id/time" android:layout_width="175dip" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:text="2014年4月25日" android:textColor="#ffffff" android:textSize="15dip" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/time_layout" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:text="地点:" android:textColor="#ffffff" android:textSize="15dip" /> <TextView android:id="@+id/address" android:layout_width="170dip" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:text="安徽省合肥市包河区富广大厦" android:lines="2" android:ellipsize="end" android:textColor="#ffffff" android:textSize="15dip" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28