安卓开发实例:高德地图

想要在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:

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

4.2 在AndroidManifest.xml中配置权限:

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文件中添加地图控件:

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

AndroidManifest.xml文件

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文件是这样写的

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 复制代码
<?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文件

依赖要写对,依赖写不对也是白费

groovy 复制代码
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'
}

然后地图就显示在模拟器上面了啊

相关推荐
踏雪羽翼1 小时前
android TextView实现文字字符不同方向显示
android·自定义view·textview方向·文字方向·textview文字显示方向·文字旋转·textview文字旋转
lxysbly1 小时前
安卓玩MRP冒泡游戏:模拟器下载与使用方法
android·游戏
夏沫琅琊3 小时前
Android 各类日志全面解析(含特点、分析方法、实战案例)
android
程序员JerrySUN4 小时前
OP-TEE + YOLOv8:从“加密权重”到“内存中解密并推理”的完整实战记录
android·java·开发语言·redis·yolo·架构
TeleostNaCl5 小时前
Android | 启用 TextView 跑马灯效果的方法
android·经验分享·android runtime
TheNextByte16 小时前
Android USB文件传输无法使用?5种解决方法
android
quanyechacsdn7 小时前
Android Studio创建库文件用jitpack构建后使用implementation方式引用
android·ide·kotlin·android studio·implementation·android 库文件·使用jitpack
程序员陆业聪8 小时前
聊聊2026年Android开发会是什么样
android
编程大师哥8 小时前
Android分层
android
极客小云9 小时前
【深入理解 Android 中的 build.gradle 文件】
android·安卓·安全架构·安全性测试