Android Studio实现百度地图定位(显示经纬度和地址)

Android Studio实现百度地图定位(显示经纬度和地址)

一、准备工作

1、学会查看官方文档 baiduMap

首先登陆自己的百度账号

依次点击导航栏中–>开发文档–>Android地图SDK–>左侧栏获取密钥–>页面跳转至控制台我的应用,点击创建应用

创建应用中选择填入项目名称、应用类型选择Android SDK、填写SHA1(点击下方的如何获取,官方文档SHA1有说明)、填写包名(同SHA1点击如何获取Packagename

2、关于SHA1可能遇到的问题:
  1. 打开Android Studio,进入Terminal工具(View–>Tool Windows–>Terminal)

  2. 按照官方文档输入命令行:keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey一般可能会产生错误,官方文档标明了选取合适的路径,因此我们首先得转换到jdk所在的路径下(查看路径File–>Project Structure–>SDK location–>JDK location)但是你会发现keytool在bin目录下,所以下一步转到JDK目录的bin目录下。

  3. 但是发现依然报错,原因是密钥文件不存在,那我们就自己去找debug.keystore。(找debug.keystore:Build–>Generate Sign Bundle/APK–>choose existing–>不选择默认,选择AS安装路径一般是C盘,以我的为例找到用户.android目录下,C:\Users\17830.android\debug.keystore,此目录下没有找到可以去SDK的.android目录下的debug.keystore、adbkey、adbkey.pub拷到.android目录下)找到debug.keystore之后可以在命令行输入keytool -list -v -keystore C:\Users\17830.android\debug.keystore,输入默认密码android,现在就可以成功了,将SHA1填入创建应用里。

3、Android studio的配置:
  1. 在官方文档的开发指南里选择Android studio配置,点击下载开发包,如果要实现定位除了本身的默认下载的以外还要勾选全量定位,点击开发包下载,解压缩后将libs里面全部文件复制到as里面的libs目录下(即project模式下app里面的libs)切回Android模式下就多了一个jniLibs文件夹。进入File–>Project Structure–>dependencies–>app–>±->Jar Dependency–>libs\BaiduLBS_Android.jar。

  2. 在build.gradle里面添加对应语句,在AndroidManifest.xml中修改配置内容,添加权限。

  3. .xml文件

<?xml version='1.0' encoding='utf-8'?><FrameLayout 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' tools:context='.MainActivity'> <!--百度地图控件--> <com.baidu.mapapi.map.MapView android:id='@+id/bmapView' android:layout_width='fill_parent' android:layout_height='fill_parent' android:clickable='true' /> <!--位置文本布局的背景色代码的前2位代码为透明度--> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:background='#e0000000' 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></FrameLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  1. MainActivity.java
import android.Manifest;import android.content.pm.PackageManager;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.MapStatusUpdate;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.model.LatLng;/*    百度地图应用,包含定位信息和地图显示    一般需要打开定位服务,选择高精度定位模式,有网络连接    需要在清单文件里使用百度云服务(参见清单文件service标签)    需要创建应用(模块)的Key,并写入清单文件(参见清单文件meta标签)*/public class MainActivity extends AppCompatActivity {    LocationClient mLocationClient;  //定位客户端    MapView mapView;  //Android Widget地图控件    BaiduMap baiduMap;    boolean isFirstLocate = true;    TextView tv_Lat;  //纬度    TextView tv_Lon;  //经度    TextView tv_Add;  //地址    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //如果没有定位权限,动态请求用户允许使用该权限        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);        }else {            requestLocation();        }    }    @Override    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {        switch (requestCode) {            case 1:                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {                    Toast.makeText(this, '没有定位权限!', Toast.LENGTH_LONG).show();                    finish();                } else {                    requestLocation();                }        }    }    private void requestLocation() {        initLocation();        mLocationClient.start();    }    private void initLocation() {  //初始化        mLocationClient = new LocationClient(getApplicationContext());        mLocationClient.registerLocationListener(new MyLocationListener());        SDKInitializer.initialize(getApplicationContext());        setContentView(R.layout.activity_main);        mapView = findViewById(R.id.bmapView);        baiduMap = mapView.getMap();        tv_Lat = findViewById(R.id.tv_Lat);        tv_Lon = findViewById(R.id.tv_Lon);        tv_Add = findViewById(R.id.tv_Add);        LocationClientOption option = new LocationClientOption();        //设置扫描时间间隔        option.setScanSpan(1000);        //设置定位模式,三选一        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);        /*option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);        option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);*/        //设置需要地址信息        option.setIsNeedAddress(true);        //保存定位参数        mLocationClient.setLocOption(option);    }    //内部类,百度位置监听器    private class MyLocationListener  implements BDLocationListener {        @Override        public void onReceiveLocation(BDLocation bdLocation) {            tv_Lat.setText(bdLocation.getLatitude()+'');            tv_Lon.setText(bdLocation.getLongitude()+'');            tv_Add.setText(bdLocation.getAddrStr());            if(bdLocation.getLocType()==BDLocation.TypeGpsLocation || bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){                navigateTo(bdLocation);            }        }    }    private void navigateTo(BDLocation bdLocation) {        if(isFirstLocate){            LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);            baiduMap.animateMapStatus(update);            isFirstLocate = false;        }    }    @Override    protected void onResume() {        super.onResume();        mapView.onResume();    }    @Override    protected void onPause() {        super.onPause();        mapView=(MapView)findViewById(R.id.bmapView);        mapView.onResume();    }    @Override    protected void onDestroy() {        super.onDestroy();        mLocationClient.stop();        mapView.onDestroy();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  1. 项目运行结果:

  2. 此项目在真机上运行结果如上,但是在我的模拟器上的想过定位不准实现真机运行的方法:手机打开设置,然后找到系统点击关于手机,多点几次版本号,这时候会提示你进入开发者模式,之后再系统中就可以看到开发者选项,将手机连接到电脑上,选择文件传输,打开开发者选项中的USB调试,之后再as中就可以看到自己的手机了,点击运行即可。

项目源码:github

(0)

相关推荐