Advertisement

Android 高德地图 多点连线 线路自定变色

阅读量:
复制代码
    // A code block
复制代码
    // An highlighted block
    
    demo地址 附带key
    
    
    public class PolylineActivity extends AppCompatActivity {
    
    	private AMap aMap;
    	private MapView mapView;
    	private GeocodeSearch geocoderSearch;
    
    	private List<LatLng> list;
    	private List<LatLng> list1;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.map_layout);
    		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() {
    		list = showListLat();
    		//起点位置和  地图界面大小控制
    		aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(list.get(0), 15));
    		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(10).setDottedLine(false).geodesic(true)
    				//颜色
    				.color(Color.argb(255,255,20,147)));
    
    		LatLonPoint latLonPoint = new LatLonPoint(34.224944,117.202596);
    		//起点图标
    		aMap.addMarker(new MarkerOptions()
    				.position(AMapUtil.convertToLatLng(latLonPoint))
    				.icon(BitmapDescriptorFactory.fromResource(R.drawable.start)));
    
    
    		list1 = showListLat1();
    		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(list1)
    				//线的宽度
    				.width(10).setDottedLine(false).geodesic(true)
    				//颜色
    				.color(Color.argb(100,100,20,147)));
    
    		//终点坐标
    		LatLonPoint latLonPointEnd1 = new LatLonPoint(34.215013,117.209835);
    		aMap.addMarker(new MarkerOptions()
    				.position(AMapUtil.convertToLatLng(latLonPointEnd1))
    				.icon(BitmapDescriptorFactory.fromResource(R.drawable.end)));
    	}
    
    	/** * 方法必须重写
    	 */
    	@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<LatLng> points = new ArrayList<LatLng>();
    		for (int i = 0; i < coords.length; i += 2) {
    			points.add(new LatLng(coords[i+1], coords[i]));
    		}
    		return points;
    	}
    
    	private double[] coords = {
    			117.202596,34.224944,
    			117.202836,34.213434,
    			117.203934,34.210573,
    			117.215414,34.212578,
    			117.214921,34.206988,
    			117.219641,34.211052,
    			117.222838,34.210754
    		};
    
    	private List<LatLng> showListLat1(){
    		List<LatLng> points = new ArrayList<LatLng>();
    		for (int i = 0; i < coords1.length; i += 2) {
    			points.add(new LatLng(coords1[i+1], coords1[i]));
    		}
    		return points;
    	}
    	private double[] coords1 = {
    			117.222838,34.210754,
    			117.225456,34.218526,
    			117.209835,34.215013,
    		};
    
    }
    
    
    
    javascript
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-17/BDksUebAu4y7lXnEVLMItPv3mQpN.png)

全部评论 (0)

还没有任何评论哟~