【问题标题】:How to add a location data to Quickblox dialog?如何将位置数据添加到 Quickblox 对话框?
【发布时间】:2015-11-15 06:59:11
【问题描述】:

我想在聊天中存储位置类型,以便可以根据位置搜索对话,因此我使用 Quickblox 管理面板创建了一个名为 MyDialog 的自定义对象。此自定义对象只有一个名为 location 的字段,类型为 Location

我尝试了两种使用对话框保存位置数据的方法,但都给我带来了问题:

方法 1:存储一个 Double[]

    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);

问题 1

这种方法的问题是创建的对话框总是有纬度'0'。以下是对话框创建响应:

{
    "_id":"56448a1aa28f9ad7af000164",
    "created_at":"2015-11-12T12:46:18Z",
    "data":{"_qb_location":{"type":"Point","coordinates":[0,-42.6159729]},"class_name":"MyDialog"}, (...) }

方法 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);

问题 2

这种方法的问题是创建的对话框总是有坐标:[0,0]。以下是对话框创建响应:

{
    "_id":"56448a1aa28f9ad7af000164",
    "created_at":"2015-11-12T12:46:18Z",
    "data":{"_qb_location":{"type":"Point","coordinates":[0,0]},"class_name":"MyDialog"}, (...) }

要检索附近的对话,我有这个代码

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);

                }

            });
    }

我的位置经理

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;
}
}

使用聊天存储位置类型并仍然能够检索附近房间的正确方法是什么?

【问题讨论】:

  • @MrsEd,我添加了用于检索聊天的代码,它只是一个使用EventBus 库通知已检索聊天的回调。而且,需要说明的是,我需要帮助将 Location 类型与 QBDialog 相关联,因为我使用了这两种方法,但它们都错误地存储了位置数据。跨度>
  • @MrsEd,我很确定问题不在于我的位置管理器,因为在检索聊天记录之前,我将当前位置存储在 Quickblox 服务器上,并以正确的纬度和经度。但是如果你想要的话,看看吧。我编辑了我的代码。
  • 没问题@MrsEd ;-)。并感谢您的关注。
  • 您可以发布您的 REST 请求日志吗?将分析并返回建议

标签: android quickblox


【解决方案1】:

您在 myLocation 中的双打之间缺少逗号:

Double myLocation[] = {locationManager.getLastLocation().getLatitude()
        locationManager.getLastLocation().getLongitude()};

收件人:

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));

Arrays.toString(myLocation)) 将包含“[lat,long]”

lat和long需要格式化为“lat,long”

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);

https://github.com/QuickBlox/quickblox-android-sdk/issues/29

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多