20212413 2023-2024-2 《移动平台开发与实践》第5次作业

20212413 2023-2024-2 《移动平台开发与实践》第5次作业

1.实验内容

设计并开发一个地图应用系统。

该实验需提前申请百度API Key,调用接口实现百度地图的定位功能、地图添加覆盖物和显示文本信息。

2.实验过程

2.1 获取SHA1与AK

进入百度地图开发平台,完成个人注册后,进入控制台,点击左栏“应用管理”→“我的应用”,进行创建应用,应用类型选择Android SDK,接着Win+R打开控制台,输入:

cd .android
keytool -list -v -keystore debug.keystore
//密钥库口令是:android

在这里插入图片描述
填入SHA1和包名后点击提交即创建应用成功,获得AK。
在这里插入图片描述

2.2 下载导入SDK

新建项目,java语言,将老师提供的jar文件和so文件放到…\baidumap\app\libs该目录下
在这里插入图片描述
在src/main/目录下新建jniLibs目录,在将对应的so文件文件夹复制到jniLibs目录
在这里插入图片描述
在build.gradle.kts文件android{}中添加如下代码:

sourceSets {
        getByName("main") {
            jniLibs.srcDirs("libs")
        }
    }

在这里插入图片描述
打开AndroidManifest.xml,添加代码(android:value的值是获得的AK):

<service android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote"/>
        <meta-data
            android:name="自己填"
            android:value="自己填"/>

在manifest中添加如下代码:

    <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_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

2.3 开发一个百度地图应用系统

main_activity.kt部分代码:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        LocationClient.setAgreePrivacy(true)
 
        SDKInitializer.setAgreePrivacy(applicationContext, true)
        try {
            // 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
            SDKInitializer.initialize(applicationContext)
        } catch (e: BaiduMapSDKException) {
        }
        SDKInitializer.setCoordType(CoordType.BD09LL)
 
 
        //如果没有定位权限,动态请求用户允许使用该权限
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                1
            )
        } else {
            requestLocation()
        }
 
    }
 
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            1 -> if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "没有定位权限!", Toast.LENGTH_LONG).show()
                finish()
            } else {
                requestLocation()
            }
        }
    }
 
 
 
    private fun requestLocation() {
        initLocation()
        mLocationClient!!.start()
    }
 
    private fun initLocation() {  //初始化
        mLocationClient = LocationClient(applicationContext)
        mLocationClient!!.registerLocationListener(MyLocationListener())
        SDKInitializer.initialize(applicationContext)
        setContentView(R.layout.activity_main)
 
        tv_Lat = findViewById(R.id.tv_Lat)
        tv_Lon = findViewById(R.id.tv_Lon)
        tv_Add = findViewById(R.id.tv_Add)
 
 
        mapView = findViewById<View>(R.id.bmapView) as MapView
        mRgType = findViewById(R.id.rg_type)
        mRbNormal = findViewById(R.id.rb_normal)
        mRbSatellite = findViewById(R.id.rb_satellite)
        mCbtTrafficEnable = findViewById(R.id.cb_trafficEnable)
        mCbHeatMapEnable = findViewById(R.id.cb_heatMapEnable)
 
        mRgType!!.setOnCheckedChangeListener { radioGroup, i ->
            if (i == mRbNormal!!.id) {
                mapView!!.map.mapType = BaiduMap.MAP_TYPE_NORMAL
            } else if (i == mRbSatellite!!.id) {
                mapView!!.map.mapType = BaiduMap.MAP_TYPE_SATELLITE
            }
        }
        mCbtTrafficEnable!!.setOnCheckedChangeListener { compoundButton, b ->
            mapView!!.map.isTrafficEnabled = b
        }
        mCbHeatMapEnable!!.setOnCheckedChangeListener { compoundButton, b ->
            mapView!!.map.isBaiduHeatMapEnabled = b
        }
 
 
        val option = LocationClientOption()
        //设置扫描时间间隔
        option.setScanSpan(1000)
        //设置定位模式,三选一
        option.locationMode = LocationClientOption.LocationMode.Hight_Accuracy
        /*option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
        option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);*/
        //设置需要地址信息
        option.setIsNeedAddress(true)
        //保存定位参数
        mLocationClient!!.locOption = option
    }

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/gradient_color"
        android:orientation="horizontal">
 
        <RadioGroup
            android:id="@+id/rg_type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
 
            <RadioButton
                android:id="@+id/rb_normal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/normal_map" />
 
            <RadioButton
                android:id="@+id/rb_satellite"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/satellite_map" />
        </RadioGroup>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_color">
 
        <CheckBox
            android:id="@+id/cb_trafficEnable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/trafficEnable" />
 
        <CheckBox
            android:id="@+id/cb_heatMapEnable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/heatMapEnable" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
 
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="20dp"
            android:orientation="horizontal" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="纬度:"
                android:textColor="#ffffff"
                android:textSize="15dp" />
            <TextView
                android:id="@+id/tv_Lat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="经度:"
                android:textColor="#ffffff"
                android:textSize="15dp" />
            <TextView
                android:id="@+id/tv_Lon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="地址:"
                android:textColor="#ffffff"
                android:textSize="15dp" />
 
            <TextView
                android:id="@+id/tv_Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout>
 
    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true" />
</LinearLayout>

2.4 运行结果

在这里插入图片描述
在这里插入图片描述

3.学习中遇到的问题及解决

  • 问题1:在模拟器上运行时闪退
  • 问题1解决方案:打开开发者模式,USB连接电脑安装APP。

4.学习感悟、思考等)

在本次移动平台开发与实践的实验中,我成功申请了百度地图API Key,并下载导入了相应的SDK。通过调用百度地图API接口,我实现了地图的定位功能等功能。
通过这次实验,我掌握了如何使用百度地图API Key和SDK的使用方法。我还学会了如何在Android Studio中进行工程配置和程序调试,并且能够使用真机运行开发项目。通过对相关开发教程文档的学习,我收获了很多宝贵的经验。
总的来说,这次实验让我对移动平台开发有了更深入的了解和实践。

参考资料

  • https://lbs.baidu.com/faq/api?title=androidsdk/guide/create-project/androidstudio
  • https://lbs.baidu.com/faq/api?title=androidsdk/guide/create-project/ak
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值