android百度地图开发V4.5最新版(4)---显示自己的位置在地图上

将自己的位置显示在地图上,这个主要是用到定位,然后将point加到mapview上面。直接撸代码

 <com.baidu.mapapi.map.TextureMapView  android:id="@+id/mTexturemap"  android:layout_width="fill_parent"  android:layout_height="400dip">
    </com.baidu.mapapi.map.TextureMapView>
    <LinearLayout  android:layout_width="fill_parent"  android:layout_height="match_parent"  android:orientation="vertical">
        <TextView  android:id="@+id/tv_addr"  android:layout_width="fill_parent"  android:layout_height="40dip"  android:layout_marginTop="22dip"  android:layout_marginLeft="10dip"  />
        <LinearLayout  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="horizontal"  android:gravity="center"  >
            <Button  android:id="@+id/bt_dingwei"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="我的位置"  />
            <Button  android:id="@+id/bt_dingwei2"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginLeft="55dip"  android:text="添加覆盖物"  />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

java文件关键代码:

 public MyLocationListenner locListener = new MyLocationListenner();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext  //注意该方法要再setContentView方法之前实现  SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        //获取地图控件  mMapView = (TextureMapView) findViewById(R.id.mTexturemap);
        tv_addr = (TextView) findViewById(R.id.tv_addr);
        addOverlayBtn = (Button) findViewById(R.id.bt_dingwei);
        locCurplaceBtn = (Button) findViewById(R.id.bt_dingwei2);
        addOverlayBtn.setEnabled(false);

        //设置地图缩放级别16 类型普通地图  mBaiduMap = mMapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(16.0f);
        mBaiduMap.setMapStatus(msu);
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        //开启定位图层  mBaiduMap.setMyLocationEnabled(true);
        //定位初始化  //注意: 实例化定位服务 LocationClient类必须在主线程中声明 并注册定位监听接口  mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(locListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);              //打开GPS  option.setCoorType("bd09ll");        //设置坐标类型  option.setScanSpan(5000);            //设置发起定位请求的间隔时间为5000ms  mLocClient.setLocOption(option);     //设置定位参数  mLocClient.start();                  //调用此方法开始定位   //Button 添加覆盖物  addOverlayBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                addCircleOverlay();
            }
        });

        //Button 定位当前位置  locCurplaceBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                addMyLocation();
            }
        });

    }

    /**  * 定位SDK监听器 需添加locSDK jarso文件  */  public class MyLocationListenner implements BDLocationListener {

        public void onReceivePoi(BDLocation location) {
        }

        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapview 销毁后不在处理新接收的位置  if (location == null || mBaiduMap == null) {
                return;
            }
            //MyLocationData.Builder定位数据建造器  MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    .direction(100)
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude())
                    .build();
            //设置定位数据  mBaiduMap.setMyLocationData(locData);
            mCurrentMode = LocationMode.NORMAL;
            //获取经纬度  latitude = location.getLatitude();
            longitude = location.getLongitude();
            addr=location.getAddrStr();
            tv_addr.setText(addr);

            //Toast.makeText(getApplicationContext(), String.valueOf(latitude), Toast.LENGTH_SHORT).show();  //第一次定位的时候,那地图中心点显示为定位到的位置  if (isFirstLoc) {
                isFirstLoc = false;
                //地理坐标基本数据结构  LatLng loc = new LatLng(location.getLatitude(),location.getLongitude());
                //MapStatusUpdate描述地图将要发生的变化  //MapStatusUpdateFactory生成地图将要反生的变化  MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(loc);
                mBaiduMap.animateMapStatus(msu);
                Toast.makeText(getApplicationContext(), location.getAddrStr(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**  * 定位并添加标注  */  private void addMyLocation() {
        //更新  mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker));
        mBaiduMap.clear();
        addOverlayBtn.setEnabled(true);
        //定义Maker坐标点  LatLng point = new LatLng(latitude, longitude);
        //构建Marker图标  BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.start_point);
        //构建MarkerOption,用于在地图上添加Marker  OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker,并显示  mBaiduMap.addOverlay(option);
    }

    /**  * 添加覆盖物  */  private void addCircleOverlay() {
        if(isShowOverlay == 1) {  //点击显示  mBaiduMap.clear();
            isShowOverlay = 0;
            //DotOptions 圆点覆盖物  LatLng pt = new LatLng(latitude, longitude);
            CircleOptions circleOptions = new CircleOptions();
            //circleOptions.center(new LatLng(latitude, longitude));  circleOptions.center(pt);                          //设置圆心坐标  circleOptions.fillColor(0xAAFFFF00);               //圆填充颜色  circleOptions.radius(250);                         //设置半径  circleOptions.stroke(new Stroke(5, 0xAA00FF00));   // 设置边框  mBaiduMap.addOverlay(circleOptions);
        }
        else {
            mBaiduMap.clear();
            isShowOverlay = 1;
        }
    }

效果图: