主要的用途是让当前用户知道自己还有哪些用户在使用此款app,为实现后续的约跑,聊天等功能做铺垫。
要知道附近其他用户必须先知道自己的定位,那就要用到之前文章中提到的定位功能,这里不再讲解。
调用后台接口上传当前用户的x、y坐标点。获取后台传回的用户的对象list。
List<UserBean> userlist = new ArrayList<UserBean>(); String resultData = String.valueOf(resultMap .get("Data")); Map jsonMap = JSON.parseObject(resultData); String jsonuserlist = String.valueOf(jsonMap.get("personList")); // 服务端返回数据 发送消息给handler处理数据 //附近用户list userlist.clear(); userlist= JSON.parseArray(jsonuserlist, UserBean.class);if(!CheckUtils.isEmpty(userlist)){ getMapMark(); }
getMapMark();该方法
private final int NEARBY_OVERLAYOPTIONS=10000; private final int NEARBY_UPMAPVIEW=20000;
private void getMapMark(){ if(!CheckUtils.isEmpty(mBaiduMap)){ mBaiduMap.clear(); } if(!CheckUtils.isEmpty(mLocationClient)){ mLocationClient=null; } mLocationClient = new LocationClient(mContext); // 声明LocationClient类 mBaiduMap = mMapView.getMap(); // x:经度 y:纬度 Double x =Double.valueOf(format.format(globalApp.getLongitude())); Double y = Double.valueOf(format.format(globalApp.getLatitude())); // 定义Maker坐标点 LatLng point = new LatLng(y, x); new addThread(userlist,point).start(); }在线程中加载图片显示头像,使用线程为了避免图片加载不出来,加载不全。
class addThread extends Thread{ private List<UserBean> userlist; private LatLng point; private boolean isTure; public addThread( List<UserBean> userlist,LatLng point) { this.userlist=userlist; this.point=point; } @Override public void run() { Looper.prepare(); isTure=false; Bitmap bimp=drawBitmap(globalApp.getmUserBean().getNc(),globalApp.getmUserBean().getHeadUrl(),globalApp.getmUserBean().getXb()); if(!CheckUtils.isEmpty(bimp)){ // 构建Marker图标 BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(bimp); // 构建MarkerOption,用于在地图上添加Marker OverlayOptions option = new MarkerOptions().position(point) .icon(bitmap); // 在地图上添加Marker,并显示 Message m=new Message(); m.what=NEARBY_OVERLAYOPTIONS; m.obj=option; NearbyFragment.this.handler.sendMessage(m); if(!CheckUtils.isEmpty(userlist)) { for (int i = 0; i < userlist.size(); i++) { // x:经度 y:纬度 Double x1 = Double.valueOf(userlist.get(i).getX()); Double y1 = Double.valueOf(userlist.get(i).getY()); // 定义Maker坐标点 LatLng point1 = new LatLng(y1, x1); Bitmap bimp1 = drawBitmap(userlist.get(i).getNc(), userlist.get(i).getHeadUrl(), userlist.get(i).getXb()); if (!CheckUtils.isEmpty(bimp1)) { // 构建Marker图标 BitmapDescriptor bitmap1 = BitmapDescriptorFactory.fromBitmap(bimp1); // 构建MarkerOption,用于在地图上添加Marker OverlayOptions option1 = new MarkerOptions().position(point1) .icon(bitmap1); m=new Message(); m.what=NEARBY_OVERLAYOPTIONS; m.obj=option1; NearbyFragment.this.handler.sendMessage(m); } } } Message m2=new Message(); m2.what=NEARBY_UPMAPVIEW; m2.obj=point; NearbyFragment.this.handler.sendMessage(m2); } Looper.loop(); } private int anInt; public Bitmap drawBitmap(String title, final String picUrl, String sex) { Bitmap bmp = Bitmap.createBitmap(DisplayUtil.dip2px(mContext, 40), DisplayUtil.dip2px(mContext, 51), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_text_view, null); AppCompatImageView ivGps= (AppCompatImageView) layout.findViewById(R.id.sex_img_gps); if(!CheckUtils.isEmpty(sex)){ if(sex.equals("2")){ ivGps.setImageResource(R.drawable.ic_sex_woman_bg); // ivGps.setBackgroundResource(R.drawable.ic_sex_woman_bg); }else{//男1 ivGps.setImageResource(R.drawable.ic_sex_man_bg); // ivGps.setBackgroundResource(R.drawable.ic_sex_man_bg); } } // TextView titleView = (TextView) layout // .findViewById(R.id.driver_location_name); isTure=true; CircleImageView iv= (CircleImageView) layout.findViewById(R.id.driver_overlay_master); BitmapUtils.display(iv, picUrl, new Callback.CacheCallback<Drawable>() { @Override public boolean onCache(Drawable drawable) { isTure=false; return false; } @Override public void onSuccess(Drawable drawable) { // LogUtil.e("下载成功"); isTure=false; } @Override public void onError(Throwable throwable, boolean b) { // LogUtil.e("下载出错," + arg0.getMessage()); isTure=false; } @Override public void onCancelled(CancelledException e) { // LogUtil.e("下载取消"); isTure=false; } @Override public void onFinished() { // LogUtil.e("下载完成"); isTure=false; } }); while (isTure){ try { Thread.sleep(200); }catch (InterruptedException e) { e.printStackTrace(); } System.out.println("等待图片的下载"+(anInt+1)); } anInt+=1; // titleView.setText(title); layout.setDrawingCacheEnabled(true); layout.measure(View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec .makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY)); layout.getMeasuredHeight(); Paint paint = new Paint(); layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight()); layout.buildDrawingCache(); Bitmap bm = layout.getDrawingCache(); //layout.draw(canvas); canvas.drawBitmap(bm, 0, 0, paint); return bmp; } };