开发者社区> 问答> 正文

利用百度地图的定位,覆盖物与绘制功能一起时,定位不准确,同时绘的图显示不出来

想做个功能,进入界面先定位自己的位置,然后在界面上可以输入一个中心点地点,以这个中心点地点画一个园,显示在地图上。代码如下:

 public class Safety_MarginActivity extends Activity implements OnGetGeoCoderResultListener,OnClickListener {
    private EditText city,center_et,range_et;
    private Button range_confirm;
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LatLng centerPoint,currentPoint;
    private GeoCoder mSearch;

    private LocationClient mLocationClient;
    private LocationMode mLocationMode;
    private MyLocationListener mLocationListener;
    private Marker mMarkerA, mMarkerB, mMarkerC, mMarkerD, mMarkerE;

    private double mLatitude, mLongitude;
    private float accuracy;
    private boolean isFirstIn = true;
    private String range;
    private String location;

    BitmapDescriptor iconMarkA = null;
    BitmapDescriptor iconMarkB = null;
    BitmapDescriptor mIconLocation;

    private String username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            SDKInitializer.initialize(getApplicationContext());
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.safety_margin);
            //初始化界面
            initView();
            //进入界面后定位
            initLocation();
            //定位后添加覆盖物
            initOverlay();
    }
    private void initView() {
            mMapView = (MapView) findViewById(R.id.id_bmapView);
            mBaiduMap = mMapView.getMap();

            mSearch = GeoCoder.newInstance();
            mSearch.setOnGetGeoCodeResultListener(this);

            city = (EditText) findViewById(R.id.setting_safety_margin_center_city_et);
            center_et = (EditText) findViewById(R.id.setting_safety_margin_center_et);
            range_et = (EditText) findViewById(R.id.setting_safety_margin_range_et);
            range_confirm = (Button) findViewById(R.id.range_confirm);
            range_confirm.setOnClickListener(this);
            MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.0f);
            mBaiduMap.setMapStatus(msu);
            mIconLocation = BitmapDescriptorFactory
                            .fromResource(R.drawable.navi_map_gps_locked);
            iconMarkA = BitmapDescriptorFactory
                            .fromResource(R.drawable.icon_1);
            iconMarkB = BitmapDescriptorFactory
                            .fromResource(R.drawable.icon_2);
    }

    private void initLocation() {
            mLocationMode = LocationMode.NORMAL;
            mLocationClient = new LocationClient(this);
            mLocationListener = new MyLocationListener();
            mLocationClient.registerLocationListener(mLocationListener);

            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true);
            option.setCoorType("bd90ll");
            option.setIsNeedAddress(true);
            option.setScanSpan(3000);
            mLocationClient.setLocOption(option);
    }


    private void initOverlay() {
             LatLng lla = new LatLng(mLatitude, mLongitude);
             ArrayList<BitmapDescriptor> gifList = new ArrayList<BitmapDescriptor>();
             gifList.add(iconMarkA);
             gifList.add(iconMarkB);
             MarkerOptions moa = new MarkerOptions().position(lla).icons(gifList)
             .zIndex(0).period(100);
             mMarkerA = (Marker) mBaiduMap.addOverlay(moa);
    }
    @Override
    protected void onStart() {
            super.onStart();
            mBaiduMap.setMyLocationEnabled(true);
            mLocationClient.start();
    }

    private class MyLocationListener implements BDLocationListener {

            @Override
            public void onReceiveLocation(BDLocation location) {
                    MyLocationData locData = new MyLocationData.Builder()
                                    .accuracy(location.getRadius()).direction(100).latitude(location.getLatitude())
                                    .longitude(location.getLongitude()).build();

                    // 设置定位数据
                    mBaiduMap.setMyLocationData(locData);
                    MyLocationConfiguration config = new MyLocationConfiguration(
                                    mLocationMode, true, mIconLocation);
                    mBaiduMap.setMyLocationConfigeration(config);

                    if (isFirstIn) {
                            LatLng newll = new LatLng(location.getLatitude(), location.getLongitude());
                            MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(newll);
                            mBaiduMap.animateMapStatus(msu);
                            isFirstIn = false;
                    }
            }
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult result) {
            if(result==null||result.error!= SearchResult.ERRORNO.NO_ERROR){
                    Toast.makeText(Safety_MarginActivity.this, "抱歉,未能找到结果", Toast.LENGTH_SHORT).show();
                    return;
            }
            mBaiduMap.clear();
            mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation()).icon(iconMarkA).zIndex(9));
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
                            .getLocation()));
            mLatitude = result.getLocation().latitude;
            mLongitude =result.getLocation().longitude;
            Toast.makeText(Safety_MarginActivity.this, mLatitude+","+mLongitude, Toast.LENGTH_SHORT).show();

    }
    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {

    }
    @Override
    public void onClick(View view) {
            switch (view.getId()) {
            case R.id.range_confirm:
                    String center = center_et.getText().toString().trim();
                    String currentCity = city.getText().toString().trim();
                    range = range_et.getText().toString().trim();
                    mSearch.geocode(new GeoCodeOption().city(currentCity).address(center));

                    centerPoint = new LatLng(mLatitude, mLongitude);
                    Toast.makeText(Safety_MarginActivity.this, mLatitude+","+mLongitude+","+range, Toast.LENGTH_SHORT).show();

                            OverlayOptions safetyCircle = new CircleOptions().fillColor(0x000000FF).center(centerPoint).stroke(new Stroke(5, 0xAA000000))
                                            .radius(Integer.parseInt(range));
                            mBaiduMap.addOverlay(safetyCircle);

                    break;

            default:
                    break;
            }
    }
            @Override
    protected void onPause() {
            // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
            mMapView.onPause();
            super.onPause();
    }

    @Override
    protected void onResume() {
            // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
            mMapView.onResume();
            mLocationClient.stop();
            super.onResume();
    }

    @Override
    protected void onDestroy() {
            // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
            super.onDestroy();
            mLocationClient.stop();
            mMapView.onDestroy();
            mSearch.destroy();
            // 回收 bitmap 资源
            iconMarkA.recycle();
             iconMarkB.recycle();
             mIconLocation.recycle();
    }


}

进入界面后首先在地图上显示自己的位置就不对,离实际位置大概有1000米左右的距离,其次,选定好中心点画圆,园不显示。
能指点下,错误在哪里吗?

展开
收起
爵霸 2016-03-20 11:40:37 3052 0
1 条回答
写回答
取消 提交回答
  • initOverlay()方法里面的换成这个是下:

    LatLng lla = new LatLng(mLatitude, mLongitude);
     OverlayOptions ooA = new MarkerOptions().position(lla).icon(bdA)
     .draggable(false).extraInfo(bundle);
     mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
    2019-07-17 19:09:19
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
AIOps-百度的思考与实践 立即下载
百度外卖从IDC到云端服务迁移历程 立即下载
百度研发工具集的应用实践——打造持续交付路上的“三堵墙” 立即下载