Reader small image

You're reading from  Android Wear Projects

Product typeBook
Published inJul 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787123229
Edition1st Edition
Languages
Right arrow
Author (1)
Ashok Kumar S
Ashok Kumar S
author image
Ashok Kumar S

Ashok Kumar S has been working in the mobile development domain from about eight years. He is a Google certified engineer, speaker at global scale conferences and he also runs a youtube channel (AndroidABCD) for Android developers. He is an early Firebase adopter since before Google acquired Firebase. He is Computer Science and Engineering graduate who is completely passionate about innovation in technology. He contributes to open source heavily to improve his e-karma. Ashok has also written a book on Wear OS programming through a project approach.
Read more about Ashok Kumar S

Right arrow

Chapter 6. Ways to Get Around Anywhere - WearMap and the GoogleAPIclient

A Map is a visual representation of an area or a part of an area.

We humans travel to different cities; they could be domestic or international cities. How about tracking the places you visit? We all use maps for different reasons, but in most cases, we use maps to plan a particular activity, such as outdoor tours, cycling, and other similar activities. Maps influence human intelligence to find the fastest route from the source location to the destination. In this project, we will build a Wear application that works with the Google Maps service.

For record, Google Maps started as a C++ desktop program in October, 2004. Google Maps officially released in February, 2005. Google Maps offers an API that allows maps to be embedded in third-party applications; Google Maps offers aerial and satellite views of many places. Google Maps is the best compared to other map services; maps are optimize and its accuracy rate is very...

Let's get started with creating WearMap


We now know how to create a application. In case if you are following this project directly without following Wear-note application which is covered in Chapter 2, Let us Help Capture What is on Your Mind - WearRecyclerView and More and Chapter 3, Let us Help Capture What is on Your Mind - Saving Data and Customizing the UI. Please do follow the Wear-note application to learn more about standalone applications.

Let's call this project WearMapDiary, since we store the locations and details about the location. The project package address up to the developer; in this project, the package address is com.packt.wearmapdiary and the API level 25 Nougat. In the activity template, select the Google Maps Wear Activity, as follows:

Select Google Maps Wear Activity template from the activity chooser

Once the project is created, we will see the necessary configuration for the project, which includes the map fragment being added; it would have set the DismissOverlays...

The Google API console


The Google API console is a web portal that allows developers to manage Google services for their project and it can be at https://console.developers.google.com.

  1. Visit the developer console with your Google account. Create a project packt-wear or something that is convenient for developers:
  1. After creating the project successfully, go to the API Manager | Library section and enable the Google Maps Android API:
  1. Click on the Enable button for enabling Maps for Android:
  1. After enabling the API in the console facility, we need to create the API key with the development machine's SHA1 and the project's package address, as follows:
  1. To get your machine's SHA1 fingerprint, open Android Studio. On the right-hand side of Android Studio, you will see the Gradle project menu. Then, follow these steps:
    1. Click on Gradle (on the right-hand side panel, you will see the Gradle Bar)
    2. Click on Refresh (click on Refresh; on the Gradle Bar, you will see a List of Gradle scripts for your project...

Configuring SQLite and saving the markers


Persisting all the necessary data is the fundamental use case for any good software. Android SDK provides an SQLite storage solution built in. It has a very small footprint and is very fast. If a is familiar with SQL queries operations, SQLite is going to be easy and delightful to work with.

Schema and contract

Essentially, for a database, we need to create a data schema, which is a formal declaration of how the is organized. The is reflected in the SQLite query statements. A contract class is a for constants that define names for URIs, tables, and columns. The class allows the use of the same constants across all the other classes in the same package.

For the scope of WearMapDiary, we will create all the instances in the DBHelper class. Now, let's create the DBhelper class, which opens and connects the application to SQLite and processes the query:

public class DbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME...

Saving data in SQLite


To connect SQLite to the and to save the data in SQLite, implement the activity LoaderManager.LoaderCallbacks<Cursor> and instatiate the datasource in the onCreate method:

mDataSource = new MemoriesDataSource(this);
getLoaderManager().initLoader(0,null,this);

Implement the callback methods for the LoaderManager.LoaderCallbacks<Cursor> interface:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return null;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

}

Now, refactor the addingMarker code in a method as follows:

private void addMarker(Memory memory) {
    Marker marker = mMap.addMarker(new MarkerOptions()
            .draggable(true)
            .position(new LatLng(memory.latitude, memory.longitude)));

    mMemories.put(marker.getId(), memory);
}

We still need to work with dragging the marker for future implementation...

Detecting your application from another device


In CapabilityAPI, your Wear can the corresponding mobile application for a Wear application. Wear devices can advertise their events to the paired device spontaneously and statically as well. For checking the advertised capabilities of paired Wear devices, check this link for information: https://developer.android.com/training/wearables/data-layer/messages.html#AdvertiseCapabilities.

Note

Note that not all the phones support the Play store (such as iPhones, and so on). This section describes the best practices for these scenarios: your standalone watch app needs your phone app and your phone app needs your standalone watch app.

Specifying capability names to detect your apps

For the app to each device type (watch or phone), specify a unique string for the capability name in the res/values/wear.xml file. For example, in your mobile module, the wear.xml file could include the following code in Wear and mobile modules:

<resources>
<string...

Keeping your application active on a Wear device


When we write an application for contexts, we need to do certain alternatives. We know that when not using the application, we should make that app to sleep in Wear devices for better battery performance, but when we are building an application for maps, it is necessary that maps be visible and active for the user.

Android has a simple configuration for this: a method with few lines that activates the ambient mode:

//oncreate Method
setAmbientEnabled();

This starts the ambient mode on the map. The API swaps to a non-interactive and low-color rendering of the map when the user is no longer actively using the app:

@Override
public void onEnterAmbient(Bundle ambientDetails) {
    super.onEnterAmbient(ambientDetails);
    mMapFragment.onEnterAmbient(ambientDetails);
}

The following code exits the ambient mode on the WearMap. The API swaps to the normal rendering of the map when the user starts actively using the app:

@Override
public void onEnterAmbient...

Understanding fully interactive mode and lite mode


The Google Maps android API can static as light mode maps.

Adding Lite mode to Android Maps is similar to configuring the normal maps, because it will use the same classes and interfaces. We can set Google Maps to the Lite mode in the following two ways:

  • As an XML attribute to your MapView or MapFrgament
  • Using the GoogleMapOptions object
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraZoom="13"
    map:mapType="normal"
    map:liteMode="true"/>

Or, using the GoogleMapOptions object as follows:

GoogleMapOptions options = new GoogleMapOptions().liteMode(true);

Interactive mode allows the to use all the methods, including onCreate(), onDestroy(), onResume(), and onPause(), and all the...

Summary


Here, we are at the chapter's end, looking forward to what improvements we can do in the WearMapDiary app. Now, we know about creating a MapsActivity, setting up the maps and Google API key, configuring Google Play services in a Wear emulator, runtime permissions check, checking for the GPS hardware, and fetching the location name using the geocoder class. We have understood the concept of the interactive mode and Lite mode for maps. In the next chapter, let's understand more Wear and map UI controls and other Google Map technologies, such as streetview, changing the map types, and so on.

 

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Wear Projects
Published in: Jul 2017Publisher: PacktISBN-13: 9781787123229
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Ashok Kumar S

Ashok Kumar S has been working in the mobile development domain from about eight years. He is a Google certified engineer, speaker at global scale conferences and he also runs a youtube channel (AndroidABCD) for Android developers. He is an early Firebase adopter since before Google acquired Firebase. He is Computer Science and Engineering graduate who is completely passionate about innovation in technology. He contributes to open source heavily to improve his e-karma. Ashok has also written a book on Wear OS programming through a project approach.
Read more about Ashok Kumar S