I try to implement baidu map in to my app. I set up everything and it can show a static map. But when I try to get current location, my app crash after when I try to call method mLocationClient.start(). I have setup the api keys, ask for permissions, enable service and put the libraries in the correct place. But this crash is still there. And I cant get any crash information from my Logcat. Anyone has the experience to implement baidu map in your android app?
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) {
}
}
Message from Logcat:
2019-01-04 13:37:57.483 25394-25394/com.starkey.android.newlink.debug I/System.out: Before call start
2019-01-04 13:37:59.255 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_BOOLEAN local from register v6 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])
2019-01-04 13:37:59.296 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_BOOLEAN local from register v6 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])
2019-01-04 13:37:59.299 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_INT local from register v9 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])
2019-01-04 13:37:59.305 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_BOOLEAN local from register v12 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])
2019-01-04 13:39:08.749 25394-25394/com.starkey.android.newlink.debug I/System.out: after call start
AFAIK, SDKInitializer.initialize
should use an ApplicationContext
, this
inside Activity
is not enough.
Use
SDKInitializer.initialize(applicationContext)
instead of
SDKInitializer.initialize(this)
Reference: Hello BaiduMap (Chinese)
- The referenced
Context
used for initialize SDK during application creation is a global variable;Warning: Before using any components in SDK, (you) must call
SDKInitializer.initialize(getApplicationContext());
. So ee suggest to put this method inside the initialization method ofApplication
.
please remove .jar & .so file from libs & jinLibs lfolder & add below api in app.gragle
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'
now, fixed SDKInitializer.initialize(applicationContext) crashed issue.
User contributions licensed under CC BY-SA 3.0