Android 如何将位置数据添加到Quickblox对话框?
我想在聊天中存储一个位置类型,以便根据位置搜索对话,因此我使用Quickblox管理面板创建了一个名为MyDialog的自定义对象。此自定义对象只有一个名为location的字段,类型为location 我尝试了两种方法来保存对话框中的位置数据,但都出现了问题: 方法1:存储双精度[]Android 如何将位置数据添加到Quickblox对话框?,android,quickblox,Android,Quickblox,我想在聊天中存储一个位置类型,以便根据位置搜索对话,因此我使用Quickblox管理面板创建了一个名为MyDialog的自定义对象。此自定义对象只有一个名为location的字段,类型为location 我尝试了两种方法来保存对话框中的位置数据,但都出现了问题: 方法1:存储双精度[] Double myLocation[] = {locationManager.getLastLocation().getLatitude() locationManager.get
Double myLocation[] = {locationManager.getLastLocation().getLatitude()
locationManager.getLastLocation().getLongitude()};
Map<String,String> customParameters = new HashMap<>();
customParameters.put("data[class_name]","MyDialog");
customParameters.put("data[location]", Arrays.toString(myLocation));
final QBDialog qbDialog = new QBDialog();
qbDialog.setName("Test");
qbDialog.setType(QBDialogType.GROUP);
qbDialog.setData(customParameters);
方法2:存储QBLocation
QBLocation qbLocation = new QBLocation(locationManager.getLastLocation().getLatitude(),
locationManager.getLastLocation().getLongitude());
Map<String,String> customParameters = new HashMap<>();
customParameters.put("data[class_name]","MyDialog");
customParameters.put("data[location]", qbLocation.toString());
final QBDialog qbDialog = new QBDialog();
qbDialog.setName("Test");
qbDialog.setType(QBDialogType.GROUP);
qbDialog.setData(customParameters);
要检索附近的对话,我有以下代码
Double[] myLocation = {locationManager.getLastLocation().getLatitude(),
locationManager.getLastLocation().getLongitude()};
final QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder();
requestBuilder.setPagesLimit(5);
requestBuilder.near("data[location]",myLocation,NEARBY_ROOMS_DISTANCE);
QBChatService.getChatDialogs(QBDialogType.GROUP, requestBuilder,
new QBEntityCallbackImpl<ArrayList<QBDialog>>() {
@Override
public void onSuccess(final ArrayList<QBDialog> dialogs, Bundle args) {
if(dialogs.size()!=0) {
roomsEvents.setEvent(RoomsEvents.ROOM_RETRIEVED);
roomsEvents.setQbDialog(dialogs.get(0));
EventBus.getDefault().post(roomsEvents);
}else{
roomsEvents.setEvent(RoomsEvents.NO_ROOMS_NEARBY);
EventBus.getDefault().post(roomsEvents);
}
}
@Override
public void onError(List<String> errors) {
roomsEvents.setEvent(RoomsEvents.RETRIEVE_ROOM_ERROR);
EventBus.getDefault().post(roomsEvents);
}
});
}
Double[]myLocation={locationManager.getLastLocation().getLatitude(),
locationManager.getLastLocation().Get经度()};
最终QBRequestGetBuilder requestBuilder=新QBRequestGetBuilder();
requestBuilder.setPagesLimit(5);
requestBuilder.near(“数据[位置]”,myLocation,near\u ROOMS\u DISTANCE);
QBChatService.getChatDialogs(QBDialogType.GROUP、requestBuilder、,
新的QBEntityCallbackImpl(){
@凌驾
成功时公共无效(最终ArrayList对话框、捆绑参数){
如果(dialogs.size()!=0){
roomsEvents.setEvent(检索roomsEvents.ROOM_);
setQbDialog(dialogs.get(0));
EventBus.getDefault().post(roomsEvents);
}否则{
roomsEvents.setEvent(roomsEvents.附近没有房间);
EventBus.getDefault().post(roomsEvents);
}
}
@凌驾
公共无效onError(列表错误){
setEvent(roomsEvents.RETRIEVE\u ROOM\u错误);
EventBus.getDefault().post(roomsEvents);
}
});
}
我的位置经理:
public class LocationManagement implements LocationManager {
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LocationRequest mLocationRequest;
@Inject
public LocationManagement(Context mContext){
this.mContext = mContext;
startGooglePlayServices();
}
private void startLocationUpdates(){
mLocationRequest = LocationRequest.create()
.setInterval(10000)
.setFastestInterval(5000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
shareOwnLocation(mLastLocation);
}
@Override
public void shareOwnLocation(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
QBLocation qbLocation = new QBLocation(lat, lng);
QBLocations.createLocation(qbLocation, new QBEntityCallbackImpl<QBLocation>() {
@Override
public void onSuccess(QBLocation qbLocation, Bundle bundle) {
LocationEvents locationEvent = new LocationEvents();
locationEvent.setEvent(LocationEvents.LOCATION_SHARED);
EventBus eventBus = EventBus.getDefault();
eventBus.post(locationEvent);
}
@Override
public void onError(List<String> list) {
// TODO
}
});
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public float distanceToMyLocation(Location destiny) {
return mLastLocation.distanceTo(destiny);
}
@Override
public Location buildLocation(double latitude, double longitude) {
Location mLocation = new Location("");
mLocation.setLatitude(latitude);
mLocation.setLongitude(longitude);
return mLocation;
}
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
shareOwnLocation(mLastLocation);
}else{
startLocationUpdates();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void startGooglePlayServices() {
this.mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void disconnectToGoogleApi() {
mGoogleApiClient.disconnect();
}
@Override
public void connectToGoogleApi() {
mGoogleApiClient.connect();
}
@Override
public Location getLastLocation() {
return mLastLocation;
}
}
公共类LocationManagement实现LocationManager{
私有上下文;
私人GoogleapClient MGoogleapClient;
私人场所;
私人位置请求mLocationRequest;
@注入
公共位置管理(上下文mContext){
this.mContext=mContext;
startGooglePlayServices();
}
private void startLocationUpdates(){
mLocationRequest=LocationRequest.create()
.setInterval(10000)
.SetFastTestInterval(5000)
.setPriority(位置请求。优先级高精度);
LocationServices.FusedLocationApi.requestLocationUpdates(MGoogleAppClient,
mLocationRequest,此);
}
@凌驾
已更改位置上的公共无效(位置){
mLastLocation=位置;
shareOwnLocation(mLastLocation);
}
@凌驾
公共无效shareOwnLocation(位置){
双纬度=location.getLatitude();
double lng=location.getLongitude();
QBLocation QBLocation=新QBLocation(lat,lng);
createLocation(qbLocation,新的QBEntityCallbackImpl(){
@凌驾
成功时公共无效(QBLocation QBLocation,Bundle Bundle){
LocationEvents locationEvent=新的LocationEvents();
locationEvent.setEvent(LocationEvents.LOCATION\u共享);
EventBus EventBus=EventBus.getDefault();
eventBus.post(locationEvent);
}
@凌驾
公共作废申报人(名单){
//待办事项
}
});
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
@凌驾
公共浮动距离到位置(位置){
返回mLastLocation.distanceTo(命运);
}
@凌驾
公共位置buildLocation(双纬度、双经度){
位置M位置=新位置(“”);
M位置设置纬度(纬度);
m位置。设置经度(经度);
返回位置;
}
@凌驾
未连接的公共空间(捆绑包){
mLastLocation=LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
如果(mLastLocation!=null){
shareOwnLocation(mLastLocation);
}否则{
startLocationUpdates();
}
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
public void startGooglePlayServices(){
this.mgoogleapclient=新的Googleapclient.Builder(mContext)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
@凌驾
public void disconnecttogoglapi(){
mGoogleApiClient.disconnect();
}
@凌驾
公共空间{
mGoogleApiClient.connect();
}
@凌驾
公共位置getLastLocation(){
返回位置;
}
}
在聊天室中存储位置类型并仍然能够检索附近房间的正确方法是什么?您在myLocation中的两个位置之间缺少一个逗号:
Double myLocation[] = {locationManager.getLastLocation().getLatitude()
locationManager.getLastLocation().getLongitude()};
致:
从我们的扩展讨论中,我认为第二次尝试是失败的,因为在查找位置时使用了QBLocation
另一个问题可能不是:
Double myLocation[] = {locationManager.getLastLocation().getLatitude()
locationManager.getLastLocation().getLongitude()};
Map<String,String> customParameters = new HashMap<>();
customParameters.put("data[class_name]","MyDialog");
customParameters.put("data[location]", Arrays.toString(myLocation));
@MrsEd,我添加了用于检索聊天记录的代码,它只是一个回调,使用库通知聊天记录已检索。而且,说清楚一点,我需要帮助将位置类型与QB对话框关联起来,因为我使用了这两种方法,但它们都存储了错误的位置数据。@MrsEd,我很确定问题不在于我的位置管理器,因为在检索聊天之前,我将当前位置存储在Quickblox服务器上,并以正确的纬度和经度存储。但是如果你想要的话,看看吧。我编辑了代码。没问题@MrsEd;-)。谢谢你的关注。你能发布你的REST请求日志吗?将进行分析并提出建议
Double myLocation[] = {locationManager.getLastLocation().getLatitude()
locationManager.getLastLocation().getLongitude()};
Double myLocation[] = {locationManager.getLastLocation().getLatitude(),
locationManager.getLastLocation().getLongitude()};
Double myLocation[] = {locationManager.getLastLocation().getLatitude()
locationManager.getLastLocation().getLongitude()};
Map<String,String> customParameters = new HashMap<>();
customParameters.put("data[class_name]","MyDialog");
customParameters.put("data[location]", Arrays.toString(myLocation));
String locationStr = locationManager.getLastLocation().getLatitude()+","+
locationManager.getLastLocation().getLongitude();
Map<String,String> customParameters = new HashMap<>();
customParameters.put("data[class_name]","MyDialog");
customParameters.put("data[location]", locationStr);