Hello Amap ---基于高德SDK的导航App

(一)参考高德的api完成Android Studio的配置

高德android studio配置说明文档:
Report

(二)写代码:

Mainfest.xml配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="你自己包名">

    <!-- 地图包、搜索包需要的基础权限 -->

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <!-- 定位包、导航包需要的额外权限(注:基础权限也需要) -->

    <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" />

    <application  android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
        <meta-data  android:name="com.amap.api.V2.7.0.apikey"//这里更改你的sdk版本(我的是V2.7.0 sdkandroid:value="你自己申请的key" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BasicMapActivity"></activity>
    </application>

</manifest>

注意:申请key很容易,看这里:
key申请

MainActivity.java


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button_1 = (Button) findViewById(R.id.myButton);
        button_1.setOnClickListener(new ButtonListener());


    }

    public class ButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            Intent i = new Intent();
            i.setClass(MainActivity.this , BasicMapActivity.class);
            startActivity(i);
        }
    }
}

activity_main.xml

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

BasicMapActivity.java
(参考高德提供的demon )


import android.app.Activity;
import android.os.Bundle;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;

public class BasicMapActivity extends Activity {

    private MapView mapView;
    private AMap    aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_map);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState); //必须重写;

    }

    /** * 初始化AMap对象 */
    private void init() {
        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();
    }

}

activity_basic_map.xml

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

运行效果

Hello Amap ---基于高德SDK的导航App_第1张图片

点击button后

Hello Amap ---基于高德SDK的导航App_第2张图片

你可能感兴趣的:(android,api,sdk,导航,高德)