新建文本文档

使用百度地图,定位你当前位置并显示

1、准备

  • 注册百度开发者账号
  • 到百度地图开放平台创建应用(as获取SHA1值)

2、将sdk整到你的项目中去

- 在你app的gradle.build中添加

 dependencies {
            ......
compile files('libs/baidumapapi_base_v4_0_0.jar')
compile files('libs/baidumapapi_cloud_v4_0_0.jar')
compile files('libs/baidumapapi_map_v4_0_0.jar')
compile files('libs/baidumapapi_radar_v4_0_0.jar')
compile files('libs/baidumapapi_search_v4_0_0.jar')
compile files('libs/baidumapapi_util_v4_0_0.jar')
compile files('libs/locSDK_6.13.jar')
}

android {
    ......
    sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }

}

3、在AndroidManifest.xml中配置











 


    
    

4、在activity中

  1. oncreate中

    地图初始化

    mBaiduMap = mMapView.getMap();
    // 开启定位图层
    mBaiduMap.setMyLocationEnabled(true);
    // 定位初始化
    mLocClient = new LocationClient(getActivity());
    mLocClient.registerLocationListener(myListener);
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true); // 打开gps
    option.setCoorType("bd09ll"); // 设置坐标类型
    option.setScanSpan(1000);
    mLocClient.setLocOption(option);
    mLocClient.start();
    LatLng ll = new LatLng(100,
            100);
    MapStatus.Builder builder = new MapStatus.Builder();
    builder.target(ll).zoom(18.0f);
    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    
  2. 内部类监听
    public class MyLocationListenner implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        // map view 销毁后不在处理新接收的位置
        if (location == null || mMapView == null) {
            return;
        }
        mBaiduMap.clear();
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                // 此处设置开发者获取到的方向信息,顺时针0-360
                .direction(100).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        mBaiduMap.setMyLocationData(locData);
        LatLng llA=new LatLng(location.getLatitude(),location.getLongitude());
        MarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA)
                .zIndex(9).draggable(true);
        mBaiduMap.addOverlay(ooA);
    
        if (isFirstLoc) {
            isFirstLoc = false;
            LatLng ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(ll).zoom(18.0f);
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        }
    }
    
    public void onReceivePoi(BDLocation poiLocation) {
    }
    

    }

5、在布局文件中

 

6、到此应该可以显示地图了,手机开wifi,开gsp试一试吧!

你可能感兴趣的:(Android)