package com.mim.routeplanning;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.mim.routeplanning.overlayutil.DrivingRouteOverlay;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private BDLocationListener myLocListener = null;
private LocationClient mLocClient = null;
private boolean isFirstLoc = true;
private BDLocation myCurLocation = null;
private RoutePlanSearch mSearch = null;
private OnGetRoutePlanResultListener myRouteListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton btnLoc = (FloatingActionButton) findViewById(R.id.btn_loc);
FloatingActionButton btnRoute = (FloatingActionButton) findViewById(R.id.btn_route);
btnLoc.setOnClickListener(this);
btnRoute.setOnClickListener(this);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.showZoomControls(false);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
//BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null);
mBaiduMap.setMyLocationConfigeration(config);
// 定位初始化
initLocation();
mSearch = RoutePlanSearch.newInstance();
myRouteListener = new MyRouteListener();
mSearch.setOnGetRoutePlanResultListener(myRouteListener);
}
private void initLocation() {
myLocListener = new MyLocationListener();
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(myLocListener);
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
int span = 5000;
option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(false);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIsNeedLocationDescribe(false);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
option.setNeedDeviceDirect(true);
mLocClient.setLocOption(option);
mLocClient.start();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
mSearch.destroy();
// 当不需要定位图层时关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_loc:
loc();
break;
case R.id.btn_route:
PlanNode stNode = PlanNode.withCityNameAndPlaceName("西安", "西安电子科技大学");
PlanNode enNode = PlanNode.withCityNameAndPlaceName("西安", "公交五公司");
mSearch.drivingSearch((new DrivingRoutePlanOption())
.from(stNode)
.to(enNode));
break;
default:
break;
}
}
private void loc() {
if (myCurLocation != null) {
LatLng ll = new LatLng(myCurLocation.getLatitude(),
myCurLocation.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, Constants.MAX_ZOOM - 2);
mBaiduMap.animateMapStatus(u);
}
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
myCurLocation = bdLocation;
Log.d(Constants.TAG, "type:" + bdLocation.getLocType() + "\n"