package cn.flightfeather.thirdappmodule.activity; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import com.ping.greendao.gen.ScenseDao; import java.util.ArrayList; import java.util.List; import cn.flightfeather.thirdappmodule.CommonApplication; import cn.flightfeather.thirdappmodule.R; import cn.flightfeather.thirdappmodule.bean.entity.Scense; import cn.flightfeather.thirdappmodule.bean.entity.Subtask; import cn.flightfeather.thirdappmodule.util.AmapNavi; public class SubTaskMapActivity extends AppCompatActivity implements View.OnClickListener { private List subTaskListCurrent; private List scenseList; private ScenseDao scenseDao; private CommonApplication application; private MapView mv_main; private AMap aMap; private int hightLightPosition = 0; private TextView tv_name; private TextView tv_address; private TextView tv_latlng; private TextView tv_contatsName; private TextView tv_contatsTele; private LinearLayout ll_blank; private LinearLayout ll_detail; private List markerList; private LinearLayout ll_call; private LinearLayout ll_navi; private LinearLayout ll_inspection; private Scense scenseCurrent; private TextView tv_type; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub_task_map); application = (CommonApplication) getApplication(); getSupportActionBar().setTitle("子任务地图"); initUI(); initMap(savedInstanceState); initLocation(); initData(); } //初始化界面 private void initUI() { mv_main = (MapView) findViewById(R.id.mv_main); tv_name = (TextView) findViewById(R.id.tv_sense_name); tv_address = (TextView) findViewById(R.id.tv_sense_address); tv_latlng = (TextView) findViewById(R.id.tv_sense_latlng); tv_contatsName = (TextView) findViewById(R.id.tv_contact_name); tv_contatsTele = (TextView) findViewById(R.id.tv_contact_tele); ll_blank = (LinearLayout) findViewById(R.id.ll_sense_detail_blank); ll_detail = (LinearLayout) findViewById(R.id.ll_sense_detail); ll_call = (LinearLayout) findViewById(R.id.ll_call); ll_navi = (LinearLayout) findViewById(R.id.ll_navi); ll_inspection = (LinearLayout) findViewById(R.id.ll_inspection); tv_type = (TextView) findViewById(R.id.tv_sense_type); ll_blank.setOnClickListener(this); ll_detail.setVisibility(View.INVISIBLE); ll_call.setOnClickListener(this); ll_navi.setOnClickListener(this); ll_inspection.setOnClickListener(this); } //初始化地图 private void initMap(Bundle savedInstanceState) { mv_main.onCreate(savedInstanceState); aMap = mv_main.getMap(); aMap.setRenderFps(60); aMap.getUiSettings().setZoomControlsEnabled(false); aMap.getUiSettings().setScaleControlsEnabled(true); aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { normalMarker(hightLightPosition); hightLightPosition = Integer.valueOf(marker.getSnippet()); highLightMarker(hightLightPosition); scenseCurrent = scenseList.get(hightLightPosition); if (hightLightPosition > -1) { Scense scense = scenseList.get(hightLightPosition); tv_name.setText(scense.getName()); tv_address.setText(getScenceAddress(scense)); tv_latlng.setText(scense.getLatitude() + " " + scense.getLongitude()); tv_contatsName.setText(scense.getContacts()); tv_contatsTele.setText(scense.getContactst()); tv_type.setText(scense.getType()); ll_detail.setVisibility(View.VISIBLE); } return false; } }); } //初始化位置 public void initLocation() { MyLocationStyle myLocationStyle; myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 // myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 myLocationStyle.showMyLocation(true); myLocationStyle.radiusFillColor(getResources().getColor(R.color.transparent)); myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//连续定位、蓝点不会移动到地图中心点,定位点依照设备方向旋转,并且蓝点会跟随设备移动 aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style aMap.getUiSettings().setMyLocationButtonEnabled(false);//设置默认定位按钮是否显示,非必需设置。 aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 } //初始化数据 private void initData() { Intent intent = getIntent(); subTaskListCurrent = (List) intent.getSerializableExtra("subTaskListCurrent"); //根据子任务查出场景并添加到地图上 if (subTaskListCurrent != null && subTaskListCurrent.size() > 0) { scenseList = new ArrayList<>(); scenseDao = application.getDaoSession().getScenseDao(); markerList = new ArrayList<>(); for (int i = 0; i < subTaskListCurrent.size(); i++) { Subtask subtask = subTaskListCurrent.get(i); Scense scense = scenseDao.queryBuilder().where(ScenseDao.Properties.Guid.eq(subtask.getScenseid())).unique(); scenseList.add(scense); if (scense != null) { LatLng sourceLatLng = new LatLng(scense.getLatitude(), scense.getLongitude()); if (subtask.getStatus().equals("已结束")) { addMarker(sourceLatLng, i + "", true); } else { addMarker(sourceLatLng, i + "", false); } if (i == 0) { aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sourceLatLng, 13f)); } } } } } //添加marker private void addMarker(LatLng latLng, String snippet, boolean isFinished) { MarkerOptions options = new MarkerOptions(); //将marker放大 Bitmap bm = null; if (isFinished) { bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_gray); } else { bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_red); } Matrix matrix = new Matrix(); matrix.setScale(0.36f, 0.36f); Bitmap bmBig = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); options.icon(BitmapDescriptorFactory.fromBitmap(bmBig)); options.position(latLng); options.snippet(snippet); Marker marker = aMap.addMarker(options); marker.setInfoWindowEnable(false); markerList.add(marker); } private void highLightMarker(int position) { //添加marker Marker markerRaw = markerList.get(position); markerRaw.remove(); MarkerOptions markerOption = new MarkerOptions(); markerOption.position(markerRaw.getPosition()); //将marker放大 Bitmap bm = null; Subtask subtask = subTaskListCurrent.get(position); if (subtask.getStatus().equals("已结束")) { bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_black); } else { bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_red_sel); } Matrix matrix = new Matrix(); matrix.setScale(0.36f, 0.36f); Bitmap bmBig = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); markerOption.icon(BitmapDescriptorFactory.fromBitmap(bmBig)); markerOption.snippet("" + position); Marker marker = aMap.addMarker(markerOption); marker.setInfoWindowEnable(false); markerList.set(position, marker); } private void normalMarker(int position) { //添加marker Marker markerRaw = markerList.get(position); markerRaw.remove(); MarkerOptions markerOption = new MarkerOptions(); markerOption.position(markerRaw.getPosition()); //将marker放大 Bitmap bm = null; Subtask subtask = subTaskListCurrent.get(position); if (subtask.getStatus().equals("已结束")) { bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_gray); } else { bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_red); } Matrix matrix = new Matrix(); matrix.setScale(0.36f, 0.36f); Bitmap bmBig = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); markerOption.icon(BitmapDescriptorFactory.fromBitmap(bmBig)); markerOption.snippet("" + position); Marker marker = aMap.addMarker(markerOption); marker.setInfoWindowEnable(false); markerList.set(position, marker); } //获取string类型的场景地址 public String getScenceAddress(Scense scenseCurrent) { String a = ""; if (scenseCurrent != null) { if (scenseCurrent.getTownname() == null) { scenseCurrent.setTownname(""); } a = scenseCurrent.getCityname() + scenseCurrent.getDistrictname() + scenseCurrent.getTownname() + scenseCurrent.getLocation(); } return a; } private void showCallDialog() { if (scenseCurrent.getContactst() != null && scenseCurrent.getContactst().length() > 0) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("要拨打电话吗"); dialog.setMessage(tv_contatsName.getText() + " " + tv_contatsTele.getText()); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { call(scenseCurrent.getContactst()); } }); dialog.setNegativeButton("取消", null); dialog.show(); } else { Toast.makeText(this, "无号码不能拨打", Toast.LENGTH_SHORT).show(); } } private void call(String tete) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tete)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } private void startNavi() { AmapNavi aMapNavi = new AmapNavi(this); Marker marker = markerList.get(hightLightPosition); aMapNavi.startNavi(marker.getOptions().getPosition().latitude, marker.getOptions().getPosition().longitude); } @Override public void onDestroy() { super.onDestroy(); aMap.setMyLocationEnabled(false); mv_main.onDestroy(); } @Override public void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mv_main.onResume(); initLocation(); } @Override public void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 aMap.setMyLocationEnabled(false); mv_main.onPause(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mv_main.onSaveInstanceState(outState); } @Override public void onClick(View view) { int id = view.getId(); if (id == R.id.ll_sense_detail_blank) { normalMarker(hightLightPosition); ll_detail.setVisibility(View.INVISIBLE); } else if (id == R.id.ll_call) { showCallDialog(); } else if (id == R.id.ll_navi) { startNavi(); } else if (id == R.id.ll_inspection) { Intent intent = new Intent(); intent.putExtra("position", hightLightPosition); setResult(RESULT_OK, intent); finish(); } } }