百度地图SDK显示与定位

总结:

第一次使用百度地图SDK进行地图的开发,遇到了很多的问题,在此做一下记录。

  • 按照百度地图官网的步骤注册并获取开发密钥AK
  • 在AndroidStudio导入应用所需的jar包和so包
  • 注意定位功能的实现时如果需要显示addr的信息,那么需要设置 option.setIsNeedAddress( true );
  • 在真机上测试时要注意打开手机上的定位权限
    在定位测试过程中如果出错,可以根据相应的error code来排错, 以下是常用的error code:
61 : GPS定位结果,GPS定位成功。
62 : 无法获取有效定位依据,定位失败,请检查运营商网络或者wifi网络是否正常开启,尝试重新请求定位。
63 : 网络异常,没有成功向服务器发起请求,请确认当前测试手机网络是否通畅,尝试重新请求定位。
65 : 定位缓存的结果。
66 : 离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果。
67 : 离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果。
68 : 网络连接失败时,查找本地离线定位时对应的返回结果。
161: 网络定位结果,网络定位定位成功。
162: 请求串密文解析失败,一般是由于客户端SO文件加载失败造成,请严格参照开发指南或demo开发,放入对应SO文件。
167: 服务端定位失败,请您检查是否禁用获取位置信息权限,尝试重新请求定位。
502: key参数错误,请按照说明文档重新申请KEY。
505: key不存在或者非法,请按照说明文档重新申请KEY。
601: key服务被开发者自己禁用,请按照说明文档重新申请KEY。
602: key mcode不匹配,您的ak配置过程中安全码设置有问题,请确保:sha1正确,“;”分号是英文状态;且包名是您当前运行应用的包名,请按照说明文档重新申请KEY。
501~700:key验证失败,请按照说明文档重新申请KEY。

1.布局文件:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_change"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="切换" />

        <Button
            android:id="@+id/btn_traffic"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="打开" />

        <Button
            android:id="@+id/btn_opt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="标注" />

        <Button
            android:id="@+id/btn_loc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="定位" />
    </LinearLayout>

    <TextView
        android:id="@+id/locmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

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

2.MainActivity.java:

public class MainActivity extends Activity {
    private MapView mMapView = null;
    private BaiduMap mBaiduMap;
    private Button btn_change;
    private Button btn_btn_traffic;
    private Button btn_opt;
    private Button btn_loc;
    private TextView mTextView = null;
    private boolean isFirstLoc = true;  //是否首次定位
    private LocationClient mLocationClient = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        //地图对象
        mBaiduMap = mMapView.getMap();

        //定位初始化
        mLocationClient = new LocationClient(getApplicationContext());
        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setIsNeedAddress( true );
        option.setWifiCacheTimeOut( 5*60*1000 );
        option.setLocationMode( LocationClientOption.LocationMode.Hight_Accuracy );

        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);


        initView();

        //按钮的监听器
        btn_change.setOnClickListener( clickListener );
        btn_btn_traffic.setOnClickListener( clickListener );
        btn_opt.setOnClickListener( clickListener );
        btn_loc.setOnClickListener( clickListener );
    }

    private void initView() {
        btn_change = findViewById( R.id.btn_change );
        btn_btn_traffic = findViewById( R.id.btn_traffic );
        btn_opt = findViewById( R.id.btn_opt );
        btn_loc = findViewById( R.id.btn_loc );
        mTextView = findViewById( R.id.locmsg );
    }

    public View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.btn_change){
                if(mBaiduMap.getMapType() == BaiduMap.MAP_TYPE_NORMAL)
                    mBaiduMap.setMapType( BaiduMap.MAP_TYPE_SATELLITE );
                else
                    mBaiduMap.setMapType( BaiduMap.MAP_TYPE_NORMAL );
            }else if(v.getId() == R.id.btn_traffic){
                if(!mBaiduMap.isTrafficEnabled())
                    mBaiduMap.setTrafficEnabled( true );
                else
                    mBaiduMap.setTrafficEnabled( false );
            }else if(v.getId() == R.id.btn_opt){
                addOpt();
            }
            else if(v.getId() == R.id.btn_loc){
                //开启地图定位图层
                mLocationClient.start();
                mTextView.setVisibility( View.VISIBLE );
            }
        }
    };

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }

            mBaiduMap.setMyLocationEnabled(true);

            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            //判断是否是第一次定位
            if(isFirstLoc){
                isFirstLoc = false;
                //将地图移动到定位的位置
                float f = mBaiduMap.getMaxZoomLevel();
                LatLng ll = new LatLng( location.getLatitude(),location.getLongitude() );
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom( ll,f-2 );
                mBaiduMap.animateMapStatus( u );
            }
            StringBuffer sb = new StringBuffer(256);
            sb.append( "time:" );
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation){
                //GPS定位
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append( "\nheight:" );
                sb.append( location.getAltitude() );
                sb.append( "\ndirection:" );
                sb.append( location.getDirection() );
                sb.append( "\naddr:" );
                sb.append( location.getAddrStr() );
                sb.append( "\ndescribe:" );
                sb.append( "GPS定位成功!" );
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                sb.append( "\noperationer:" );
                sb.append( location.getOperators() );
                sb.append( "\ndescribe:" );
                sb.append( "网络定位成功" );
            }else if(location.getLocType() == BDLocation.TypeOffLineLocation){
                //离线定位
                sb.append( "\ndescribe:" );
                sb.append( "离线定位成功" );
            }else if(location.getLocType() == BDLocation.TypeServerError){
                sb.append( "\ndescribe:" );
                sb.append( "server定位失败,没有对应的位置信息" );
            }else if(location.getLocType() == BDLocation.TypeNetWorkException){
                sb.append( "\ndescribe:" );
                sb.append( "网络连接失败" );
            }
            mTextView.setText( sb );
        }
    }


    private void addOpt() {
        LatLng point = new LatLng( 39.9155260000,116.4038470000 );
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource( R.drawable.tag );
        OverlayOptions option = new MarkerOptions().position( point ).icon( bitmap );
        mBaiduMap.addOverlay( option );
    }

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

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

    @Override
    protected void onDestroy() {
        mLocationClient.stop();
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}

3.DemoApplication.java

public class DemoApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType( CoordType.BD09LL);
    }
}

4.定位需要的权限:

<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值