生活随笔
收集整理的這篇文章主要介紹了
Android实现百度地图定位服务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
百度地圖定位 下載定位包 AndroidManifest.xml添加與修改 activity_main.xml代碼 java代碼 連接真機 結(jié)果展示 gitee代碼
本文主要講述如何使用Android studio實現(xiàn)定位和地圖功能,通過GPS,獲取終端的地理坐標,電子地圖,位置查詢。包括定位包的下載和使用,java,xml代碼實現(xiàn)。
下載定位包
訪問http://lbsyun.baidu.com,選擇:開發(fā)文檔->Android 地圖SDK->產(chǎn)品下載->自定義下載,勾選百度定位包(選擇全景定位和基礎(chǔ)地圖)后下載。 下載JAR包并且對其進行解壓 選擇模塊視圖為Project,復(fù)制定位包BaiduLBS_Android.jar至程序map的libs文件夾里,然后右鍵jar文件,選擇“Add As Library” 在main文件夾下新建名為jniLibs的文件夾,復(fù)制存放.so文件(share object)的多個文件夾至jniLibs文件夾
創(chuàng)建百度位置應(yīng)用,需進入百度開發(fā)者注冊和登錄http://developer.baidu.com
使用百度帳號成功登錄后,進入控制臺,http://lbsyun.baidu.com/apiconsole/key創(chuàng)建應(yīng)用。需要輸入本機Android指紋碼SHA1和應(yīng)用的包名。
如何獲得本機Android指紋碼SHA1? 輸入cmd ,進入命令輸入,進入用戶,輸入命令 cd .android,進入找到.android文件。 輸入命令 keytool -list -v -keystore debug.keystore,輸入默認密鑰庫口令:android后,回車,獲取到開發(fā)版的SHA1值
如何獲得包名? 點擊程序中src的build.gradle,查看applicationId,即為包名。 提交后,生成應(yīng)用的AK,查看應(yīng)用Key并復(fù)制,以供清單文件配置應(yīng)用Key用。
AndroidManifest.xml添加與修改
在清單文件即 AndroidManifest.xml 中,增加如下權(quán)限:
< 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" />
在Activity組件注冊的代碼后,添加注冊遠程服務(wù)和配置應(yīng)用Key的代碼: 每個APP擁有單獨的定位服務(wù),所以在使用百度定位和地圖服務(wù)前,需聲明service服務(wù)組件并登記
< service android: name= " com.baidu.location.f" android: enabled= " true" android: process= " :remote" />
< meta-dataandroid: name= " com.baidu.lbsapi.API_KEY" android: value= " DQGuGACl3uQV1zq4GzuHHxxyYldeIMTl" />
activity_main.xml代碼
界面布局使用幀布局,包含有重疊效果的地圖和位置文本,主要內(nèi)容含有經(jīng)度,緯度,地址,以及定位后的地圖顯示 activity_main.xml的代碼:
< FrameLayout xmlns: android= " http://schemas.android.com/apk/res/android" android: layout_width= " match_parent" android: layout_height= " match_parent" > < com.baidu.mapapi.map.MapViewandroid: id= " @+id/bmapView" android: layout_width= " fill_parent" android: layout_height= " fill_parent" android: clickable= " true" /> < LinearLayoutandroid: layout_width= " fill_parent" android: layout_height= " wrap_content" android: background= " #e0000000" android: orientation= " vertical" > < LinearLayoutandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: layout_marginLeft= " 12dp" android: layout_marginTop= " 20dp" android: orientation= " horizontal" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 緯度:" android: textColor= " #ffffff" android: textSize= " 15dp" /> < TextViewandroid: id= " @+id/tv_Lat" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " " android: textColor= " #ffffff" android: textSize= " 15dp" /> </ LinearLayout> < LinearLayoutandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: layout_marginLeft= " 12dp" android: layout_marginTop= " 10dp" android: orientation= " horizontal" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 經(jīng)度:" android: textColor= " #ffffff" android: textSize= " 15dp" /> < TextViewandroid: id= " @+id/tv_Lon" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " " android: textColor= " #ffffff" android: textSize= " 15dp" /> </ LinearLayout> < LinearLayoutandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: layout_marginBottom= " 10dp" android: layout_marginLeft= " 12dp" android: layout_marginTop= " 10dp" android: orientation= " horizontal" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 地址:" android: textColor= " #ffffff" android: textSize= " 15dp" /> < TextViewandroid: 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>
java代碼
包括請求使用定位權(quán)限,獲取地址信息,定位參數(shù),監(jiān)聽位置 界面程序MainActivity.java代碼:
package com. example. map ; import androidx. annotation. NonNull ;
import androidx. appcompat. app. AppCompatActivity ;
import androidx. core. app. ActivityCompat ; import android. Manifest ;
import android. content. pm. PackageManager ;
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 ; public class MainActivity extends AppCompatActivity { LocationClient mLocationClient
; MapView mapView
; 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 , "沒有定位權(quán)限!" , 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
. 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
. onResume ( ) ; } @Override protected void onDestroy ( ) { super . onDestroy ( ) ; mLocationClient
. stop ( ) ; mapView
. onDestroy ( ) ; }
}
連接真機
以華為手機為例: 手機連usb到電腦,手機選擇“傳輸文件”,打開“設(shè)置”->系統(tǒng)與更新->開發(fā)人員選項->打開USB調(diào)試
錯誤:INSTALL_FAILED_ABORTED: User rejected permissions 解決如下:關(guān)閉監(jiān)控ADB安裝應(yīng)用 android studio如果無法識別到USB device,需要打開tools->SDK Manager->SDK Tools 中安裝Google USB Driver
as中左上角出現(xiàn)手機型號,則表示軟件與手機連接成功
結(jié)果展示
連接真機,進行測試
gitee代碼
gitee代碼
總結(jié)
以上是生活随笔 為你收集整理的Android实现百度地图定位服务 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。