package com.epapnavigation.activity;






import java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
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.map.Geometry;
import com.baidu.mapapi.map.Graphic;
import com.baidu.mapapi.map.GraphicsOverlay;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.map.Symbol;
import com.baidu.mapapi.search.MKRoute;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.epapnavigation.R;
import com.epapnavigation.common.DateUtil;
import com.epapnavigation.common.Declare;
import com.epapnavigation.db.MapLongLatService;
import com.epapnavigation.db.MapPointsService;
import com.epapnavigation.db.MapRouteService;
import com.epapnavigation.domain.MapLongLat;
import com.epapnavigation.domain.MapPoints;
import com.epapnavigation.domain.MapRoute;
import com.epapnavigation.domain.SP_Dict;
import com.epapnavigation.ui.EndPointSpinner;






public class MapNaviActivity extends BaseActivity {



//MapView 是地图主控件  
private MapView mMapView = null;
//用MapController完成地图控制  
private MapController mMapController = null;

// 定位相关
private LocationClient mLocationClient = null;
private MyLocationListener myListener = new MyLocationListener();
private LocationData locData = null;
// 定位图层
locationOverlay myLocationOverlay = null;

private EditText et_startpoint;
private EndPointSpinner sp_endPoint;
private Button btnNavigator,btnRoutePlan ,btn_startcollect,btn_stopcollect;
private List mapLongLatList ;

private static int POINTTYPEEND = 1;
//服务类
private MapLongLatService mapLongLatService;
private MapPointsService mapPointsService;
private MapRouteService mapRouteService;

Declare appDeclare; //控制采集流程
private Boolean collectStartGps = false;



@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

appDeclare = (Declare) this.getApplication();
if (appDeclare.mBMapManager == null) {
appDeclare.mBMapManager = new BMapManager(getApplicationContext());
//如果BMapManager没有初始化则初始化BMapManager  
appDeclare.mBMapManager.init(new Declare.MyGeneralListener());
}

setContentView(R.layout.activity_mapnavi);
initView();
initMap();

}
//初期化视图
private void initView(){

et_startpoint = (EditText) findViewById(R.id.et_startpoint);
sp_endPoint = (EndPointSpinner) findViewById(R.id.sp_endPoint);
sp_endPoint.setSelection(0);
//实例化服务类
mapLongLatService = new MapLongLatService(mContext);
mapPointsService = new MapPointsService(mContext);
mapRouteService = new MapRouteService(mContext);

//绑定目的地列表
endpointsBind();

//启动导航
btnNavigator = (Button) findViewById(R.id.btnNavigator);

btnNavigator.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {



}
});
//路径规划
btnRoutePlan = (Button) findViewById(R.id.btnRoutePlan);
btnRoutePlan.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//加载地图经纬度数据源
initData();
if(null != mapLongLatList){
getRoute(mapLongLatList);
}

}
});



//开始采集
btn_startcollect = (Button) findViewById(R.id.btn_startcollect);
btn_startcollect.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
collectStartGps = true;
if(dbLatitude.equals(0.0) || dbLongitude.equals(0.0)){
Toast.makeText(mContext, "操作失误", Toast.LENGTH_LONG).show();
}else{
MapPoints mapPoint = new MapPoints();
mapPoint.setCreateDate(DateUtil.getStringDate());
mapPoint.setPointLat(dbLatitude);
mapPoint.setPointLong(dbLongitude);
mapPoint.setPointName(startpointName);
try {
mapPointsService.UpdateMapPoint(mapPoint);
} catch (Exception e) {
Toast.makeText(mContext, "操作失误", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Toast.makeText(mContext, "开始采集!", Toast.LENGTH_LONG).show();
}
}
});
//采集结束
btn_stopcollect = (Button) findViewById(R.id.btn_stopcollect);
btn_stopcollect.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
collectStartGps = false;
if(dbLatitude.equals(0.0) || dbLongitude.equals(0.0)){
Toast.makeText(mContext, "操作失误", Toast.LENGTH_LONG).show();
}else{
MapPoints mapPoint = new MapPoints();
mapPoint.setCreateDate(DateUtil.getStringDate());
mapPoint.setPointLat(dbLatitude);
mapPoint.setPointLong(dbLongitude);
mapPoint.setPointName(endpointName);
try {
mapPointsService.UpdateMapPoint(mapPoint);
} catch (Exception e) {
Toast.makeText(mContext, "操作失误", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Toast.makeText(mContext, "结束采集!", Toast.LENGTH_LONG).show();}
}
});
}
//加载地图经纬度数据源
private void initData(){
//导航点ID
if(sp_endPoint.getCount() > 0){
int mappointID = ((SP_Dict) sp_endPoint.getSelectedItem()).getId(); 
//路线ID
try {
int maproutID = mapRouteService.SeeMapRoute(mappointID).getMapRouteID();
mapLongLatList = mapLongLatService.getMapLongLats(maproutID);
} catch (Exception e) {

e.printStackTrace();
}
}

}

//Bind 目的地列表
private void endpointsBind(){
try {
List mapPoints = mapPointsService.SeeMapPointList(POINTTYPEEND);
sp_endPoint.setList((ArrayList)mapPoints);
ArrayAdapter mapPointAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, mapPoints);
sp_endPoint.setAdapter(mapPointAdapter);

} catch (Exception e) {
Toast.makeText(mContext, "系统错误", Toast.LENGTH_SHORT).show();
}

}
    /**
     * 绘制折线,该折线状态随地图状态变化
     * @return 折线对象
     */
private Graphic drawLine(List mapLongLats){  
   //构建线
  Geometry lineGeometry = new Geometry();  
  int counts = mapLongLats.size();
  //设定折线点坐标
  GeoPoint[] linePoints = new GeoPoint[counts];
  for(int i =0 ;i   linePoints[i] = mapLongLats.get(i).getGeopoint();
  }
  lineGeometry.setPolyLine(linePoints);
  //设定样式
  Symbol lineSymbol = new Symbol();
  Symbol.Color lineColor = lineSymbol.new Color();
  lineColor.red = 255;
  lineColor.green = 0;
  lineColor.blue = 0;
  lineColor.alpha = 255;
  lineSymbol.setLineSymbol(lineColor, 10);
  //生成Graphic对象
  Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);
  return lineGraphic;
    }

/**
* 绘制路线

* @param List
*            
* */
private void getRoute(List mapLongLatList) {
// 用站点数据构建一个MKRoute
MKRoute route = new MKRoute();
// 起点坐标
GeoPoint start = mapLongLatList.get(0).getGeopoint();
// 终点坐标
GeoPoint stop = mapLongLatList.get((mapLongLatList.size() - 1)).getGeopoint();
int size = mapLongLatList.size();
GeoPoint[][] routeData = new GeoPoint[1][];
GeoPoint[] step = new GeoPoint[size];
for (int i = 0; i < mapLongLatList.size(); i++) {
step[i] = mapLongLatList.get(i).getGeopoint();
}
routeData[0] = step;
route.customizeRoute(start, stop, routeData);
RouteOverlay routeOverlay = new RouteOverlay(MapNaviActivity.this, mMapView);
routeOverlay.setData(route);
//向地图添加构造好的RouteOverlay
mMapView.getOverlays().add(routeOverlay);
//执行刷新使生效
   mMapView.refresh();
}
/**
* 将自定义线条和关键点图标加载到地图上

* */
private void initOverlay(){
  GraphicsOverlay graphicsOverlay = new GraphicsOverlay(mMapView);
        mMapView.getOverlays().add(graphicsOverlay);
}

private void initMap(){
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
// 实例化地图控制器
mMapController = mMapView.getController();
mMapController.setZoom(14);
mMapController.enableClick(true);
mMapView.setBuiltInZoomControls(true);

// 定位初始化
locData = new LocationData();
mLocationClient = new LocationClient(getApplicationContext());
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
option.setAddrType("false");//返回的定位结果包含地址信息
option.setPriority(LocationClientOption.GpsFirst);//GPS优先
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(myListener);
mLocationClient.start();
// 定位图层初始化
myLocationOverlay = new locationOverlay(mMapView);
// 设置定位数据
myLocationOverlay.setData(locData);
// 添加定位图层
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
//修改定位数据后刷新图层生效
mMapView.refresh();

}



/**
* BDLocationListener接口有2个方法需要实现: 1.接收异步返回的定位结果,参数是BDLocation类型参数。
* 2.接收异步返回的POI查询结果,参数是BDLocation类型参数。

*/
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {


if (location == null)
return;
locData.latitude = location.getLatitude();
locData.longitude = location.getLongitude();
dbLatitude = location.getLatitude();
dbLongitude = location.getLongitude();
if(collectStartGps){
try {
MapRoute mapRoute = mapRouteService.SeeMapRoute(endpointID);
   int maprouteID = mapRoute.getMapRouteID();
   MapLongLat mapLongLat = new MapLongLat();
   mapLongLat.setLatitude(dbLatitude);
   mapLongLat.setLongitude(dbLongitude);
   mapLongLat.setMapRouteID(maprouteID);
   mapLongLatService.AddMapLongLat(mapLongLat);
} catch (Exception e) {
Toast.makeText(mContext, "经纬度采集失败", Toast.LENGTH_LONG).show();
}
}
// 如果不显示定位精度圈,将accuracy赋值为0即可
locData.accuracy = location.getRadius();
// 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。
locData.direction = location.getDerect();

if (myLocationOverlay == null) {
myLocationOverlay = new locationOverlay(mMapView);
// 添加定位图层
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
}
// 更新定位数据
myLocationOverlay.setData(locData);
mMapController.setCenter(new GeoPoint(
(int) (locData.latitude * 1e6),
(int) (locData.longitude * 1e6)));
// 更新图层数据执行刷新后生效
mMapView.refresh();
// 移动到定位点
mMapController.animateTo(new GeoPoint(
(int) (locData.latitude * 1e6),
(int) (locData.longitude * 1e6)));
}


@Override
public void onReceivePoi(BDLocation arg0) {


}
}

@Override
protected void onDestroy() {
// 退出时销毁定位
if (mLocationClient != null)
mLocationClient.stop();
mMapView.destroy();

super.onDestroy();
}


@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}


@Override
protected void onResume() {
mMapView.onResume();


super.onResume();
}


@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);


}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}

    
  public class locationOverlay extends MyLocationOverlay{


  public locationOverlay(MapView mapView) {
  super(mapView);
 
  }
  @Override
  protected boolean dispatchTap() {
 
  return true;
  }
 
  }


}

更多相关文章

  1. 挨踢人的脚步(2015.11.04)
  2. Android(安卓)百度 Map -----定位功能(工具类)
  3. Android之百度地图定位最详细使用总结
  4. 关于android端的百度地图定位无法定位,经纬度都是4.9E-324的问题
  5. flex兼容 iOS和Android样式兼容 【微信小程序】
  6. Android(安卓)Studio + Eclipse 实现类似微博主页功能APP
  7. 【Android】高德定位错误总结
  8. Android(安卓)开发GPS定位、网络定位、卡尔曼滤波优化结果对比
  9. Android之调用百度地图API规划当前位置到指定位置的路线

随机推荐

  1. 一种巧妙获取Android状态栏高度的办法
  2. Cocos2dx杂记:仿Android提示Toast
  3. 14天学会安卓开发(第十天)Android网络与
  4. 在eclipse创建android 工程
  5. Android开发学习
  6. 今天的小收获
  7. Android核心分析(链接)
  8. Android(安卓)异步和超时处理 例子
  9. 解决Android9.0网络请求无效问题
  10. Android(安卓)打开网络上pdf文件