高德地圖的使用

root:build.gradle中添加依賴

apply plugin: 'com.android.application'
   ...
android {
    defaultConfig {
        ndk {
            //設(shè)置支持的SO庫架構(gòu)(開發(fā)者可以根據(jù)需要,選擇一個或多個平臺的so)
            abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "arm64-v8a", "x86_64"
        }
    }
}

dependencies {
   ...
     compile 'com.amap.api:3dmap:latest.integration'      //3D地圖
compile 'com.amap.api:map2d:latest.integration'           //2D地圖
compile 'com.amap.api:navi-3dmap:latest.integration'   //導(dǎo)航
compile 'com.amap.api:search:latest.integration'           //搜索
compile 'com.amap.api:location:latest.integration'         //定位
    }

AndroidManifest

添加權(quán)限
  //地圖SDK(包含其搜索功能)需要的基礎(chǔ)權(quán)限

    <!-- 允許程序打開網(wǎng)絡(luò)套接字 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 允許程序設(shè)置內(nèi)置sd卡的寫權(quán)限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 允許程序獲取網(wǎng)絡(luò)狀態(tài) -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- 允許程序訪問WiFi網(wǎng)絡(luò)信息 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!-- 允許程序讀寫手機(jī)狀態(tài)和身份 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!-- 允許程序訪問CellID或WiFi熱點來獲取粗略的位置 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    //定位包、導(dǎo)航包需要的額外權(quán)限(注:基礎(chǔ)權(quán)限也需要)
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
申請高德 Key 并且配置到項目中
<application
       ...>
        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="申請的key"></meta-data>
     ...
    </application>


申請key時候,需要SHA1
Windows電腦,在運行中輸入cd .android回車
輸入:  keytool -list -v -keystore debug.keystore回車
提示輸入密鑰庫口令:如果沒有設(shè)置過,回車即可
即可獲得到SHA1

地圖界面布局的編寫

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

地圖界面代碼的編寫

簡單的地圖顯示

  MapView mMapView;
    AMap aMap;//初始化地圖控制器對象


onCreat(){
 mMapView.onCreate(savedInstanceState);

        //初始化地圖控制器對象
        if (aMap == null) {
            aMap = mMapView.getMap();

        }

}


 @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity執(zhí)行onDestroy時執(zhí)行mMapView.onDestroy(),銷毀地圖
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity執(zhí)行onResume時執(zhí)行mMapView.onResume (),重新繪制加載地圖
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        //在activity執(zhí)行onPause時執(zhí)行mMapView.onPause (),暫停地圖的繪制
        mMapView.onPause();
    }

 @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity執(zhí)行onSaveInstanceState時執(zhí)行mMapView.onSaveInstanceState (outState),保存地圖當(dāng)前的狀態(tài)
        mMapView.onSaveInstanceState(outState);
    }

定位藍(lán)點的顯示

  //實現(xiàn)定位藍(lán)點
        myLocationStyle = new MyLocationStyle();
        //    myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
        //初始化定位藍(lán)點樣式類myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
        // 連續(xù)定位、且將視角移動到地圖中心點,定位點依照設(shè)備方向旋轉(zhuǎn),并且會跟隨設(shè)備移動。(1秒1次定位)如果不設(shè)置myLocationType,
        // 默認(rèn)也會執(zhí)行此種模式。

        myLocationStyle.interval(2000);
        //設(shè)置連續(xù)定位模式下的定位間隔,只在連續(xù)定位模式下生效,單次定位模式下不會生效。單位為毫秒。

        aMap.setMyLocationStyle(myLocationStyle);//設(shè)置定位藍(lán)點的Style
//aMap.getUiSettings().setMyLocationButtonEnabled(true);設(shè)置默認(rèn)定位按鈕是否顯示,非必需設(shè)置。

        aMap.setMyLocationEnabled(true);// 設(shè)置為true表示啟動顯示定位藍(lán)點,false表示隱藏定位藍(lán)點并不進(jìn)行定位,默認(rèn)是false。

        // myLocationStyle.showMyLocation(true);

        //獲取經(jīng)緯度

定位 獲取經(jīng)緯度


 public AMapLocationClient mLocationClient = null;

//聲明mLocationOption對象
    public AMapLocationClientOption mLocationOption = null;

 //初始化定位參數(shù)
        mLocationClient = new AMapLocationClient(this);
        mLocationOption = new AMapLocationClientOption();


        //設(shè)置定位模式為高精度模式,Battery_Saving為低功耗模式,Device_Sensors是僅設(shè)備模式

        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

//設(shè)置是否返回地址信息(默認(rèn)返回地址信息)

        mLocationOption.setNeedAddress(true);

//設(shè)置是否只定位一次,默認(rèn)為false

        mLocationOption.setOnceLocation(true);


        if (mLocationOption.isOnceLocationLatest()) {

            mLocationOption.setOnceLocationLatest(true);

//設(shè)置setOnceLocationLatest(boolean b)接口為true,啟動定位時SDK會返回最近3s內(nèi)精度最高的一次定位結(jié)果。

//如果設(shè)置其為true,setOnceLocation(boolean b)接口也會被設(shè)置為true,反之不會。

        }


//設(shè)置是否強(qiáng)制刷新WIFI,默認(rèn)為強(qiáng)制刷新

        mLocationOption.setWifiActiveScan(true);

//設(shè)置是否允許模擬位置,默認(rèn)為false,不允許模擬位置

        mLocationOption.setMockEnable(false);

//設(shè)置定位間隔,單位毫秒,默認(rèn)為2000ms

        mLocationOption.setInterval(2000);

//給定位客戶端對象設(shè)置定位參數(shù)

        mLocationClient.setLocationOption(mLocationOption);

//啟動定位

        mLocationClient.startLocation();


        //設(shè)置定位回調(diào)監(jiān)聽

        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {

                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {

                        aMapLocation.getLatitude();//獲取緯度

                        aMapLocation.getLongitude();//獲取經(jīng)度
                        aMapLocation.getCity();//獲取城市
                        Log.e("====", aMapLocation.getCity());


                        mainTextView.setText("緯度:" + aMapLocation.getLatitude() + "  經(jīng)度:" + aMapLocation.getLongitude() + "  城市:" + aMapLocation.getCity());
                    }
                }
            }
        });

設(shè)置多描點


 LatLng latLng;
    Marker marker;
   latLng = new LatLng(latitude, longitude);//緯度  經(jīng)度
                    marker = aMap.addMarker(new MarkerOptions().position(latLng).title(name).snippet("" + id));
          //title設(shè)置的是點擊描點顯示的名稱
         //snippet設(shè)置的是點擊描點,顯示的簡單介紹

搜索功能


 //1、繼承 InputtipsListener 監(jiān)聽。
        //2、構(gòu)造 InputtipsQuery 對象
        InputtipsQuery inputquery = new InputtipsQuery(輸入的要搜索的內(nèi)容, "城市名稱");
        inputquery.setCityLimit(true);//限制在當(dāng)前城市
        //3、構(gòu)造 Inputtips 對象,并設(shè)置監(jiān)聽。
        Inputtips inputTips = new Inputtips(Main2Activity.this, inputquery);
        inputTips.setInputtipsListener(this);

        //4、調(diào)用 PoiSearch 的 requestInputtipsAsyn() 方法發(fā)送請求。
        inputTips.requestInputtipsAsyn();



 @Override
    public void onGetInputtips(List<Tip> list, int i) {
        ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, list);
        mainLv.setAdapter(listViewAdapter);

    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容