private void initBaiduMap() { mapView = (MapView) findViewById(R.id.bmapView); baiduMap = mapView.getMap(); baiduMap.setMaxAndMinZoomLevel(18, 13); IntentFilter iFilter = new IntentFilter(); iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR); iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR); receiver = new BaiduReceiver(); registerReceiver(receiver, iFilter); geoCoder = GeoCoder.newInstance(); geoCoder.setOnGetGeoCodeResultListener(this); Intent intent = getIntent(); intentType = intent.getStringExtra(TYPE); initActionBar(R.string.chat_position); if (intentType.equals(TYPE_SELECT)) { // 选择发送位置 // 开启定位图层 baiduMap.setMyLocationEnabled(true); baiduMap.setMyLocationConfigeration( new MyLocationConfigeration(MyLocationConfigeration.LocationMode.NORMAL, true, null)); // 定位初始化 locClient = new LocationClient(this); locClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setProdName("avosim"); option.setOpenGps(true); option.setCoorType("bd09ll"); option.setScanSpan(1000); option.setOpenGps(true); option.setIsNeedAddress(true); option.setIgnoreKillProcess(true); locClient.setLocOption(option); locClient.start(); if (locClient != null && locClient.isStarted()) { locClient.requestLocation(); } if (lastLocation != null) { // 显示在地图上 LatLng ll = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(u); } } else { Bundle b = intent.getExtras(); LatLng latlng = new LatLng(b.getDouble(LATITUDE), b.getDouble(LONGITUDE)); // 维度在前,经度在后 baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latlng)); OverlayOptions ooA = new MarkerOptions().position(latlng).icon(descriptor).zIndex(9); baiduMap.addOverlay(ooA); } }
@Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mapView == null) return; if (lastLocation != null) { if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) { Logger.d(getString(R.string.chat_geoIsSame)); // 若两次请求获取到的地理位置坐标是相同的,则不再定位 locClient.stop(); return; } } lastLocation = location; Logger.d( "lontitude = " + location.getLongitude() + ",latitude = " + location.getLatitude() + "," + getString(R.string.chat_position) + " = " + lastLocation.getAddrStr()); MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .build(); baiduMap.setMyLocationData(locData); LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); String address = location.getAddrStr(); if (address != null && !address.equals("")) { lastLocation.setAddrStr(address); } else { // 反Geo搜索 geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(ll)); } // 显示在地图上 MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(u); // 设置按钮可点击 }