1、地图控件概述
1、MapView 地图主控件
2、MapController 一个工具类 用于控制地图的缩放(ZoomIn、ZoomOut) 和平移(pan)
3、MKMapViewListener 用于处理地图事件回调
4、MyPopupOverlay
图层类
5、LocationClient
定位服务类
6、MyLocationListenner 监听SDK定位函数
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class ImPoi extends Activity {
private LocationClient mLocClient = null;
private LocationData locData = null;
private locationOverlay myLocationOverlay = null;
private MyLocationListenner myListener = null;
private MapView mMapView = null;
private MapController mMapController = null;
private MKMapViewListener mMapListener = null;
private Button btMapZoomOut;
private Button btMapZoomIn;
private ImageButton ibMLLocate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.im_poi);
Log.i("ImPoi - onCreate=================");
MyApplication application = (MyApplication)getApplication();
if (application.mBMapManager == null) {
application.mBMapManager = new BMapManager(getApplicationContext());
application.mBMapManager.init(new MyApplication.MyGeneralListener());
}
btMapZoomOut = (Button) findViewById(R.id.btMapZoomOut);
btMapZoomOut.setOnClickListener(onClickListener);
btMapZoomIn = (Button) findViewById(R.id.btMapZoomIn);
btMapZoomIn.setOnClickListener(onClickListener);
ibMLLocate = (ImageButton) findViewById(R.id.ibMLLocate);
ibMLLocate.setOnClickListener(onClickListener);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapController = mMapView.getController();
mMapController.enableClick(true);
mMapController.setZoom(18);
mMapView.setBuiltInZoomControls(false);
// 定位初始化
myListener = new MyLocationListenner();
mLocClient = new LocationClient(this);
locData = new LocationData();
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(5000);
mLocClient.setLocOption(option);
mLocClient.start();
// 定位图层初始化
myLocationOverlay = new locationOverlay(mMapView);
myLocationOverlay.setData(locData);
myLocationOverlay.setLocationMode(LocationMode.NORMAL);
// 添加定位图层
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
mMapView.refresh();
mMapListener = new MKMapViewListener() {
@Override
public void onMapMoveFinish() {
}
@Override
public void onClickMapPoi(MapPoi mapPoiInfo) {
/**
* 在此处理底图poi点击事件 显示底图poi名称并移动至该点 设置过:
* mMapController.enableClick(true); 时,此回调才能被触发
*/
}
@SuppressLint("SimpleDateFormat") @Override
public void onGetCurrentMap(Bitmap bitmap) {
if (poiInfo == null) {
Toast.makeText(ImPoi.this, "获取定位信息失败", Toast.LENGTH_SHORT).show();
return;
}
String filePath = SmackUtil.getSDCardDirectory("picture");
Date today = new Date();
String fileHead = new SimpleDateFormat("yyyyMMdd").format(today);// 时分秒
String fileEnd = new SimpleDateFormat("HHmmSS").format(today);// 时分秒
String fileName = "IMG_" + fileHead + "_" + fileEnd + ".png";
String fileUrl = filePath + "/" + fileName;
new MyThread(bitmap,fileUrl).start();
finish();
}
@Override
public void onMapAnimationFinish() {
System.out.println("onMapAnimationFinish =================");
}
@Override
public void onMapLoadFinish() {
System.out.println("onMapLoadFinish =================");
}
};
mMapView.regMapViewListener(application.mBMapManager,mMapListener);
}
public class MyThread extends Thread{
private String fileUrl;
private Bitmap bitmap;
public MyThread(Bitmap bitmap, String fileUrl) {
this.fileUrl = fileUrl;
this.bitmap = bitmap;
}
@Override
public void run() {
try {
File mapFile = new File(fileUrl);
FileOutputStream out = new FileOutputStream(mapFile);
if(bitmap.compress(Bitmap.CompressFormat.PNG, 70, out)) {
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
/**
* 单击事件
*/
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btMapZoomOut:
mMapController.setZoom(mMapView.getZoomLevel()+1);
break;
case R.id.btMapZoomIn:
mMapController.setZoom(mMapView.getZoomLevel()-1);
break;
case R.id.ibMLLocate:
mLocClient.requestLocation();
break;
case R.id.btnSend:
mMapView.getCurrentMap();
break;
}
}
};
// 继承MyLocationOverlay重写dispatchTap实现点击处理
public class locationOverlay extends MyLocationOverlay {
public locationOverlay(MapView mapView) {
super(mapView);
}
@Override
protected boolean dispatchTap() {
return false;
}
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
try {
locData.latitude = location.getLatitude();
locData.longitude = location.getLongitude();
// 如果不显示定位精度圈,将accuracy赋值为0即可
locData.accuracy = location.getRadius();
// 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。
locData.direction = location.getDerect();
// 更新定位数据
myLocationOverlay.setData(locData);
mMapView.refresh();
/**
* 移动地图到定位点
*/
GeoPoint geoPoint = new GeoPoint(
(int) (locData.latitude * 1e6),
(int) (locData.longitude * 1e6));
mMapController.animateTo(geoPoint);
myLocationOverlay.setLocationMode(LocationMode.NORMAL);
} catch (Exception e) {
Log.i("ImLocation - MyLocationListenner: " + e.getMessage());
}
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null) {
return;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMapView != null) {
mMapView.destroyDrawingCache();
mMapView.destroy();
}
if (mLocClient != null) {
mLocClient.stop();
mLocClient.unRegisterLocationListener(myListener);
}
}
}
mLocClient.requestLocation(); 对地图进行定位
mMapController.setZoom(mMapView.getZoomLevel()+1); 放大地图
mMapController.setZoom(mMapView.getZoomLevel()-1); 缩小地图
myLocationOverlay.setLocationMode(LocationMode.NORMAL);设置地图层状态 (LOC:定位、FOLLOW:跟随、COMPASS:罗盘)