如何将位置数据添加到 Quickblox 对话框?

How to add a location data to Quickblox dialog?

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

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

方法一:存储双精度[]

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

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

您在 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"

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