1. 程式人生 > >(一)高德地圖之基本屬性以及顯示模式

(一)高德地圖之基本屬性以及顯示模式

       我們在開發中常常會用到地圖功能,那麼我們最常用的就是百度地圖和高德地圖了,今天我們就來看一下高德地圖怎麼用,我個人認為高德地圖封裝的還是比較好的,使用起來方便快捷。

首先我們需要到高德開發者平臺註冊賬號,根據API進行操作即可,高德開放平臺連結我放在下面:

https://lbs.amap.com/api/android-sdk/summary/

下面主要介紹我個人開發使習慣用的步驟,僅供參考

一、首先是開發前的準備工作,配置工程

1、在高德開放平臺下載SDK;

2、新增jar檔案:將下載的SDK的jar包複製到工程的libs目錄下;

3、新增so庫:在main目錄下建立資料夾jniLibs(如果有就不需要建立了),將下載的so庫複製到這個目錄即可;

4、在Project的build.gradle檔案中配置repositories,新增maven或jcenter倉庫地址

Android Studio預設會在Project的build.gradle為所有module自動新增jcenter的倉庫地址,如果已存在,則不需要重複新增。

配置如下:

allprojects {
    repositories {
        jcenter() // 或者 mavenCentral()
    }
 }

5、為了保證高德 Android SDK 的功能正常使用,您需要申請高德 Key 並且配置到專案中。

專案的 “AndroidManifest.xml” 檔案中,新增如下程式碼:

<application
         android:icon="@drawable/icon"
         android:label="@string/app_name" >
         <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="請輸入您的使用者Key"/>
            ……
</application>

6、配置許可權

在AndroidManifest.xml中配置許可權:

//地圖包、搜尋包需要的基礎許可權
<!--允許程式開啟網路套接字-->
<uses-permission android:name="android.permission.INTERNET" />  
<!--允許程式設定內建sd卡的寫許可權-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
<!--允許程式獲取網路狀態-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<!--允許程式訪問WiFi網路資訊-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<!--允許程式讀寫手機狀態和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />     
<!--允許程式訪問CellID或WiFi熱點來獲取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

二、工程配置完成,接下來我們進入開發階段

1、建立.xml佈局檔案activity_showmap

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn_NORMAL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="標準地圖"/>
        <Button
            android:id="@+id/btn_SATELLITE"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="衛星地圖"/>
        <Button
            android:id="@+id/btn_NIGHT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="夜景地圖"/>
        <Button
            android:id="@+id/btn_NAVI"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="導航地圖"/>
        <Button
            android:id="@+id/btn_BUS"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="車路線地圖"/>
        <CheckBox
            android:id="@+id/cb_traffic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="顯示實時交通狀況"/>
        <CheckBox
            android:id="@+id/cb_building"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顯示3D 樓塊"/>
        <CheckBox
            android:id="@+id/cb_maptext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顯示底圖文字"/>

    </LinearLayout>

</RelativeLayout>

2、建立顯示地圖的類檔案ShowMapactivity

package com.junto.gdmaptest.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.junto.gdmaptest.R;

/**
 * Created by WangJinyong on 2018/10/19.
 */

public class ShowMapActivity extends Activity implements View.OnClickListener {

    MapView mapView = null;
    AMap aMap;
    private UiSettings mUiSettings;
    Button btn_NORMAL, btn_SATELLITE, btn_NIGHT, btn_NAVI, btn_BUS;
    CheckBox cb_traffic,cb_building,cb_maptext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showmap);
        //獲取地圖控制元件引用
        mapView = findViewById(R.id.map);
        //在activity執行onCreat時執行mapView.onCreate(savedInstanceState),建立地圖,必需要寫的
        mapView.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        //初始化AMap物件
        if (aMap == null) {
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
        }
        mUiSettings.setLogoBottomMargin(20);//設定LOGO圖示距離頁面底部的距離
        mUiSettings.setZoomControlsEnabled(true);//設定地圖預設縮放控制元件是否顯示
        mUiSettings.setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_BUTTOM);//設定縮放控制元件位置
        mUiSettings.setScaleControlsEnabled(true);//設定地圖預設的比例尺是否顯示
        mUiSettings.setCompassEnabled(true);//設定地圖預設的指南針是否顯示
        mUiSettings.setTiltGesturesEnabled(true);// 設定地圖是否可以傾斜
        mUiSettings.setMyLocationButtonEnabled(true);//設定地圖預設的定位按鈕是否顯示
        aMap.setMyLocationEnabled(true);//是否可觸發定位並顯示定位層

//        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);// 設定地圖logo顯示在左下方
//        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_CENTER);// 設定地圖logo顯示在底部居中
//        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);// 設定地圖logo顯示在右下方
        btn_NORMAL = findViewById(R.id.btn_NORMAL);
        btn_NORMAL.setOnClickListener(this);
        btn_SATELLITE = findViewById(R.id.btn_SATELLITE);
        btn_SATELLITE.setOnClickListener(this);
        btn_NIGHT = findViewById(R.id.btn_NIGHT);
        btn_NIGHT.setOnClickListener(this);
        btn_NAVI = findViewById(R.id.btn_NAVI);
        btn_NAVI.setOnClickListener(this);
        btn_BUS = findViewById(R.id.btn_BUS);
        btn_BUS.setOnClickListener(this);
        cb_traffic = findViewById(R.id.cb_traffic);
        cb_traffic.setOnClickListener(this);
        cb_building = findViewById(R.id.cb_building);
        cb_building.setOnClickListener(this);
        cb_maptext = findViewById(R.id.cb_maptext);
        cb_maptext.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity執行onResume時執行mMapView.onResume (),重新繪製載入地圖
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity執行onPause時執行mMapView.onPause (),暫停地圖的繪製
        mapView.onPause();
    }

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity執行onSaveInstanceState時執行mMapView.onSaveInstanceState (outState),儲存地圖當前的狀態
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_NORMAL://標準地圖
                aMap.setMapType(AMap.MAP_TYPE_NORMAL);
                break;
            case R.id.btn_SATELLITE://衛星地圖
                aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
                break;
            case R.id.btn_NIGHT://夜景地圖
                aMap.setMapType(AMap.MAP_TYPE_NIGHT);
                break;
            case R.id.btn_NAVI://導航地圖
                aMap.setMapType(AMap.MAP_TYPE_NAVI);
                break;
            case R.id.btn_BUS://車路線地圖
                aMap.setMapType(AMap.MAP_TYPE_BUS);
                break;
            case R.id.cb_traffic://顯示實時交通狀況
                aMap.setTrafficEnabled(((CheckBox) view).isChecked());
                break;
            case R.id.cb_building://顯示3D樓塊
                aMap.showBuildings(((CheckBox) view).isChecked());
                break;
            case R.id.cb_maptext://顯示底圖文字
                aMap.showMapText(((CheckBox) view).isChecked());
                break;
        }
    }
}

在專案中使用地圖的時候需要注意,需要合理的管理地圖生命週期,這非常的重要。

這樣一個基本地圖就完成了。

下一節將介紹高德地圖的手勢互動功能。