Android百度地图开发(二)定位之手动定位

Android百度地图定位与显示
本文介绍如何在Android应用中使用百度地图API实现用户位置的自动定位,并将定位点显示在地图中央。通过示例代码展示了如何设置地图视图、响应定位按钮点击事件以及更新地图状态。

接上篇 Android百度地图开发(二)定位之自动定位及标注(及定位几日内瓦或坐标4.9E-324问题)

直接上代码:

布局文件如下:

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

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

    <ImageButton
        android:id="@+id/fire_map_ib"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:background="@null"
        android:src="@drawable/ic_myloc_btn_black" />

</RelativeLayout>


相关class代码如下:

/**
         * 点击定位并将位置移动到屏幕中间
         */
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mLocationClient.requestLocation();

                LatLng point = new LatLng(
                        MyApplication.LOC_LAT, MyApplication.LOC_LON);
                MapStatusUpdate u = MapStatusUpdateFactory
                        .newLatLng(point);
            /*u = MapStatusUpdateFactory.newLatLngZoom(point,
                    mBaiduMap.getMaxZoomLevel());*/
                mBaiduMap.animateMapStatus(u);
                mBaiduMap.setMapStatus(u);

                Toast.makeText(getActivity(), "定位中……", Toast.LENGTH_SHORT).show();
            }
        });

请自行整合之前代码,如有疑问,欢迎留言讨论

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值