Android 利用百度地图SDK实现定位功能

SDK使用说明及下载地址:http://developer.baidu.com/map/sdk-android.htm
map.xml:



    
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:clickable="true" />

Map.java:

public class HouseKeeperSearch extends Activity {
private MapView mMapView;
private MapController mMapController = null;
// 定位相关
private LocationClient mLocClient;
private LocationData locData = null;
public MyLocationListener myListener = new MyLocationListener();
boolean isFirstLoc = true;// 是否首次定位
private MyLocationOverlay myLocationOverlay = null; // 定点标志
private BMapManager mBMapMan = null;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBMapMan = new BMapManager(getApplication());
    mBMapMan.init("Ut5QiVKCvqiXol4Ph6uW3N3h", null);
    pd = new ProgressDialog(this);
    pd.setIndeterminate(true);
    pd.setMessage(getString(R.string.loading_location));
    pd.setCancelable(true);
    pd.show();
    setContentView(R.layout.activity_housekeeper_search);
    initMap();
}

private void initMap() {
    // 地图初始化
    mMapView = (MapView) findViewById(R.id.map_view);
    mMapController = mMapView.getController();
    mMapView.getController().setZoom(16);
    mMapView.getController().enableClick(true);
    mMapView.setBuiltInZoomControls(true);
    // 定位初始化
    mLocClient = new LocationClient(this);
    locData = new LocationData();
    mLocClient.registerLocationListener(myListener);
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true);// 打开gps
    option.setCoorType("bd09ll"); // 设置坐标类型
    option.setScanSpan(1000);
    mLocClient.setLocOption(option);
    mLocClient.start();           //启动

    // 定位图层初始化
    myLocationOverlay = new MyLocationOverlay(mMapView);
    // 设置定位数据
    myLocationOverlay.setMarker(getResources().getDrawable(
            R.drawable.location));
    // 添加定位图层
    mMapView.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableCompass();
    // 修改定位数据后刷新图层生效
    mMapView.refresh();       //加载
}
/**
 * 定位SDK监听函数
 */
public class MyLocationListener implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location == null)
            return;
        locData.latitude = location.getLatitude();
        locData.longitude = location.getLongitude();
        // 如果不显示定位精度圈,将accuracy赋值为0即可
        locData.accuracy = location.getRadius();
        // 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。
        locData.direction = location.getDerect();
        myLocationOverlay.setData(locData);
        // 更新图层数据执行刷新后生效
        mMapView.refresh();
        // 是手动触发请求或首次定位时,移动到定位点
        if (isFirstLoc) {
            // 移动地图到定位点
            Log.d("LocationOverlay", "receive location, animate to it");
            mMapController.animateTo(new GeoPoint(
                    (int) (locData.latitude * 1e6),
                    (int) (locData.longitude * 1e6)));
            if (pd.isShowing())
                pd.dismiss();
        }
        // 首次定位完成
        isFirstLoc = false;

    }

    public void onReceivePoi(BDLocation poiLocation) {
        if (poiLocation == null) {
            return;
        }
    }

}

@Override
protected void onPause() {
    mMapView.onPause();
    if (mBMapMan != null) {
        mBMapMan.stop();
    }
    super.onPause();
}

@Override
protected void onResume() {
    mMapView.onResume();
    if (mBMapMan != null) {
        mBMapMan.start();
    }
    super.onResume();
}

@Override
protected void onDestroy() {
    // 退出时销毁定位
    if (mLocClient != null)
        mLocClient.stop();
    mMapView.destroy();
    if (mBMapMan != null) {
        mBMapMan.destroy();
        mBMapMan = null;
    }
    super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mMapView.onSaveInstanceState(outState);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mMapView.onRestoreInstanceState(savedInstanceState);
}

}
效果图:
Android 利用百度地图SDK实现定位功能_第1张图片

你可能感兴趣的:(baidu-地图,android)