安卓开发实例:高德地图-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

想要在app里面显示高德地图遇到了很多问题开始想显示百度地图的个人感觉不喜欢百度地图跟高德地图有缘所以就弄个高德地图。
当然你可以直接看开发文档啊慢走不送谢谢。
https://lbs.amap.com/api/android-sdk/summary
示例代码我也下载了也还是不行呢。首先当然不是新建一个页面啊

1.下载并安装 Android Studio

我没用Android Studio我用的是IntelliJ IDEA 2021.3.1
新建一个应用

2.获取高德Key

https://lbs.amap.com/dev/#/
点击进入
控制台首页应用管理我的应用创建应用应用名称随便填应用类型也随便填。
在这里插入图片描述
添加Key
发布版(release)安全码SHA1如何获取
调试版(debug)安全码SHA1如何获取
双击这个方框就出来了呢
在这里插入图片描述
然后就给个Key
在这里插入图片描述

3.下载并安装地图开发包

https://lbs.amap.com/api/android-sdk/download/
我并没有下载主要是下载的几个都不对。具体也不知道是哪里不对反正是报错。
当时想可能是跟版本啥的有关系因为有的库里面没有提示库里找不到Amap这个。
一共有这几种包3D地图包2D地图包轻量地图包导航包定位包。
这几个我都下了一遍但是好像都是有问题运行不起来。

4.修改配置文件AndroidManifest.xml

4.1 在AndroidManifest.xml的application标签中配置Key

    <meta-data
      android:name="com.amap.api.v2.apikey"
      android:value="8464b99ee529f07ddfa00df8c2edee72"/>

4.2 在AndroidManifest.xml中配置权限

<!--允许访问网络必选权限-->
<uses-permission android:name="android.permission.INTERNET" />  

<!--允许获取粗略位置若用GPS实现定位小蓝点功能则必选-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

<!--允许获取设备和运营商信息用于问题排查和网络定位若无gps但仍需实现定位小蓝点功能则此权限必选-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />    

<!--允许获取网络状态用于网络定位若无gps但仍需实现定位小蓝点功能则此权限必选-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    

<!--允许获取wifi网络信息用于网络定位若无gps但仍需实现定位小蓝点功能则此权限必选-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

<!--允许获取wifi状态改变用于网络定位若无gps但仍需实现定位小蓝点功能则此权限必选-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 

<!--允许写入扩展存储用于数据缓存若无此权限则写到私有目录-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<!--允许写设备缓存用于问题排查-->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />  

<!--允许读设备等信息用于问题排查-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

4.3 配置XML布局文件 在布局xml文件中添加地图控件

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

AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" package="com.weijun901.shows">
  <!--允许程序打开网络套接字-->
  <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" />
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Shows">
    <activity
      android:name=".Location"
      android:exported="true"/>
    <activity
      android:name=".Sensor"
      android:exported="true"/>
    <activity
      android:name=".DateTime"
      android:exported="true"/>
    <activity
      android:name=".RandomNumber"
      android:exported="true"/>
    <activity
      android:name=".MainActivity"
      android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <meta-data
      android:name="com.amap.api.v2.apikey"
      android:value="8464b99ee529f07ddfa00df8c2edee72"/>
  </application>
</manifest>

我的Location.java文件是这样写的

package com.weijun901.shows;

import android.app.Activity;
import android.os.Bundle;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;

public class Location extends Activity {

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

    MapView mapView = findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);// 此方法必须重写
    AMap aMap = mapView.getMap();

    aMap.setTrafficEnabled(true);// 显示实时交通状况
    //地图模式可选类型MAP_TYPE_NORMAL,MAP_TYPE_SATELLITE,MAP_TYPE_NIGHT
    aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 卫星地图模式
  }
}

完整的activity_location.xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Location">
  <com.amap.api.maps.MapView
    android:id="@+id/map"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

还有一个重中之重app文件夹下面的build.gradle文件
依赖要写对依赖写不对也是白费

plugins {
  id 'com.android.application'
}

android {
  compileSdk 33

  defaultConfig {
    applicationId "com.weijun901.shows"
    minSdk 24
    targetSdk 33
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    debug {
      applicationIdSuffix ".debug"
      debuggable true
    }
    release {
      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }

  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  implementation 'androidx.appcompat:appcompat:1.4.1'
  implementation 'com.google.android.material:material:1.4.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  implementation 'com.amap.api:map2d:2.9.0'
  implementation 'com.amap.api:3dmap:5.0.0'
  testImplementation 'junit:junit:4.13.2'
  androidTestImplementation 'androidx.test.ext:junit:1.1.5'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

然后地图就显示在模拟器上面了啊
在这里插入图片描述

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: android