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

白鹤大人泰库拉 2024-06-22 09:45:40

1.实验内容

设计并开发一个地图应用系统。
该实验需提前申请百度API Key,调用接口实现百度地图的定位功能、地图添加覆盖物和显示文本信息。

2.实验过程

2.1申请密钥

在控制台创建应用,需要发布版的sha1,因此需要在as里创建签名文件,并用keytool来获取sha1

2.1.1.创建release版本的签名文件:

先去Build菜单下生成一个正式的.jks签名文件

 

img

 

 

img

 

进行release版本的签名验证:
file>project structure>modules>signing configs

 

img

 

获取sha1:
keytool -list -v -keystore F: \miyao\1. jks

 

 

 

申请key:

 

img

 

 

img

 

2.2配置开发环境

第一步:在工程app/libs目录下放入BaiduLBS_Android.jar包
第二步:工程配置还需要把jar包集成到自己的工程中,放入libs目录下。jar文件右键-选择Add As Library,导入到工程中。

2.3编写代码

androidmanifest,xml

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. xmlns:tools="http://schemas.android.com/tools"

  4. package="com.example.mylocation">

  5.  
  6. <application

  7. android:allowBackup="true"

  8. android:icon="@mipmap/ic_launcher"

  9. android:label="@string/app_name"

  10. android:roundIcon="@mipmap/ic_launcher_round"

  11. android:supportsRtl="true"

  12. android:theme="@style/AppTheme">

  13. <activity android:name=".MainActivity">

  14. <intent-filter>

  15. <action android:name="android.intent.action.MAIN" />

  16.  
  17. <category android:name="android.intent.category.LAUNCHER" />

  18. </intent-filter>

  19. </activity>

  20. <service android:name="com.example.baidu"

  21. android:enabled="true"

  22. android:process=":remote"/>

  23. <meta-data

  24. android:name="com.baidu.lbsapi.API_KEY"

  25. android:value="6lmYlAJwcQUNz2gGwem8VXpq0T7lDGxG"/>

  26. </application>

  27. <!--百度定位所需要权限,前面是LOCATE权限组的2个危险权限-->

  28. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

  29. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

  30. <!--百度定位所需要的普通权限-->

  31. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

  32. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

  33. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

  34. <!--因为程序要与百度云服务交互-->

  35. <uses-permission android:name="android.permission.INTERNET"/>

  36.  
  37. </manifest>

  38.  

activity_main.xml

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. xmlns:app="http://schemas.android.com/apk/res-auto"

  4. xmlns:tools="http://schemas.android.com/tools"

  5. android:layout_width="match_parent"

  6. android:layout_height="match_parent"

  7. tools:context=".MainActivity">

  8.  
  9. <!--百度地图控件-->

  10. <com.baidu.mapapi.map.MapView

  11. android:id="@+id/bmapView"

  12. android:layout_width="fill_parent"

  13. android:layout_height="fill_parent"

  14. android:clickable="true" />

  15. <!--位置文本布局的背景色代码的前2位代码为透明度-->

  16. <LinearLayout

  17. android:layout_width="fill_parent"

  18. android:layout_height="wrap_content"

  19. android:background="#e0000000"

  20. android:orientation="vertical" >

  21. <LinearLayout

  22. android:layout_width="wrap_content"

  23. android:layout_height="wrap_content"

  24. android:layout_marginLeft="12dp"

  25. android:layout_marginTop="20dp"

  26. android:orientation="horizontal" >

  27. <TextView

  28. android:layout_width="wrap_content"

  29. android:layout_height="wrap_content"

  30. android:text="纬度:"

  31. android:textColor="#ffffff"

  32. android:textSize="15dp" />

  33. <TextView

  34. android:id="@+id/tv_Lat"

  35. android:layout_width="wrap_content"

  36. android:layout_height="wrap_content"

  37. android:text=""

  38. android:textColor="#ffffff"

  39. android:textSize="15dp" />

  40. </LinearLayout>

  41. <LinearLayout

  42. android:layout_width="wrap_content"

  43. android:layout_height="wrap_content"

  44. android:layout_marginLeft="12dp"

  45. android:layout_marginTop="10dp"

  46. android:orientation="horizontal" >

  47. <TextView

  48. android:layout_width="wrap_content"

  49. android:layout_height="wrap_content"

  50. android:text="经度:"

  51. android:textColor="#ffffff"

  52. android:textSize="15dp" />

  53. <TextView

  54. android:id="@+id/tv_Lon"

  55. android:layout_width="wrap_content"

  56. android:layout_height="wrap_content"

  57. android:text=""

  58. android:textColor="#ffffff"

  59. android:textSize="15dp" />

  60. </LinearLayout>

  61. <LinearLayout

  62. android:layout_width="wrap_content"

  63. android:layout_height="wrap_content"

  64. android:layout_marginBottom="10dp"

  65. android:layout_marginLeft="12dp"

  66. android:layout_marginTop="10dp"

  67. android:orientation="horizontal" >

  68. <TextView

  69. android:layout_width="wrap_content"

  70. android:layout_height="wrap_content"

  71. android:text="地址:"

  72. android:textColor="#ffffff"

  73. android:textSize="15dp" />

  74.  
  75. <TextView

  76. android:id="@+id/tv_Add"

  77. android:layout_width="wrap_content"

  78. android:layout_height="wrap_content"

  79. android:text=""

  80. android:textColor="#ffffff"

  81. android:textSize="15dp" />

  82. </LinearLayout>

  83. </LinearLayout>

  84.  
  85. </FrameLayout>

  86.  

MainActivity

 
  1. package com.example.baidu;

  2.  
  3. import android.Manifest;

  4. import android.content.pm.PackageManager;

  5. import android.support.annotation.NonNull;

  6. import android.support.v4.app.ActivityCompat;

  7. import android.support.v7.app.AppCompatActivity;

  8. import android.os.Bundle;

  9. import android.widget.TextView;

  10. import android.widget.Toast;

  11.  
  12. import com.baidu.location.BDLocation;

  13. import com.baidu.location.BDLocationListener;

  14. import com.baidu.location.LocationClient;

  15. import com.baidu.location.LocationClientOption;

  16. import com.baidu.mapapi.SDKInitializer;

  17. import com.baidu.mapapi.map.BaiduMap;

  18. import com.baidu.mapapi.map.BitmapDescriptor;

  19. import com.baidu.mapapi.map.BitmapDescriptorFactory;

  20. import com.baidu.mapapi.map.MapStatusUpdate;

  21. import com.baidu.mapapi.map.MapStatusUpdateFactory;

  22. import com.baidu.mapapi.map.MapView;

  23. import com.baidu.mapapi.map.MyLocationConfiguration;

  24. import com.baidu.mapapi.model.LatLng;

  25.  
  26. public class MainActivity extends AppCompatActivity {

  27. LocationClient mLocationClient; //定位客户端

  28. MapView mapView; //Android Widget地图控件

  29. BaiduMap baiduMap;

  30. boolean isFirstLocate = true;

  31.  
  32. TextView tv_Lat; //纬度

  33. TextView tv_Lon; //经度

  34. TextView tv_Add; //地址

  35.  
  36. @Override

  37. protected void onCreate(Bundle savedInstanceState) {

  38. super.onCreate(savedInstanceState);

  39. //如果没有定位权限,动态请求用户允许使用该权限

  40. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

  41. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

  42. }else {

  43. requestLocation();

  44. }

  45. }

  46. @Override

  47. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

  48. switch (requestCode) {

  49. case 1:

  50. if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {

  51. Toast.makeText(this, "没有定位权限!", Toast.LENGTH_LONG).show();

  52. finish();

  53. } else {

  54. requestLocation();

  55. }

  56. }

  57. }

  58. private void requestLocation() {

  59. initLocation();

  60. mLocationClient.start();

  61. }

  62. private void initLocation() { //初始化

  63. mLocationClient = new LocationClient(getApplicationContext());

  64. mLocationClient.registerLocationListener(new MyLocationListener());

  65. SDKInitializer.initialize(getApplicationContext());

  66. setContentView(R.layout.activity_main);

  67.  
  68. mapView = findViewById(R.id.bmapView);

  69. baiduMap = mapView.getMap();

  70. tv_Lat = findViewById(R.id.tv_Lat);

  71. tv_Lon = findViewById(R.id.tv_Lon);

  72. tv_Add = findViewById(R.id.tv_Add);

  73.  
  74. LocationClientOption option = new LocationClientOption();

  75. //设置扫描时间间隔

  76. option.setScanSpan(1000);

  77. //设置定位模式,三选一

  78. option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);

  79. /*option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);

  80. option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);*/

  81. //设置需要地址信息

  82. option.setIsNeedAddress(true);

  83. //保存定位参数

  84. mLocationClient.setLocOption(option);

  85. }

  86. //内部类,百度位置监听器

  87. private class MyLocationListener implements BDLocationListener {

  88. @Override

  89. public void onReceiveLocation(BDLocation bdLocation) {

  90. tv_Lat.setText(bdLocation.getLatitude()+"");

  91. tv_Lon.setText(bdLocation.getLongitude()+"");

  92. tv_Add.setText(bdLocation.getAddrStr());

  93. if(bdLocation.getLocType()==BDLocation.TypeGpsLocation || bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){

  94. navigateTo(bdLocation);

  95. }

  96. }

  97. }

  98. private void navigateTo(BDLocation bdLocation) {

  99. if(isFirstLocate){

  100. LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());

  101. MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);

  102. baiduMap.animateMapStatus(update);

  103. isFirstLocate = false;

  104. }

  105. }

  106. @Override

  107. protected void onResume() {

  108. super.onResume();

  109. mapView.onResume();

  110. }

  111. @Override

  112. protected void onPause() {

  113. super.onPause();

  114. mapView=(MapView)findViewById(R.id.bmapView);

  115. mapView.onResume();

  116. }

  117. @Override

  118. protected void onDestroy() {

  119. super.onDestroy();

  120. mLocationClient.stop();

  121. mapView.onDestroy();

  122. }

  123.  

 

3.运行结果

 

4.实验总结

本次实验在老师给出相关代码的前提下,难度不高,参与这次地图应用系统的设计与开发实验,我深感理论与实践相结合的重要性。这次实验不仅锻炼了我的编程能力,也提升了我对API调用、接口整合和实际应用开发的理解。并且使我对移动平台开发这门课程的理解更加深刻。

 

 

 

...全文
220 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

102

社区成员

发帖
与我相关
我的任务
社区描述
实验报告
android 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧