Uniapp 離線打包 添加高德SDK 實現uni.getLocation(OBJECT)


本地安卓項目配置

官網教程
*如果配置好編譯報錯:weex_amap-release.aar 如沒有請忽略參考解決網址
如果需本地離線打包項目教程=》離線打報教程

獲取本地jks SHA1高德申請key會用到

如有請忽略 生成本地jks文件
**記得將證書信息截圖后面生成會用到**

獲取已有證書SHA1

在存放證書目錄打開cmd

輸入: keytool -v -list -storetype jks -keystore XXX.jkskeytool -v -list -keystore keystore XXX.jkskeytool -list -v -keystore keystore XXX.jks

調試SHA1

Win+R打開新的cmd輸入:*默認密碼為:android

cd .android 
keytool -list -v -keystore debug.keystore
詳細說明請參考 高德教程

高德地圖申請key

官方教程

uniapp 配置高德key

申請高德或者百度的key,在manifest.json --> App SDK中勾選地圖和定位服務,在App模塊權限配置中勾選Maps,這樣我們可以獲取更多的位置服務權限。
參考來源

uniapp編碼部分

//定位權限
export const openPosition = ()=>{
    let system = uni.getSystemInfoSync()
    if(system.platform === 'android'){//判斷平台
        var context = plus.android.importClass("android.content.Context")
        var locationManager = plus.android.importClass("android.location.LocationManager")
        var main = plus.android.runtimeMainActivity()
        var mainSvr = main.getSystemService(context.LOCATION_SERVICE)
        console.log('GPS',mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER))
        if(!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)){
            uni.showModal({
                title:'提示',
                content:'請打開定位服務功能',
                showCancel:false,
                success() {
                    if(!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)){
                        let main = plus.android.runtimeMainActivity();
                        let Intent = plus.android.importClass("android.content.Intent");
                        let mIntent = new Intent('android.settings.ACTION_SETTINGS');
                        main.startActivity(mIntent);//打開系統設置GPS服務頁面 
                    }else{
                        uni.showToast({
                            title:'定位功能已啟動',
                            duration:2000
                        })
                    }
                }
            })
        }
    }
}
//獲取當前的地理位置坐標
getCurLocation(){
               uni.getLocation({
                   geocode:true,
                   type: 'gcj02',
                   success:async (res)=>{
                       let { pois } = await posToCity(
                                   {
                                     latitude: res.latitude,
                                     longitude: res.longitude,
                                   },
                                   1
                                 );
                       this.address = pois[0].title
                       this.latitude = res.latitude
                       this.longitude = res.longitude
                       uni.setStorageSync('latitude',res.latitude)
                       uni.setStorageSync('longitude',res.longitude)
                       this.curAddress = store.state.address.curPickAddress.title
                   }
               })
           }
// 高德坐標解析
export const posToCity = async (location, extensions = 0) => new Promise((resolve, reject) => {
  uni.request({
    url: `https://restapi.amap.com/v3/geocode/regeo`,
    method: 'GET',
    data: {
      key: gdMapKey,
      location: `${location.longitude},${location.latitude}`,
      extensions: extensions ? 'all' : 'base'
    },
    success: ({data}) => {
      const {status, info, regeocode} = data
      if (status === '1') {
        // console.log(regeocode)
        if ('pois' in regeocode) {
          regeocode.pois = regeocode.pois.filter(item => typeof item.address === 'string' && typeof item.location === 'string').map(item => ({
            id: item.id,
            title: item.name,
            address: item.address,
            location: {
              lat: item.location.split(',')[1],
              lng: item.location.split(',')[0]
            }
          }))
        }
        resolve(regeocode)
      } else {
        reject(info)
      }
    },
    fail: err => {
      reject(err)
    }
  })
})
來源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM