From 8ce3abe8b9d08228a56c082353ae1e537cd26536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=8C?= Date: Sat, 11 Jun 2016 22:35:43 +0800 Subject: [PATCH] finish the demo --- .../java/cn/bmob/imdemo/ui/LocActivity.java | 238 +++++++----------- 1 file changed, 96 insertions(+), 142 deletions(-) diff --git a/app/src/main/java/cn/bmob/imdemo/ui/LocActivity.java b/app/src/main/java/cn/bmob/imdemo/ui/LocActivity.java index 00b6e87..6d83392 100644 --- a/app/src/main/java/cn/bmob/imdemo/ui/LocActivity.java +++ b/app/src/main/java/cn/bmob/imdemo/ui/LocActivity.java @@ -27,6 +27,7 @@ import com.orhanobut.logger.Logger; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; +import java.util.List; import java.util.Map; import cn.bmob.imdemo.R; @@ -34,40 +35,45 @@ import cn.bmob.imdemo.adapter.ChatAdapter; import cn.bmob.imdemo.base.ParentWithNaviActivity; import cn.bmob.imdemo.util.Utils; import cn.bmob.imdemo.bean.FindUOverlayManager; +import cn.bmob.newim.BmobIM; import cn.bmob.newim.bean.BmobIMConversation; import cn.bmob.newim.bean.BmobIMLocationMessage; import cn.bmob.newim.bean.BmobIMMessage; import cn.bmob.newim.core.BmobIMClient; +import cn.bmob.newim.event.MessageEvent; +import cn.bmob.newim.listener.MessageListHandler; import cn.bmob.newim.listener.MessageSendListener; +import cn.bmob.newim.listener.ObseverListener; import cn.bmob.v3.exception.BmobException; -public class LocActivity extends ParentWithNaviActivity{ - MapView mMapView = null; - BaiduMap mBaiduMap = null; +public class LocActivity extends ParentWithNaviActivity implements ObseverListener,MessageListHandler { + public MapView mMapView = null; + public BaiduMap mBaiduMap = null; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); - private MyLocationData mylocdata = null; - private MyLocationData friendlocdata = null; - + public LatLng my_loc = new LatLng(28.233982,113.003927); + public LatLng friend_loc = new LatLng(28.233982,113.003927); private LinkedList locationList = new LinkedList(); // 存放历史定位结果的链表,最大存放当前结果的前5次定位结果 int exe_count = 0; BmobIMConversation c; - ChatAdapter adapter; + //ChatAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化百度地图的定位接口 - mLocationClient = new LocationClient( getApplicationContext()); + mLocationClient = new LocationClient(getApplicationContext()); SDKInitializer.initialize(getApplicationContext()); - mLocationClient.registerLocationListener( myListener ); + mLocationClient.registerLocationListener(myListener); setContentView(R.layout.activity_loc); - c= BmobIMConversation.obtain(BmobIMClient.getInstance(), (BmobIMConversation) getBundle().getSerializable("c")); - adapter = new ChatAdapter(this,c); + c = BmobIMConversation.obtain(BmobIMClient.getInstance(), (BmobIMConversation) getBundle().getSerializable("c")); + BmobIM.getInstance().addMessageListHandler(this); + + //adapter = new ChatAdapter(this, c); initNaviView(); initLocation(); mMapView = (MapView) findViewById(R.id.bmapView); @@ -75,55 +81,8 @@ public class LocActivity extends ParentWithNaviActivity{ mBaiduMap = mMapView.getMap(); mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); - // 开启定位图层 - - mLocationClient.start(); - Logger.i("before \n"); - //mLocationClient.requestLocation(); - - Logger.i("after \n"); - - - - - - //mBaiduMap.clear(); - - //mLocationClient.requestLocation(); - - //send_recv_real_time(); - - } - - private void send_recv_real_time() { - new Thread(new Runnable() { - @Override - public void run() { - - int times = 1; - int maxtime = 1000000; - while( times < maxtime ) { - - /* BmobIMLocationMessage location =new BmobIMLocationMessage("广州番禺区",23.5,112.0); - Map map =new HashMap<>(); - map.put("from", "百度地图"); - location.setExtraMap(map); - c.sendMessage(location, listener); -*/ - - try { - Thread.sleep(500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - } - - } - }) ; - } /** @@ -141,12 +100,12 @@ public class LocActivity extends ParentWithNaviActivity{ @Override public void onStart(BmobIMMessage msg) { super.onStart(msg); - adapter.addMessage(msg); + //adapter.addMessage(msg); } @Override public void done(BmobIMMessage msg, BmobException e) { - adapter.notifyDataSetChanged(); + //adapter.notifyDataSetChanged(); if (e != null) { toast(e.getMessage()); } @@ -191,15 +150,14 @@ public class LocActivity extends ParentWithNaviActivity{ @Override protected String title() { - return "find ";// + c.getConversationTitle(); + return "寻找 " + c.getConversationTitle(); } private void initLocation(){ LocationClientOption option = new LocationClientOption(); - option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy - );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 + option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 - int span=1000; + int span=4000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps @@ -216,30 +174,7 @@ public class LocActivity extends ParentWithNaviActivity{ @Override public void onReceiveLocation(BDLocation location) { - /*StringBuffer sb = new StringBuffer(256); - sb.append("time : "); - sb.append(location.getTime()); - sb.append("\nerror code : "); - sb.append(location.getLocType()); - sb.append("\nlatitude : "); - sb.append(location.getLatitude()); - sb.append("\nlontitude : "); - sb.append(location.getLongitude()); - sb.append("\nradius : "); - sb.append(location.getRadius()); - - sb.append("\nlocationdescribe : "); - sb.append(location.getLocationDescribe());// 位置语义化信息 - List list = location.getPoiList();// POI数据 - if (list != null) { - sb.append("\npoilist size = : "); - sb.append(list.size()); - for (Poi p : list) { - sb.append("\npoi= : "); - sb.append(p.getId() + " " + p.getName() + " " + p.getRank()); - } - } - Logger.i(sb.toString());*/ + exe_count = exe_count + 1; if (location != null && (location.getLocType() == 161 || location.getLocType() == 66)) { Message locMsg = locHander.obtainMessage(); @@ -257,22 +192,7 @@ public class LocActivity extends ParentWithNaviActivity{ sb.append("\n"); Logger.i( sb.toString() ); mBaiduMap.setMyLocationEnabled(true); - /* - // 构造定位数据 - MyLocationData locData = new MyLocationData.Builder() - .accuracy(location.getRadius()) - // 此处设置开发者获取到的方向信息,顺时针0-360 - .direction(100).latitude(location.getLatitude()) - .longitude(location.getLongitude()).build(); - // 设置定位数据 - mBaiduMap.setMyLocationData(locData); - - // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标) - BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory - .fromResource(R.drawable.chat_voice_bg); - MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker); - mBaiduMap.setMyLocationConfigeration(config); - */ + } } /*** @@ -337,52 +257,23 @@ public class LocActivity extends ParentWithNaviActivity{ BDLocation location = msg.getData().getParcelable("loc"); int iscal = msg.getData().getInt("iscalculate"); if (location != null) { - LatLng point = new LatLng(location.getLatitude(), location.getLongitude()); - // 构建Marker图标 - BitmapDescriptor bitmap = null; - if (iscal == 0) { - bitmap = BitmapDescriptorFactory.fromResource(R.drawable.chat_voice_bg_press); // 非推算结果 - } else { - bitmap = BitmapDescriptorFactory.fromResource(R.drawable.chat_voice_bg); // 推算结果 - } - - // 构建MarkerOption,用于在地图上添加Marker - OverlayOptions option1 = new MarkerOptions().position(point).icon(bitmap); - LatLng point2 = new LatLng(location.getLatitude() + 0.1, location.getLongitude() + 0.1); - - OverlayOptions option2 = new MarkerOptions().position(point2).icon(bitmap); - LatLng point3 = new LatLng(location.getLatitude() - 0.05, location.getLongitude() - 0.05); - LatLng point4 = new LatLng(location.getLatitude() + 0.15, location.getLongitude() + 0.15); - - OverlayOptions option3 = new MarkerOptions().position(point3); - OverlayOptions option4 = new MarkerOptions().position(point4); - - // 在地图上添加Marker(多个),并显示 - FindUOverlayManager overlayManager = new FindUOverlayManager(mBaiduMap); - ArrayList aslist = new ArrayList(); - aslist.add(option1); - aslist.add(option2); - //aslist.add(option3); - //aslist.add(option4); - overlayManager.setData(aslist); - mBaiduMap.setOnMarkerClickListener(overlayManager); - overlayManager.addToMap(); - if( exe_count < 3) - overlayManager.zoomToSpan(); - mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng( new LatLng ( (point.latitude + point2.latitude )/ 2,(point.longitude + point2.longitude)/2)) ); - - BmobIMLocationMessage loc_msg =new BmobIMLocationMessage("myloc",location.getLatitude(),location.getAltitude()); + + my_loc = new LatLng(location.getLatitude(), location.getLongitude()); + + refresh_map(); + + BmobIMLocationMessage loc_msg =new BmobIMLocationMessage("myloc",location.getLatitude(),location.getLongitude()); c.sendMessage(loc_msg, new MessageSendListener() { @Override public void onStart(BmobIMMessage msg) { - adapter.addMessage(msg); - adapter.notifyDataSetChanged(); + //adapter.addMessage(msg); + //adapter.notifyDataSetChanged(); } @Override public void done(BmobIMMessage msg, BmobException e) { - adapter.notifyDataSetChanged(); + // adapter.notifyDataSetChanged(); if (e != null) { toast(e.getMessage()); } @@ -395,7 +286,70 @@ public class LocActivity extends ParentWithNaviActivity{ } } + }; + + public void refresh_map() { + + StringBuffer sb = new StringBuffer(256); + sb.append("my loc :"); + sb.append(my_loc.latitude ); + sb.append(" , "); + sb.append(my_loc.longitude); + sb.append(" friend loc :"); + sb.append(friend_loc.latitude ); + sb.append(" , "); + sb.append(friend_loc.longitude); + Logger.i( sb + " end"); + + LatLng point1 = my_loc; + LatLng point2 = friend_loc; + // 构建Marker图标 + BitmapDescriptor bitmap = null; + + bitmap = BitmapDescriptorFactory.fromResource(R.drawable.chat_voice_bg_press); // 非推算结果 + + // 构建MarkerOption,用于在地图上添加Marker + OverlayOptions option1 = new MarkerOptions().position(point1).icon(bitmap); + OverlayOptions option2 = new MarkerOptions().position(point2).icon(bitmap); + + // 在地图上添加Marker(多个),并显示 + FindUOverlayManager overlayManager = new FindUOverlayManager(mBaiduMap); + ArrayList aslist = new ArrayList(); + aslist.add(option1); + aslist.add(option2); + + mBaiduMap.clear(); + overlayManager.setData(aslist); + mBaiduMap.setOnMarkerClickListener(overlayManager); + overlayManager.addToMap(); + if( exe_count < 3) { + overlayManager.zoomToSpan(); + mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng((point1.latitude + point2.latitude) / 2, (point1.longitude + point2.longitude) / 2))); + } + } + + @Override + public void onMessageReceive(List list) { + Logger.i("聊天页面接收到消息:" + list.size()); + //当注册页面消息监听时候,有消息(包含离线消息)到来时会回调该方法 + for (int i=0;i