关闭

百度地图定位开发

标签: 百度地图定位开发
149人阅读 评论(1) 收藏 举报
分类:

百度地图定位开发


先给个网站什么资料都是这里的,毕竟是百度的东西当然别个要教你怎么写了
http://lbsyun.baidu.com/
这个网站就是开者网站

开发流程
1 你需要注册一个百度的帐号
https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http://lbsyun.baidu.com/index.php?title=android-locsdk
2 注册完成进入API控制台获取KEY
点击创建应用选择你的类型,这里面的麻烦的是怎么获取SHA1值,我们知道eclipse查看很简单,android studio 感觉不好查看,就给个软件去看把
3 在获取到key以后就去下载百度定位的SDK
http://lbsyun.baidu.com/index.php?title=android-locsdk/geosdk-android-download
4 导入到你的项目的libs包下
5下面贴一下获取定位的代码

public class LocationUtil {
    private final static boolean DEBUG = true;
    private final static String  TAG   = "LocationUtil88";
    private static LocationUtil mInstance;
    private BDLocation mLocation     = null;
    private MLocation  mBaseLocation = new MLocation();
    public StringBuffer mSb;
    public BDLocationListener myListener = new MyLocationListener();
    private LocationClient mLocationClient;
    public static LocationUtil getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new LocationUtil(context);
        }
        return mInstance;
    }
    private LocationUtil(Context context) {
        mLocationClient = new LocationClient(context.getApplicationContext());
        initParams();
    }

    public void startMonitor() {
        if (DEBUG) Log.d(TAG, "start monitor location");
        if (!mLocationClient.isStarted()) {
            mLocationClient.start();
        }
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.requestLocation();
        } else {
            Log.d("LocSDK3", "locClient is null or not started");
        }
    }

    public void stopMonitor() {
        if (DEBUG) Log.d(TAG, "stop monitor location");
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
    }

    public BDLocation getLocation() {
        if (DEBUG) Log.d(TAG, "get location");
        return mLocation;
    }

    public MLocation getBaseLocation() {
        if (DEBUG) Log.d(TAG, "get location");
        return mBaseLocation;
    }

    private void initParams() {
        LocationClientOption option = new LocationClientOption();
        mLocationClient.registerLocationListener(myListener);
        option.setOpenGps(true);
        //option.setPriority(LocationClientOption.GpsFirst);
        //option.setAddrType("all");//返回的定位结果包含地址信息
        option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
        option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
        option.disableCache(true);//禁止启用缓存定位
        option.setLocationMode(LocationClientOption.
        LocationMode.Hight_Accuracy);
        option.setIsNeedAddress(true);
        option.setNeedDeviceDirect(true);
        option.setPriority(LocationClientOption.NetWorkFirst);
        mLocationClient.setLocOption(option);
        mLocationClient.requestLocation();
        mLocationClient.start();
        //option.setPoiNumber(5);    //最多返回POI个数
        //option.setPoiDistance(1000); //poi查询距离
        //option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息
    }
    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) {
                return ;
            }
            mLocation = location;
            mBaseLocation.latitude = mLocation.getLatitude();
            mBaseLocation.longitude = mLocation.getLongitude();
            mSb = new StringBuffer(256);
            mSb.append("time : ");
            mSb.append(location.getTime());
            mSb.append("\nerror code : ");
            mSb.append(location.getLocType());
            mSb.append("\nlatitude : ");
            mSb.append(location.getLatitude());
            mSb.append("\nlontitude : ");
            mSb.append(location.getLongitude());
            mSb.append("\nradius : ");
            mSb.append(location.getRadius());
            mSb.append("\ncity : ");
            mSb.append(location.getCity());
            if (location.getLocType() == BDLocation.TypeGpsLocation){
                mSb.append("\nspeed : ");
                mSb.append(location.getSpeed());
                mSb.append("\nsatellite : ");
                mSb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation)
            {
                mSb.append("\naddr : ");
                mSb.append(location.getAddrStr());
            }
            if (DEBUG) Log.d("LocationUtil88", "" + mSb);
        }
        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    public class MLocation {
        public double latitude;
        public double longitude;
    }
}

上面是获取当前用户位置的代码,你可以直接当作工具类来使用,当然你想隔多长时间获取一次就需要写一个定时器了,这样就可以做轨迹的记录了。这里就不贴代码了
最后分享一个AS获取SHA1的APK
链接: http://pan.baidu.com/s/1miybCSw 密码: 9697

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2096次
    • 积分:121
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论