1. 程式人生 > >android中百度地圖撒點標識後臺附近的人座標點及使用者頭像

android中百度地圖撒點標識後臺附近的人座標點及使用者頭像

主要的用途是讓當前使用者知道自己還有哪些使用者在使用此款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;
}
};