SimpleSample-location users-android

Ask tech team
From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

Contents

Sources

Project homepage on GIT — https://github.com/QuickBlox/quickblox-android-sdk/tree/master/sample-location

Download ZIP - https://github.com/QuickBlox/quickblox-android-sdk/archive/master.zip


Overview

This sample demonstrates how to work with QuickBlox Location API.
It allows you to see other users on the Map and share your own position.

It shows you how to:

  1. Retrieve all users' points & add them to the Map
  2. Share your own location


Setup

If you want to use Google Maps Android Api v2 you need to perform next steps:

  1. Create SHA1 fingerprint of your debug or release key
  2. Open Google Console
  3. Create project… → Enter name of the project, e.g. “QB Location sample”
  4. Add Google Maps Android API v2 as a service for the project
  5. Select “API Access” at the left navigation bar and press “Create new Android key...” at the bottom.
  6. Enter “<SHA1>;<package>”, e.g.

    BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample

  7. Press Create.
  8. Add generated key to AndroidManifest.xml
 <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your_api_key"/>

Guide: Getting Started with Location API

Getting a QuickBlox account

http://admin.quickblox.com/register

Creating an application on the Admin panel

http://admin.quickblox.com/apps/new

For further reading there is also this 5 minute guide.

Adding Location features to your application

The Location API provides 2 main features:

  • People - see/show users over map — stores locations and status messages for each user. Connect app users with each other and their environment. Using GPS locations, map integrations and local data, end users can literally 'see' each other on the map and engage with other users.
  • Places — supports POIs (which could be buildings, attractions, events venues, check-in places) or could also be any virtual world object. Places can have profile data along with a photo.

People

Check-in

In order to check-in you must be logged in to act on the user's behalf - please refer to Android Users API documentation.

Just use this code snippet below if you want to check-in to somewhere:

QBLocation location = new QBLocation(23.41422, 12.436346, "I'm at PIzzeria";
QBLocations.createLocation(location, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Check-in result
        if (result.isSuccess()) {
            QBLocationResult locationResult = (QBLocationResult) result;
            Log.d(TAG, "Your checkin: " + locationResult.getLocation());
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

After that other users will be able to see your position.

Retrieving checkins

Through filters

There are lots of filters & options that you can use to retrieve checkins. Follow QBLGeoDataGetRequest class documentation, which describes all posible options.

For example, let's retrieve the last 100 checkins, only the last user's checkin, with statuses, sorted by the createdAt field:

QBLocationRequestBuilder getLocationsBuilder = new QBLocationRequestBuilder();
getLocationsBuilder.setPerPage(100);
getLocationsBuilder.setLastOnly();
getLocationsBuilder.setHasStatus();
getLocationsBuilder.setSort(SortField.CREATED_AT);
QBLocations.getLocations(getLocationsBuilder, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Retrieve checkins result
        if (result.isSuccess()) {
            QBLocationPagedResult locationsResult = (QBLocationPagedResult) result;
            for (QBLocation location : locationsResult.getLocations()) {
                Log.d(TAG, "Your checkin: " + location);
            }
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});
Through ID

You can also retrieve particular checkins through their ID:

QBLocations.getLocation(34706, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Update checkin result
        if (result.isSuccess()) {
            QBLocationResult locationResult = (QBLocationResult) result;
            Log.d(TAG, "Your checkin: " + locationResult.getLocation());
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

Updating checkins

If you want to update one of your checkins - just use this code snippet below:

QBLocation location = new QBLocation(13.2344, -42.23523, "I'am at Pizzzza house!");
location.setId(34638);
QBLocations.updateLocation(location, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Get checkin result
        if (result.isSuccess()) {
            QBLocationResult locationResult = (QBLocationResult) result;
            Log.d(TAG, "Your checkin: " + locationResult.getLocation());
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

Deleting Checkins

There are 2 ways to delete checkins:

  • Deleting a particular checkin by ID
  • Deleting checkins that were created in a period of the last n days
Through ID
QBLocations.deleteLocation(34640, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Delete checkin result
        if (result.isSuccess()) {
            Log.d(TAG, "Checkin was deleted successfully");
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});
Mass delete
// delete all checkins that were created in period of last 2 days
QBLocations.deleteObsoleteLocations(2, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Delete checkins result
        if (result.isSuccess()) {
            Log.d(TAG, "Checkin was deleted successfully");
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

Places

Creating a Place

There are 2 ways to create a place:


Creating a Place through the Admin panel

Just go to admin.quickblox.com, find the Location module page, then the Places tab and press the Add new place button. The 'add a new place' page will be opened. Pick a location, set a title, description, connect a photo and press the Add button. the new place will be created & shown in the table.

Bulk uploads of places is supported as well.

Creating a Place through the Android SDK

In order to create a place you must be logged in and to act on a user's behalf - please refer to Android Users API documentation.

// 447 - this is ID of file in Content module
QBPlace place = new QBPlace(34691, "My place title", "London, Gadge st, 34", "London, Gadge st, 34", 447);
QBLocations.createPlace(place, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Create Place result
        if (result.isSuccess()) {
            QBPlaceResult placeResult = (QBPlaceResult) result;
            Log.d(TAG, "Your checkin: " + placeResult.getPlace());
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

Retrieving places

There are 2 ways to retrieve places:

  • Retrieving all places
  • Retrieving a particular place through it's ID
Retrieving all places
QBLocations.getPlaces(new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Get Places result
        if (result.isSuccess()) {
            QBPlacePagedResult placeResult = (QBPlacePagedResult) result;
            for (QBPlace place : placeResult.getPlaces()) {
                Log.d(TAG, "Your checkin: " + place);
            }
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});
Retrieving a Place through it's ID
QBLocations.getPlace(1190, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Retrieve Place with ID result
        if (result.isSuccess()) {
            QBPlaceResult placeResult = (QBPlaceResult) result;
            Log.d(TAG, "Your checkin: " + placeResult.getPlace());
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

Updating a Place

If you want to update one of your places - just use the code snippet below:

QBPlace place = new QBPlace(34691, "Lime cafe", "Cool, Fresh place");
QBLocations.updatePlace(place, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Update Place result
        if (result.isSuccess()) {
            QBPlaceResult placeResult = (QBPlaceResult) result;
            Log.d(TAG, "Your checkin: " + placeResult.getPlace());
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
});

Deleting a Place

QBLocations.deletePlace(1230, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        // Delete Place with ID result
        if (result.isSuccess()) {
            Log.d(TAG, "Place was deleted successfully");
        } else {
            Log.e(TAG, "Errors: " + result.getErrors());
        }
    }
}, this);

Comments

Feel free to comment on this page using the form below.

blog comments powered by Disqus