|
@@ -0,0 +1,174 @@
|
|
|
+package com.quansu.gaode.activity;
|
|
|
+
|
|
|
+import android.graphics.Color;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
+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.MarkerOptions;
|
|
|
+import com.amap.api.maps.model.PolylineOptions;
|
|
|
+import com.amap.api.services.core.LatLonPoint;
|
|
|
+import com.amap.api.services.geocoder.GeocodeSearch;
|
|
|
+import com.githang.statusbar.StatusBarCompat;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
+import com.quansu.gaode.R;
|
|
|
+import com.quansu.gaode.utils.AMapUtil;
|
|
|
+import com.ysnows.base.model.PositionBean;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by shihuiyun
|
|
|
+ * on 2020/10/15
|
|
|
+ */
|
|
|
+public class PolylineActivity extends AppCompatActivity {
|
|
|
+ private AMap aMap;
|
|
|
+ private MapView mapView;
|
|
|
+ private ImageView imgBack;
|
|
|
+ private TextView tvStart;
|
|
|
+ private TextView tvEnd;
|
|
|
+ private GeocodeSearch geocoderSearch;
|
|
|
+
|
|
|
+ private List<LatLng> list;
|
|
|
+ private List<PositionBean> item;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
+ StatusBarCompat.setStatusBarColor(this, Color.parseColor("#00ffffff"));
|
|
|
+
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.map_layout);
|
|
|
+
|
|
|
+ item = new Gson().fromJson(getIntent().getStringExtra("item"), new TypeToken<List<PositionBean>>() {
|
|
|
+ }.getType());
|
|
|
+
|
|
|
+ list = showListLat(item);
|
|
|
+ imgBack = findViewById(R.id.img_back);
|
|
|
+ tvStart = findViewById(R.id.tv_start);
|
|
|
+ tvEnd = findViewById(R.id.tv_end);
|
|
|
+ tvStart.setText(item.get(0).position);
|
|
|
+ tvEnd.setText(item.get(item.size()-1).position);
|
|
|
+ imgBack.setOnClickListener(v -> {
|
|
|
+ finish();
|
|
|
+ });
|
|
|
+
|
|
|
+ mapView = (MapView) findViewById(R.id.map);
|
|
|
+ mapView.onCreate(savedInstanceState);// 此方法必须重写
|
|
|
+ init();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 初始化AMap对象
|
|
|
+ */
|
|
|
+ private void init() {
|
|
|
+ if (aMap == null) {
|
|
|
+ aMap = mapView.getMap();
|
|
|
+ geocoderSearch = new GeocodeSearch(this);
|
|
|
+ setUpMap();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setUpMap() {
|
|
|
+
|
|
|
+ //起点位置和 地图界面大小控制
|
|
|
+ aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(list.get(0), 7));
|
|
|
+ aMap.setMapTextZIndex(2);
|
|
|
+ // 绘制一条带有纹理的直线
|
|
|
+ aMap.addPolyline((new PolylineOptions())
|
|
|
+ //手动数据测试
|
|
|
+ //.add(new LatLng(26.57, 106.71),new LatLng(26.14,105.55),new LatLng(26.58, 104.82), new LatLng(30.67, 104.06))
|
|
|
+ //集合数据
|
|
|
+ .addAll(list)
|
|
|
+ //线的宽度
|
|
|
+ .width(30).setDottedLine(true).geodesic(true)
|
|
|
+ //颜色
|
|
|
+ .color(Color.argb(255,255,20,147)));
|
|
|
+
|
|
|
+
|
|
|
+ if(TextUtils.isEmpty(item.get(0).latitude)||TextUtils.isEmpty(item.get(item.size()-1).latitude)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LatLonPoint latLonPoint = new LatLonPoint(Double.parseDouble(item.get(0).latitude),Double.parseDouble(item.get(0).longitude));
|
|
|
+
|
|
|
+ //起点图标
|
|
|
+ View markerView = LayoutInflater.from(this).inflate(R.layout.gaode_start_marker,mapView,false);
|
|
|
+ aMap.addMarker(new MarkerOptions()
|
|
|
+ .position(AMapUtil.convertToLatLng(latLonPoint))
|
|
|
+ .icon(BitmapDescriptorFactory.fromView(markerView)));
|
|
|
+
|
|
|
+ //终点坐标
|
|
|
+ View markerEndView = LayoutInflater.from(this).inflate(R.layout.gaode_end_marker,mapView,false);
|
|
|
+
|
|
|
+ LatLonPoint latLonPointEnd = new LatLonPoint(Double.parseDouble(item.get(item.size()-1).latitude),Double.parseDouble(item.get(item.size()-1).longitude));
|
|
|
+ aMap.addMarker(new MarkerOptions()
|
|
|
+ .position(AMapUtil.convertToLatLng(latLonPointEnd))
|
|
|
+ .icon(BitmapDescriptorFactory.fromView(markerEndView)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法必须重写
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ mapView.onResume();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法必须重写
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void onPause() {
|
|
|
+ super.onPause();
|
|
|
+ mapView.onPause();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 方法必须重写
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void onSaveInstanceState(Bundle outState) {
|
|
|
+ super.onSaveInstanceState(outState);
|
|
|
+ mapView.onSaveInstanceState(outState);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法必须重写
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ mapView.onDestroy();
|
|
|
+ }
|
|
|
+ /***
|
|
|
+ *经纬度集合
|
|
|
+ */
|
|
|
+ private List<LatLng> showListLat(List<PositionBean> item){
|
|
|
+
|
|
|
+
|
|
|
+ List<LatLng> points = new ArrayList<LatLng>();
|
|
|
+
|
|
|
+ for(PositionBean ss:item){
|
|
|
+ if(!TextUtils.isEmpty(ss.latitude)&&!TextUtils.isEmpty(ss.longitude)) {
|
|
|
+ Log.e("-shy-", "latitude=: "+ss.latitude+"//longitude="+ss.longitude );
|
|
|
+ points.add(new LatLng(Double.parseDouble(ss.latitude), Double.parseDouble(ss.longitude)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return points;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|