百度地图sdk怎么用

配置文件 

meta里加上key

        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="Tdu6i8odfOHqiQ7Cm8zAV6qdmRQLE76X" />
        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote"></service>

在application里

 SDKInitializer.initialize(this)

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <com.baidu.mapapi.map.MapView
        android:id="@+id/mv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.baidu.mapapi.map.MapView>
</LinearLayout>

代码

package com.rulerbug.qmuidemo.qmuidemo.Fragment

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
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.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.rulerbug.qmuidemo.qmuidemo.Base.BaseFragment
import com.rulerbug.qmuidemo.qmuidemo.R


class DiTuFragment : BaseFragment() {
    var locationClient: LocationClient? = null
    override fun init() {
        locationClient = LocationClient(context!!)
        locationClient!!.registerLocationListener(MyLocationListener())
        requestDingWei()
        setMap()
    }

    private fun setMap() {
        mapView!!.map.isMyLocationEnabled = true
    }

    val REQUEST_CODE = 100
    fun requestDingWei() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // 检查是否有存储和拍照权限
            if (
                    context!!.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) === PackageManager.PERMISSION_GRANTED
                    && context!!.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) === PackageManager.PERMISSION_GRANTED
                    && context!!.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) === PackageManager.PERMISSION_GRANTED
            ) {
                //有权限
                dingwei()
            } else {
                //没有权限,开始申请
                requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA), REQUEST_CODE)
            }
        } else {
            dingwei()
        }
    }

    private fun dingwei() {
        initLocation()
        //开始定位
        locationClient!!.start()
    }

    private fun initLocation() {

        //设置刷新和需要文字形式的地址
        //并且每5秒刷新一次
        val option = LocationClientOption()
        option.setScanSpan(5000)
        option.setIsNeedAddress(true);

        locationClient!!.locOption = option
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_ditu
    }

    var tv: TextView? = null
    var mapView: MapView? = null

    override fun initViews(rootView: View) {
        tv = rootView.findViewById(R.id.tv)
        mapView = rootView.findViewById(R.id.mv)
    }

    inner class MyLocationListener : BDLocationListener {
        override fun onReceiveLocation(location: BDLocation?) {
            //这里得到位置
            val sb: StringBuffer = StringBuffer()
            sb.append("纬度\t" + location!!.latitude)
            sb.append("经度\t" + location!!.longitude)
            sb.append("国家\t" + location!!.country)
            sb.append("\t" + location!!.province)
            sb.append("\t" + location!!.city)
            sb.append("\t" + location!!.district)
            sb.append("\t" + location!!.street)
            tv!!.setText(sb.toString())
            if (location!!.locType == BDLocation.TypeNetWorkLocation || location!!.locType == BDLocation.TypeGpsLocation) {
                navigateTo(location)
            }
        }

    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                dingwei()
            } else {
                Toast.makeText(context, "权限授予失败!", Toast.LENGTH_SHORT).show()
            }
        }
    }

    var isFirst: Boolean = true
    fun navigateTo(location: BDLocation) {
        if (isFirst) {
            //更新地图位置
            val ll = LatLng(location.latitude, location.longitude)
            var update = MapStatusUpdateFactory.newLatLng(ll)
            mapView!!.map.animateMapStatus(update)
            update = MapStatusUpdateFactory.zoomTo(12.5f)
            mapView!!.map.animateMapStatus(update)
            isFirst = false
        }
        //设置自己在地图上的显示
        val builder = MyLocationData.Builder()
        builder.latitude(location.latitude)
        builder.longitude(location.longitude)
        val build = builder.build()
        mapView!!.map.setMyLocationData(build)
    }

    override fun onDestroy() {
        super.onDestroy()
        locationClient!!.stop()
        mapView!!.onDestroy()
        mapView!!.map.isMyLocationEnabled = false
    }

    override fun onResume() {
        super.onResume()
        mapView!!.onResume()
    }

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值