1.先根據官網介紹下載相應的SDK包。
**注意,從座標拾取系統上拾取的座標,在使用時需要把經緯度反過來填寫!!!
(拾取的是(10,20),設置時應該爲(20,10))才能顯示正確的位置**
a:定位設置
//三角箭頭跟隨手機方向轉動 //初始化定位藍點樣式類 MyLocationStyle myLocationStyle = new MyLocationStyle(); // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE); // 連續定位、且將視角移動到地圖中心點,定位點依照設備方向旋轉,並且會跟隨設備移動。 // (1秒1次定位)如果不設置myLocationType,默認也會執行此種模式。 //設置連續定位模式下的定位間隔,只在連續定位模式下生效,單次定位模式下不會生效。單位爲毫秒。 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE); myLocationStyle.interval(10000);//持續定位,間隔10s //設置定位藍點的Style aMap.setMyLocationStyle(myLocationStyle); //設置默認定位按鈕是否顯示,自行設置。(true:地圖上顯示定位按鈕,點擊後自動定位當前位置) aMap.getUiSettings().setMyLocationButtonEnabled(true); myLocationStyle.showMyLocation(true); //設置爲true表示啓動顯示定位藍點,false表示隱藏定位藍點並不進行定位,默認是false。 aMap.setMyLocationEnabled(true);
b:顯示地圖(可修改默認顯示位置)
佈局裏面放置地圖控件:
<com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/>
private MapView mapView; //地圖控件 private AMap aMap;//地圖控制器對象
mapView = (MapView) findViewById(R.id.map); // a1.此方法須覆寫,虛擬機需要在很多情況下保存地圖繪製的當前狀態。 mapView.onCreate(savedInstanceState); //a2.初始化地圖控制器對象(地圖可以顯示了,默認顯示北京) if (aMap == null) { aMap = mapView.getMap(); } //-------------------- //修改默認顯示位置 LatLng latLng = new LatLng(30.489444,114.417544);//構造一個位置 //參數1:座標點,參數2: 地圖放大倍數
//此時顯示指定位置
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));c:設置覆蓋物(給一個座標點設置圖標)
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10, 30), 19)); MarkerOptions options = new MarkerOptions(); options.position(new LatLng(10, 30));//指定地點 options.title("指定位置");//點擊圖標會顯示此標題 options.visible(true); options.draggable(true);//marker可以拖動 //maker 圖標 BitmapDescriptor bp = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stop)); options.icon(bp); aMap.addMarker(options);
d:在地圖上畫圖形
畫線(根據座標點畫折現)
List<LatLng> latLngs = new ArrayList<>(); latLngs.add(new LatLng(30.489444,114.417544)); latLngs.add(new LatLng(30.49878,114.417472)); latLngs.add(new LatLng(30.492307,114.436947)); latLngs.add(new LatLng(30.485772,114.427461)); Polyline line = aMap.addPolyline(new PolylineOptions().addAll(latLngs).width(20).color(Color.argb(255, 1, 1, 1))); line.setVisible(true); 畫圓:
LatLng latLng = new LatLng(30.489444,114.417544);//圓心點 Circle circle = aMap.addCircle(new CircleOptions() .center(latLng) .radius(1000)//半徑 .fillColor(Color.argb(50,1, 1, 1))//填充顏色 .strokeColor(Color.RED)//邊線顏色 .strokeWidth(20));//邊線寬