Java tutorial
package com.tsroad.map; import java.util.ArrayList; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import com.amap.api.maps.AMap; import com.amap.api.maps.AMapOptions; import com.amap.api.maps.AMapUtils; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.SupportMapFragment; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.CameraPosition; 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.NavigateArrowOptions; import com.amap.api.maps.model.PolylineOptions; /** % @authors Keung Charteris & T.s.road CZQ % @version 1.0 ($Revision$) % @addr. GUET, Gui Lin, 540001, P.R.China % @contact : cztsiang@gmail.com % @date Copyright(c) 2016-2020, All rights reserved. % This is an open access code distributed under the Creative Commons Attribution License, which permits % unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited. * Created by tsroad on 5/5/15. */ public class SetTraceActivity extends FragmentActivity { private AMap aMap; private Button replayButton; private SeekBar processbar; private Marker marker = null;// ? public Handler timer = new Handler();// public Runnable runnable = null; // ?? private ArrayList<LatLng> latlngList = new ArrayList<LatLng>(); private ArrayList<LatLng> latlngList_path = new ArrayList<LatLng>(); // private ArrayList<LatLng> latlngList_path1 = new ArrayList<LatLng>(); private static final LatLng marker1 = new LatLng(39.24426, 100.18322); private static final LatLng marker2 = new LatLng(39.24426, 104.18322); private static final LatLng marker3 = new LatLng(39.24426, 108.18322); private static final LatLng marker4 = new LatLng(39.24426, 112.18322); private static final LatLng marker5 = new LatLng(39.24426, 116.18322); private static final LatLng marker6 = new LatLng(36.24426, 100.18322); private static final LatLng marker7 = new LatLng(36.24426, 104.18322); private static final LatLng marker8 = new LatLng(36.24426, 108.18322); private static final LatLng marker9 = new LatLng(36.24426, 112.18322); private static final LatLng marker10 = new LatLng(36.24426, 116.18322); Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.set_trace); context = this; init(); float f = 0; for (int i = 0; i < latlngList.size() - 1; i++) { f += AMapUtils.calculateLineDistance(latlngList.get(i), latlngList.get(i + 1)); } Log.i("float", String.valueOf(f / 1000)); } /** * ?AMap */ private void init() { replayButton = (Button) findViewById(R.id.btn_replay); processbar = (SeekBar) findViewById(R.id.process_bar); processbar.setSelected(false); processbar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); // ? processbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { // ?OnSeeBarChangeListener // OnStartTrackingTouch,? @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } // onProgressChanged?? @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub latlngList_path.clear(); if (progress != 0) { for (int i = 0; i < seekBar.getProgress(); i++) { latlngList_path.add(latlngList.get(i)); } drawLine(latlngList_path, progress); } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // onStopTrackingTouch,? @Override public void onStopTrackingTouch(SeekBar seekBar) { latlngList_path.clear(); int current = seekBar.getProgress(); if (current != 0) { for (int i = 0; i < seekBar.getProgress(); i++) { latlngList_path.add(latlngList.get(i)); } drawLine(latlngList_path, current); } } }); // ?runnable runnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub // ?? handler.sendMessage(Message.obtain(handler, 1)); } }; // ?runnable? // TODO Auto-generated method stub if (aMap == null) { aMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); if (aMap != null) { setUpMap(); } } } private void drawLine(ArrayList<LatLng> list, int current) { // TODO Auto-generated method stub aMap.clear(); LatLng replayGeoPoint = latlngList.get(current - 1); if (marker != null) { marker.destroy(); } // ? MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(replayGeoPoint).title("").snippet(" ") .icon(BitmapDescriptorFactory .fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.car))) .anchor(0.5f, 0.5f); marker = aMap.addMarker(markerOptions); // aMap.addMarker(new MarkerOptions().position(latlngList.get(0)).title("") .icon(BitmapDescriptorFactory.fromBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.nav_route_result_start_point)))); // ? if (latlngList_path.size() > 1) { // PolylineOptions polylineOptions = (new PolylineOptions()) // .addAll(latlngList_path) // .color(Color.rgb(9, 129, 240)).width(6.0f); // aMap.addPolyline(polylineOptions); aMap.addNavigateArrow(new NavigateArrowOptions().addAll(latlngList_path).width(10)); aMap.getMaxZoomLevel(); } if (latlngList_path.size() == latlngList.size()) { aMap.addMarker(new MarkerOptions().position(latlngList.get(latlngList.size() - 1)).title("") .icon(BitmapDescriptorFactory.fromBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.nav_route_result_end_point)))); } } // ?? private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { int curpro = processbar.getProgress(); if (curpro != processbar.getMax()) { processbar.setProgress(curpro + 1); timer.postDelayed(runnable, 500);// 0.5? } else { Button button = (Button) findViewById(R.id.btn_replay); button.setText(" ");// ??? ? } } } }; private void setUpMap() { // TODO Auto-generated method stub // ?? latlngList.add(marker1); latlngList.add(marker2); latlngList.add(marker3); latlngList.add(marker4); latlngList.add(marker5); latlngList.add(marker6); latlngList.add(marker7); latlngList.add(marker8); latlngList.add(marker9); latlngList.add(marker10); // latlngList_path.add(marker1); // ? processbar.setMax(latlngList.size()); aMap.setMapType(AMap.MAP_TYPE_NORMAL); aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlngList.get(0), 4)); aMap.getUiSettings().setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_CENTER); aMap.getUiSettings().setMyLocationButtonEnabled(true);// ?? aMap.getUiSettings().setCompassEnabled(true);// ?? // aMap.addNavigateArrow(new NavigateArrowOptions().addAll(latlngList).width(20)); // aMap.moveCamera(CameraUpdateFactory // .newCameraPosition(new CameraPosition(marker5, 16f, 38.5f, 300))); } public void btn_replay_click(View v) { // ??? if (replayButton.getText().toString().trim().equals("")) { if (latlngList.size() > 0) { // ???? 0 if (processbar.getProgress() == processbar.getMax()) { processbar.setProgress(0); } // "?" replayButton.setText(" ? "); timer.postDelayed(runnable, 10); } } else { // timer.removeCallbacks(runnable); replayButton.setText(" "); } } }