riku
2025-10-17 fbae5f3ea74727ccadc48314a864a1ea0099a945
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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<Subtask> subTaskListCurrent;
    private List<Scense> 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<Marker> 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<Subtask>) 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();
        }
    }
}