Search code examples
androidquickblox

How to add a location data to Quickblox dialog?


I would like to store a location type with a chat so I could search dialogues based on a location, so I created a Custom Object called MyDialog using the Quickblox Admin Panel. This custom object has only one field called location of type Location.

I tried two approaches to save the location data with the dialog, but both gave me problems:

Approach 1: Store a 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);

Problem 1

The problem with this approach is that the created dialog always have latitude '0'. Here are the dialog creation response:

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

Approach 2: Store a 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);

Problem 2

The problem with this approach is that the created dialog always have coordinates : [0,0]. Here are the dialog creation response:

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

To retrieve the nearby dialogues I have this code

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

                }

            });
    }

My Location Manager:

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

Whats the correct way to store a location type with a chat and still be able to retrieve the nearby rooms?


Solution

  • You are missing a comma in between your doubles in myLocation:

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

    To:

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

    From our extended discussion, I think the second attempt is failing due to using QBLocation, when you are looking for Location.

    Another problem may be instead of:

    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)) will include "[lat,long]"

    The lat and long needs to be formatted as "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