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

Check-in

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 QBEntityCallback<QBLocation>() {
    @Override
    public void onSuccess(QBLocation qbLocation, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

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

Check-in with push to near users

You can checkin and notify all users who are near you with push notification:

double latitude = 25.3433;
double longitude = -5.3454;
String status = "Checked here!";
//
final QBLocation location = new QBLocation(latitude, longitude, status);
 
QBLocations.createLocation(location, new QBEntityCallback<QBLocation>() {
    @Override
    public void onSuccess(QBLocation qbLocation, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
}, "Your friend is near!", QBEnvironment.DEVELOPMENT, 1000);

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.

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 QBEntityCallback<ArrayList<QBLocation>>() {
    @Override
    public void onSuccess(ArrayList<QBLocation> locations, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Through ID

You can also retrieve particular checkins through their ID:

QBLocations.getLocation(34706, new QBEntityCallback<QBLocation>() {
    @Override
    public void onSuccess(QBLocation qbLocation, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Update 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 QBEntityCallback<QBLocation>() {
    @Override
    public void onSuccess(QBLocation qbLocation, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Delete 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 QBEntityCallback<Void>() {
    @Override
    public void onSuccess() {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Mass delete

// delete all checkins that were created in period of last 2 days
int days = 2;
 
QBLocations.deleteObsoleteLocations(2, new QBEntityCallback<Void>() {
    @Override
    public void onSuccess() {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});