1. 程式人生 > >高德地圖定位、新增定點陣圖標、連線(二)

高德地圖定位、新增定點陣圖標、連線(二)

定位之後想在地圖中顯示,那麼就要在新增一個jar包,該jar包中有地圖的控制元件

一、新增定位標記

1、在build.gradle中新增

compile files('libs/AMap_3DMap_V3.3.1_20160419.jar')

使用的是3D的,因為使用3D的是刪格化的,載入起來比較快

2、在佈局檔案中新增控制元件

<com.amap.api.maps.MapView
     android:id="@+id/map"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
> </com.amap.api.maps.MapView>

3、宣告MapView物件,並找到該控制元件

 private MapView mapView;
 mapView = (MapView) findViewById(R.id.map);

4、宣告AMap物件,並通過控制元件找到AMap物件

//宣告
private AMap aMap;
//得到aMap物件
aMap = mapView.getMap();

5、新增定位標記
1)獲取定位的座標
為LatLng物件設定經緯度,(緯度,經度)

LatLng latLng = new LatLng(aMapLocation.getLatitude
(),aMapLocation.getLongitude());

2)獲取MarkOptions物件,並設定引數

MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);

3)新增標記

aMap.addMarker(markerOptions);

綜上:只要記住新增標記是通過addMarker();方法即可,以為當你使用該方法時你就會知道需要的引數為MarkOptions,但是也不許記得用MarkOptions的option方法新增引數,這樣,就知道還需要LatLng物件,所以就一步一步向前推,然後就大功告成了。好多的應用中都是這樣的,倒敘著去記更容易理解和記憶。

二、地圖上兩點連線

聯絡與新增定點陣圖標相似,不過使用的是PolylineOptions物件。

1、獲取PolylineOptions物件

PolylineOptions polylineOptions = new PolylineOptions();

2、設定PolylineOptions物件的屬性

 //設定線的寬度
 polylineOptions.width(10);
 //設定線的顏色
 polylineOptions.color(Color.RED);
 //設定線是否可見
 polylineOptions.visible(true);

3、獲得點

 for(int i = 0;i<latLngList.size();i++){
            polylineOptions.add(latLngList.get(i));
        }

4、畫線

mMapView.getMap().addPolyline(polylineOptions);

mMapView為MapView控制元件。