安卓用百度定位

因为安卓自身的定位问题很多,多次尝试之后还是放弃了,百度定位它不香吗
需要的库去百度地图下载
方法很简单,我的需求是获得自身经纬度,如下

	boolean isFirstLoc; // 只获得一次即可
	 /**
     * 获得自身坐标
     */
    public void getMyLoc() {     
        isFirstLoc = true;
        // 定位初始化
        LocationClient mLocClient = new LocationClient(this);
        MyLocationListenner myListener = new MyLocationListenner();
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        // 打开gps
        option.setOpenGps(true);
        // 设置坐标类型
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }
    
     /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location != null && isFirstLoc && location.getLocType() != BDLocation.TypeServerError) {
                int errorCode = location.getLocType();
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                Log.d("unity", "第一次定位" + location.getLatitude() + "," + location.getLongitude() + "错误代码:" + errorCode);
                isFirstLoc = false;
                //UnityPlayer.UnitySendMessage("AR Camera", "GetLoc", location.getLongitude() + "," + location.getLatitude());
            }
        }
    }

其他的一些配置官方文档都有写,我就不放了

发布了57 篇原创文章 · 获赞 22 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Mediary/article/details/103941058