百度地图之定位

作为百度地图小白的我来说,最近刚刚研究了一下百度地图的定位功能。百度地图太强大了,有定位追踪、历史轨迹、覆盖物等功能,我们慢慢来熟悉,使用的时候不至于无处下手!
先来看一下效果图:

这里写图片描述
一、我们来看一下布局文件

MapView上边有两个控制覆盖物样式的RadioButton,可以显示地图样式的textView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

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

    <Button
        android:id="@+id/location_mode"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/location_mode"
        android:layout_below="@id/location_mode"
        android:layout_marginTop="10dp"
        android:orientation="vertical"
        android:padding="2dp">

        <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RadioButton
                android:checked="true"
                android:id="@+id/normal_map"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="默认图标" />

            <RadioButton
                android:id="@+id/special_map"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="自定义图标" />
        </RadioGroup>
    </LinearLayout>
</RelativeLayout>

二、主要的BaiduMapActivity
首先,声明我们需要的控件

 MapView mapView;
 RadioGroup radioGroup;
 Button requseLocButton;
 View.OnClickListener btnOnclicListener;
 private MyLocationConfiguration.LocationMode locationMode;
 private BaiduMap baiduMap;//百度地图
 BitmapDescriptor bitmapMarker;//覆盖物
 boolean isFirstLoc = true; // 是否首次定位
 LocationClient locationClient;//定位客户端
 RadioGroup.OnCheckedChangeListener radioButtonListener;
 public MyLocationListner myLocationListner = 
                             new MyLocationListner();

二、先来看一下,我们在onCreate()方法里调用的方法

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());//这个方法一定要在setContentView()之前调用
        setContentView(R.layout.activity_baidu_map);
        initMap();//控件的初始化
        setButton();//Button设置LocationMode样式
        setRadioGropListener();//radioButton进行监听,控制样式
        requseLocButton.setOnClickListener(btnOnclicListener);
        radioGroup.setOnCheckedChangeListener(radioButtonListener);
        locationClient.start();//启动定位客户端
    }

三、我们来细看一下initMap()方法 ,主要是对控件的初始化

//地图的初始化
    public void initMap() {
        mapView = (MapView) findViewByI(R.id.bmapView);
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        requseLocButton = (Button) findViewById(R.id.location_mode);
        baiduMap = mapView.getMap();
        //开启定位图层
        baiduMap.setMyLocationEnabled(true);
        //开启定位服务
        locationClient = new LocationClient(this);
        locationClient.registerLocationListener(myLocationListner);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);//打开GPS
        option.setCoorType("bd09ll");// 设置坐标类型,返回国测局经纬度坐标系:gcj02 返回百度墨卡托坐标系 :bd09 返回百度经纬度坐标系 :bd09ll
        option.setScanSpan(1000);//设置扫描间隔,单位是毫秒
        locationClient.setLocOption(option);
    }

四、setTextView() 方法,实现textView设置LocationMode样式

 //点击Button对百度地图样式进行设置
        public void setBuuton() {
        requseLocButton.setText("普通");
        locationMode = MyLocationConfiguration.LocationMode.NORMAL;
        btnOnclicListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (locationMode) {
                    case NORMAL:
                        requseLocButton.setText("跟随");
                        locationMode = MyLocationConfiguration.LocationMode.FOLLOWING;
                        baiduMap.setMyLocationConfigeration(new MyLocationConfiguration(locationMode, true, bitmapMarker));
                        break;
                    case FOLLOWING:
                        requseLocButton.setText("罗盘");
                        locationMode = MyLocationConfiguration.LocationMode.COMPASS;
                        baiduMap.setMyLocationConfigeration(new MyLocationConfiguration(locationMode, true, bitmapMarker));
                        break;
                    case COMPASS:
                        requseLocButton.setText("普通");
                        locationMode = MyLocationConfiguration.LocationMode.NORMAL;
                        baiduMap.setMyLocationConfigeration(new MyLocationConfiguration(locationMode, true, bitmapMarker));
                        break;
                    default:
                        break;
                }
            }
        };
    }

五、定位SDK监听函数

 //定位SDK监听函数
    public class MyLocationListner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null && mapView == null) {
                return;
            }
            MyLocationData locationData = new MyLocationData.Builder().accuracy(bdLocation.getRadius())
                    .direction(100)// 此处设置开发者获取到的方向信息,顺时针0-360
                    .latitude(bdLocation.getLatitude())
                    .longitude(bdLocation.getLongitude()).build();
            baiduMap.setMyLocationData(locationData);
            if (isFirstLoc) {
                isFirstLoc = true;
                LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(latLng).zoom(18.0f);
                baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }
    }

六、 setRadioGropListener() 方法,设置radioButton的监听事件,控制BitmapDescriptor 的样式

 //设置radioButton的监听事件
    public void setRadioGropListener() {
        radioButtonListener = new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.normal_map) {
                    //传入null则,恢复默认图标
                    bitmapMarker = null;
                    baiduMap.setMyLocationConfigeration(new MyLocationConfiguration(locationMode, true, null));
                }
                if (checkedId == R.id.special_map) {
                    bitmapMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
                    baiduMap.setMyLocationConfigeration(new MyLocationConfiguration(locationMode, true, bitmapMarker));
                }
            }
        };
    }

七、退出百度地图

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

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

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        locationClient.stop();
        // 关闭定位图层
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();
    }

注意
(1)在as里引入相对应的jar包和.so文件,.so文件时候放在main文件夹下的jniLibs文件下的
这里写图片描述

(2) 在AndroidManifest.xml里注册百度地图的service、权限、自己的key

 <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
 </service>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <!-- 这个权限用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <!-- 这个权限用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- 用于读取手机当前的状态 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 访问网络,网络定位需要上网 -->
    <uses-permission android:name="android.permission.INTERNET" />

这里写图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值