博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android之使用百度地图(1)
阅读量:6079 次
发布时间:2019-06-20

本文共 6616 字,大约阅读时间需要 22 分钟。

实现功能:

定位自己的位置

Activity_main.xml

1 
2
6 7
11 12

AndroidManifest.xml

 

 

 MainActivity.java

1 package acticitytest.exapmle.com.lbstest;  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.v4.content.ContextCompat;  8 import android.support.v7.app.AppCompatActivity;  9 import android.os.Bundle; 10 import android.widget.TextView; 11 import android.widget.Toast; 12  13 import com.baidu.location.BDLocation; 14 import com.baidu.location.BDLocationListener; 15 import com.baidu.location.LocationClient; 16 import com.baidu.location.LocationClientOption; 17  18 import java.util.ArrayList; 19 import java.util.List; 20  21 public class MainActivity extends AppCompatActivity { 22     public LocationClient mLocationClient; 23     private TextView postionText; 24  25     @Override 26     protected void onCreate(Bundle savedInstanceState) { 27         super.onCreate(savedInstanceState); 28         setContentView(R.layout.activity_main); 29         mLocationClient=new LocationClient(getApplicationContext()); 30         mLocationClient.registerLocationListener(new MyLocationListener()); 31         setContentView(R.layout.activity_main); 32         postionText=(TextView)findViewById(R.id.tv_positionTextView); 33         List
permissionList=new ArrayList<>(); 34 if(ContextCompat.checkSelfPermission(MainActivity.this, 35 Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){ 36 permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); 37 } 38 if(ContextCompat.checkSelfPermission(MainActivity.this, 39 Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){ 40 permissionList.add(Manifest.permission.READ_PHONE_STATE); 41 } 42 if(ContextCompat.checkSelfPermission(MainActivity.this, 43 Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){ 44 permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); 45 } 46 if(!permissionList.isEmpty()){ 47 String [] permissions=permissionList.toArray(new String[permissionList.size()]); 48 ActivityCompat.requestPermissions(MainActivity.this,permissions,1); 49 }else{ 50 requestLocation(); 51 } 52 } 53 54 public void requestLocation(){ 55 initLocation(); 56 mLocationClient.start();//开始定位 57 } 58 /**用于更新位置信息*/ 59 private void initLocation(){ 60 LocationClientOption option=new LocationClientOption(); 61 option.setScanSpan(5000);//每五秒更新当前位置 62 option.setIsNeedAddress(true); 63 mLocationClient.setLocOption(option); 64 } 65 66 @Override 67 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 68 switch (requestCode){ 69 case 1: 70 if(grantResults.length>0){ 71 for (int result :grantResults){ 72 if (result!=PackageManager.PERMISSION_GRANTED){ 73 Toast.makeText(this,"必须同意所有权限才能使用本程序",Toast.LENGTH_SHORT).show(); 74 finish(); 75 return; 76 } 77 } 78 requestLocation(); 79 }else{ 80 Toast.makeText(this,"未知错误",Toast.LENGTH_SHORT).show(); 81 finish(); 82 } 83 break; 84 default: 85 break; 86 } 87 } 88 89 public class MyLocationListener implements BDLocationListener{ 90 @Override 91 public void onReceiveLocation(BDLocation bdLocation) { 92 StringBuilder currentPostion =new StringBuilder(); 93 currentPostion.append("纬度:").append(bdLocation.getLatitude()).append("\n"); 94 currentPostion.append("经度:").append(bdLocation.getLongitude()).append("\n"); 95 currentPostion.append("国家:").append(bdLocation.getCountry()).append("\n"); 96 currentPostion.append("省份:").append(bdLocation.getProvince()).append("\n"); 97 currentPostion.append("区:").append(bdLocation.getCity()).append("\n"); 98 currentPostion.append("定位方式:"); 99 if (bdLocation.getLocType()==BDLocation.TypeGpsLocation){100 currentPostion.append("GPS");101 }else if(bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){102 currentPostion.append("网络");103 }else{104 currentPostion.append("未识别");105 }106 postionText.setText(currentPostion);107 }108 }109 110 @Override111 protected void onDestroy() {112 super.onDestroy();113 mLocationClient.stop();//不断更新时一定要注意,在活动销毁时关闭,否则会一直耗电114 }115 }

 依赖:bulid.gradle

apply plugin: 'com.android.application'android {    compileSdkVersion 26    defaultConfig {        applicationId "acticitytest.exapmle.com.lbstest"        minSdkVersion 15        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    sourceSets{        main(){            jniLibs.srcDirs=['libs']        }    }}dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation 'com.android.support:appcompat-v7:26.1.0'    implementation 'com.android.support.constraint:constraint-layout:1.1.0'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.2'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'    compile files('libs/BaiduLBS_Android.jar')}

 

转载于:https://www.cnblogs.com/archer-lcy/p/8947676.html

你可能感兴趣的文章
07-k8s-dns
查看>>
Android 中 ListView 分页加载数据
查看>>
oracle启动报错:ORA-00845: MEMORY_TARGET not supported on this system
查看>>
Go方法
查看>>
Dapper丶DapperExtention,以及AbpDapper之间的关系,
查看>>
搞IT的同学们,你们在哪个等级__那些年发过的帖子
查看>>
且谈语音搜索
查看>>
MySQL数据库导入导出常用命令
查看>>
低版本Samba无法挂载
查看>>
Telegraf+Influxdb+Grafana构建监控平台
查看>>
使用excel 展现数据库内容
查看>>
C#方法拓展
查看>>
MySql.Data.dll的版本
查看>>
Linux系统磁盘管理
查看>>
hdu 2191 (多重背包+二进制优化)
查看>>
home.php
查看>>
neo4j---删除关系和节点
查看>>
redis分布式锁redisson
查看>>
什么样的企业可以称之为初创企业?
查看>>
Python爬虫之BeautifulSoup
查看>>