我想存储一个带聊天的位置类型,以便我可以根据位置搜索对话框,因此我使用Quickblox管理面板创建了一个名为 MyDialog 的自定义对象。此自定义对象只有一个名为位置的字段位置。
我尝试了两种方法来保存位置数据和对话框,但两者都给我带来了问题:
方法1:存储双[]
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
这种方法的问题在于创建的对话框始终具有纬度&#39; 0&#39; 。以下是对话框创建响应:
{
"_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;
}
}
通过聊天存储位置类型并仍能检索附近房间的正确方法是什么?
答案 0 :(得分: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))
将包含&#34; [lat,long]&#34;
lat和long需要格式化为&#34; lat,long&#34;
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