高德地圖API簡單記錄

使用MapView的方式
一、引入佈局文件
<com.amap.api.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

二、在Activity中的基本程序代碼(在Fragment中類似,不再贅述)
public class BasicMapActivity extends Activity {
    private MapView mapView;
    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basicmap_activity);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 必須要寫
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    /**
     * 方法必須重寫
     */
    @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();
    }
}

三、地圖操作
1,改變可視區域 :使用CameraUpdate類
aMap.moveCamera(update);//爲沒有動畫
aMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(Constants.ZHONGGUANCUN,18, 0, 30)),1000, callback);//第一個參數爲CameraUpdate,一般使用CameraPosition生成好一些,第二個參數爲動畫時間,第三個參數爲動畫完成或取消監聽接口
(CameraPostion創建:CameraPosition LUJIAZUI = new CameraPosition.Builder().target(Constants.SHANGHAI).zoom(18).bearing(0).tilt(30).build();
target爲LatLng位置,zoom爲縮放級別,bearing爲可視區域指向的方向,以角度爲單位,正北方向爲0度,tilt爲目標可視區域的傾斜度,以角度爲單位。)

2,放大縮小地圖:還是使用CameraUpdate類
aMap.animateCamera(CameraUpdateFactory.zoomIn(),1000, callback);
aMap.animateCamera(CameraUpdateFactory.zoomOut(),1000, callback);

3,設置點擊地圖或移動回調監聽接口
aMap.setOnMapClickListener(this);// 對amap添加單擊地圖事件監聽器
aMap.setOnMapLongClickListener(this);// 對amap添加長按地圖事件監聽器
aMap.setOnCameraChangeListener(this);// 對amap添加移動地圖事件監聽器

4,設置地圖模式:
aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地圖模式

5,顯示交通狀況:
aMap.setTrafficEnabled(false);// 顯示實時交通狀況

6,設置地圖顯示的相關屬性
mUiSettings = aMap.getUiSettings();
mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);// 設置地圖logo顯示在左下方
mUiSettings.setZoomControlsEnabled(false); //是否顯示縮放按鈕
mUiSettings.setCompassEnabled(false); //是否顯示指南針
mUiSettings.setMyLocationButtonEnabled(true); // 是否顯示默認的定位按鈕
(一般設置爲true時,配套的設置有    
     aMap.setMyLocationEnabled(true);// 是否可觸發定位並顯示定位層
     aMap.setLocationSource(this);// 設置定位監聽
    監聽接口
     @Override
     public void activate(OnLocationChangedListener listener) {
         mListener = listener;
         if (mAMapLocationManager == null) {
            mAMapLocationManager = LocationManagerProxy.getInstance(getActivity());
            mAMapLocationManager.setGpsEnable(true);
            mAMapLocationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, this);
        }
    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mAMapLocationManager != null) {
            mAMapLocationManager.removeUpdates(this);
        }
        mAMapLocationManager = null;
    }
)

mUiSettings.setScrollGesturesEnabled(false);//是否允許滑動
mUiSettings.setZoomGesturesEnabled(true);//是否允許縮放手勢
mUiSettings.setTiltGesturesEnabled(true);//是否允許傾斜手勢
mUiSettings.setRotateGesturesEnabled(true);//是否允許旋轉手勢

7,  Marker部分:
(1)aMap.setOnMarkerDragListener(this);// 設置marker可拖拽事件監聽器
          aMap.setOnMapLoadedListener(this);// 設置amap加載成功事件監聽器
          aMap.setOnMarkerClickListener(this);// 設置點擊marker事件監聽器
          aMap.setOnInfoWindowClickListener(this);// 設置點擊infoWindow事件監聽器
          aMap.setInfoWindowAdapter(this);// 設置自定義InfoWindow樣式,可以在監聽接口中返回自己的view

(2)MarkerOptions markerOptions=new MarkerOptions();
           markerOptions.position(someLatlng);
           markerOptions.title("someTitle");
           markerOptions.snippet("SomeSnippet");
           markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.xx);
           markerOptions..perspective(true).draggable(true);
           Marker aMarker= aMap.addMarker(markerOptions);
            //Marker是由aMap的addMarker時返回的,addMarker的參數是MarkerOptions,markerOptions可以設置position是指定位置,title和snippet是infowindow或infocontent中可以用到的title和snippet屬性,icon一般是由BitmapDescriptorFactory.fromXXX系列工廠方法或BitmapDescriptorFactory.defaultMarker()方法獲得,可以從view生成,但是view中的點擊事件應該是不能執行,只能是地圖點擊事件,perspective是遠小近大效果(2.1.0版本新增),draggable是是否可移動

        aMarker.showInfoWindow();// 主動顯示marker的infowinfow
     
(3)aMap.clear();//清除地圖上的所有Marker
發佈了44 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章