Android 百度地图蓝点定位

Android 百度地图sdk
    官方文档 
    http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5 
    http://lbsyun.baidu.com/index.php?title=androidsdk

    1.获取密钥

    http://lbsyun.baidu.com/apiconsole/key 
    创建应用:–> 
    应用名称:BaiduMapTest 
    应用类型:Android SDK



    发布版SHA1:A9:7D:70:70:4C:67:83:71:AC:FD:18:6E:EF:9B:08:FF:CF:07:A6:BD 
    如何获取:AS–>Build–>Generate Signed APK 
    创建签名文件key.jks
    keytool -v -list -keystore key.jks

    SHA1: A9:7D:70:70:4C:67:83:71:AC:FD:18:6E:EF:9B:08:FF:CF:07:A6:BD 
    包名:com.bshui.baidumaptest 
    提交后生成访问应用AK:HSyrKQAG1BktzbYSqtR8ZQHTPwleEGaV
    



    2.开发包&Demo下载

    最新版本为:Android 地图 SDK v4.5.2 
    http://lbsyun.baidu.com/index.php?title=sdk/download&action#selected=mapsdk_basicmap,mapsdk_searchfunction,mapsdk_lbscloudsearch,mapsdk_calculationtool,mapsdk_radar 
    在导入时需要根据包名和SHA1生成一个key在BaiduMapsApiASDemo的AndroidMainfest.xml里添加 
    运行Demo测试地图功能
    
    3.Android Studio工程配置

    1)开发包拷到工程 
        将BaiduLBS_Android.jar放到app/libs目录下 
        右击BaiduLBS_Android.jar Add As Library…加入库 
        同步后build.gradle中会出现 implementation files(‘libs/BaiduLBS_Android.jar’) 
    2)在libs下新建armeabi目录 
    导入:libBaiduMapSDK_base_v4_5_2.so,libBaiduMapSDK_map_v4_5_2.so,liblocSDK7b.so 
    在app的build.gradle中说明so为该libs路径 
        sourceSets { 
            main { 
                jniLibs.srcDir ‘libs’ 
            } 
        }
    4.BaiduMapDemo工程开发

    一:显示地图 
    1)在AndroidManifest中添加开发密钥,所需权限 


    
    
    
    
    
    
    
    
    
    
    
    
    
    

2)添加开发密钥

3)在布局xml文件中添加地图控件

 

4)在应用程序创建时初始化SDK引用的Context全局变量 
SDKInitializer.initialize(getApplicationContext()); 

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
        LocationClient mLocClient;
        MyLocationListenner myListener = new MyLocationListenner();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        //定位初始化
        mLocClient = new LocationClient(getApplicationContext());//传入全局的context
        mLocClient.registerLocationListener(myListener);//注册定位监听函数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();//启动定位sdk
    }
 private void initView() {
        mMapView = (MapView) findViewById(R.id.bmapView);
        //获取地图实例
        mBaiduMap = mMapView.getMap();
        // 开启定位图层,一定不要少了这句,否则对在地图的设置、绘制定位点将无效
        mBaiduMap.setMyLocationEnabled(true);
        View child = mMapView.getChildAt(1);
        if (child != null) {
            child.setVisibility(View.INVISIBLE);
        }
    }

生命周期方法
 

@Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

自定义类MyLocationListenner实现BDLocationListener接口

private class MyLocationListenner implements BDLocationListener{
}

重写方法

  public void onReceiveLocation(BDLocation location) {}
 public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            mCurrentLat = location.getLatitude();
            mCurrentLon = location.getLongitude();
            mCurrentAccracy = location.getRadius();
            Log.i("tag","lat:"+ mCurrentLat +"\n"+
                    "lon:"+ mCurrentLon);
            //构造定位数据
           
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(mCurrentDirection).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
           
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(17.0f);
                                    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }

自定义类继承Application重写onCreate()方法

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(this);
    }
}

最后别忘了在清单文件里配置

android:name=".MyApplication"

你可能感兴趣的:(Android 百度地图蓝点定位)