尝试使用百度地图获取位置时应用崩溃

时间:2019-01-04 18:17:19

标签: android mobile baidu-map

我尝试在我的应用程序中实现百度地图。我设置了所有内容,它可以显示静态地图。但是,当我尝试获取当前位置时,尝试调用方法mLocationClient.start()后,我的应用程序崩溃。我已经设置了api密钥,请求权限,启用服务并将库放在正确的位置。但是这种崩溃仍然存在。而且我无法从Logcat获取任何崩溃信息。任何人都有在您的Android应用中实现百度地图的经验?

import android.os.Bundle``
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.starkey.android.newlink.R

class BaiduMapDemoActivity : AppCompatActivity() {

    private var mMapView: MapView? = null
    private var findLocation: Button? = null
    lateinit var map: BaiduMap
    private var isFirstLoc = true

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        SDKInitializer.initialize(this)
        SDKInitializer.setCoordType(CoordType.BD09LL)

        setContentView(R.layout.baidu_demo)
        mMapView = findViewById(R.id.bmapView)
        findLocation = findViewById(R.id.addfence) as Button

        map = mMapView!!.map
        map.isMyLocationEnabled = true
        map.isMyLocationEnabled = true

        val mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING
        val mCurrentMarker = 
        BitmapDescriptorFactory.fromResource(R.drawable.ha_icon)
        val accuracyCircleFillColor = 0xAAFFFF88.toInt()
        val accuracyCircleStrokeColor = 0xAA00FF00.toInt()

        map.setMyLocationConfiguration(MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker,
                accuracyCircleFillColor, accuracyCircleStrokeColor))
        val mLocationClient = LocationClient(applicationContext)
        val mylistener = MyLocationListenner()

        mLocationClient.registerLocationListener(mylistener)
        val option = LocationClientOption()
        option.locationMode = 
(LocationClientOption.LocationMode.Hight_Accuracy)
        option.setCoorType("bd09ll")
        option.setScanSpan(1000)
        option.openGps = true
        option.setLocationNotify(true)
        option.setIgnoreKillProcess(false)
        option.SetIgnoreCacheException(false)
        option.setWifiCacheTimeOut(5 * 60 * 1000)
        option.setEnableSimulateGps(false)

        mLocationClient.setLocOption(option)

        (findLocation as Button).setOnClickListener {
            mLocationClient.start()
        }

        var test = mLocationClient.isStarted
        mLocationClient.requestLocation()
    }

    override fun onDestroy() {
        super.onDestroy()
        mMapView!!.onDestroy()
    }

    override fun onResume() {

        super.onResume()
        mMapView!!.onResume()

    }

    override fun onPause() {
        super.onPause()
        mMapView!!.onPause()
    }

    inner class MyLocationListenner : BDAbstractLocationListener() {

        override fun onReceiveLocation(location: BDLocation?) {
            if (location == null || mMapView == null)
                return;
            val locData = MyLocationData.Builder()
                    .accuracy(location.radius)
                    .direction(100.toFloat()).latitude(location.latitude)
                    .longitude(location.longitude).build()
            map.setMyLocationData(locData)
            if (isFirstLoc) {
                isFirstLoc = false
                val ll = LatLng(location.latitude,
                        location.longitude)
                val u = MapStatusUpdateFactory.newLatLng(ll)
                map.animateMapStatus(u)
            }
        }
        fun onReceivePoi(poiLocation: BDLocation) {
        }
    }

Logcat的消息:

  

2019-01-04 13:37:57.483 25394-25394 / com.starkey.android.newlink.debug I / System.out:呼叫开始之前

     

2019-01-04 13:37:59.255 25394-25401 / com.starkey.android.newlink.debug E / zygote64:无法通过void com.android.internal方法从DEX pc 0x0000014f的寄存器v6读取JT_BOOLEAN本地.os.ZygoteInit.main(java.lang.String [])

     

2019-01-04 13:37:59.296 25394-25401 / com.starkey.android.newlink.debug E / zygote64:无法通过void com.android.internal方法从DEX pc 0x0000014f处的寄存器v6读取JT_BOOLEAN本地.os.ZygoteInit.main(java.lang.String [])

     

2019-01-04 13:37:59.299 25394-25401 / com.starkey.android.newlink.debug E / zygote64:无法通过void com.android.internal方法从DEX pc 0x0000014f处的寄存器v9读取JT_INT本地.os.ZygoteInit.main(java.lang.String [])

     

2019-01-04 13:37:59.305 25394-25401 / com.starkey.android.newlink.debug E / zygote64:无法通过void com.android.internal方法从DEX pc 0x0000014f的寄存器v12读取JT_BOOLEAN本地.os.ZygoteInit.main(java.lang.String [])

     

2019-01-04 13:39:08.749 25394-25394 / com.starkey.android.newlink.debug I / System.out:通话开始后

2 个答案:

答案 0 :(得分:0)

AFAIK,SDKInitializer.initialize应该使用ApplicationContext内的thisActivity来代替。

使用

SDKInitializer.initialize(applicationContext)

代替

SDKInitializer.initialize(this)

参考:Hello BaiduMap(中文)

  
      
  1. 在应用程序创建期间用于初始化SDK的引用Context是全局变量;
  2.   
     

警告:在使用SDK中的任何组件之前,(您)必须调用SDKInitializer.initialize(getApplicationContext());。因此,ee建议将此方法放在Application的初始化方法中。

答案 1 :(得分:0)

请从libs和jinLibs lfolder中删除.jar和.so文件,并在app.gragle中添加以下api

implementation 'com.baidu.mapapi:base:3.7.1'
implementation 'com.baidu.mapapi:map:3.7.1'
implementation 'com.baidu.mapapi:search:3.7.1'
implementation 'com.baidu.mapapi:util:3.7.1'

现在,已修复SDKInitializer.initialize(applicationContext)崩溃的问题。