The world on your wrist: Google Maps API on Android Wear
May 28th, 2015 | Published in Google Maps
Android Wear is great for quick glanceable information that lets you stay present and be connected. For example, wouldn’t it be great if Android Wear could show you a map of your run or allow you to pick a place on the map for you and your friend to meet? Now developers can support these use cases directly with location detection and the Google Maps API on Android Wear.
You can already use the paired phone to detect location, or use on-device location detection for wearables that have an onboard GPS. Now, with Google Play Services 7.3, developers can use Google Maps API directly on Android Wear. You can now offer your users a wearable-tailored Google Maps experience.
Getting Started - Showing a Map
If you are familiar with the Maps API on Android, you will feel right at home with the Maps API for Android Wear. Your Maps API key for your mobile app will work for your Wear app as well, provided your apps share the same package name. To display a map, you can follow the easy steps in our training guide or explore our sample. There are endless possibilities for maps on wearables, including finding the nearest coffee shop, getting the fastest route home from work, tracking your run, and more.
One new thing you will need to think about with Maps on Wear is the way that users exit your application. On Android Wear, users can usually exit an application if they swipe from left to right. If you don’t need to include an interactive map, this swipe-to-dismiss approach should work for your app too. However, if you want to allow the user to pan the map in any direction, the maps component will automatically disable the swipe-to-dismiss gesture and your application will need to handle how users will exit the application. The recommended approach is to implement the standard long-press-to-dismiss gesture. Using the Android Wear support library, this is quite simple.
To implement long-press-to-dismiss:
1. Add a the DismissOverlayView from the Wearable UI Support Library to the layout and initialise it in the onCreate method of the activity
android:layout_height="match_parent" android:layout_width="match_parent"/> private DismissOverlayView mDismissOverlay;
@Override public void onCreate(Bundle savedState) { ... mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay); // Introduce this gesture to the user if necessary mDismissOverlay.setIntroText (R.string.basic_wear_long_press_intro); mDismissOverlay.showIntroIfNecessary(); ... }
2. Implement GoogleMap.OnMapLongClickListener for your activity and override the method. This will display the dismiss overlay with an option to exit your app:
@Override public void onMapLongClick(LatLng latLng) { mDismissOverlay.show(); }3. Assign the activity as the listener in onMapReady:
@Override public void onMapReady(GoogleMap googleMap) { ... mMap.setOnMapLongClickListener(this); ... }Connecting People
In addition to displaying a map, another possible use for Maps on Android Wear is to pan a map to a specific location so that, for example, your friends can meet you at that specific location. In this case, you should add a marker and use GoogleMap.OnCameraChangeListener.
1. Add an image in the center of the screen on top of the map. This indicates the selected location within the MapFragment element:
2. Implement GoogleMap.OnCameraChangeListener interface and override the following method:
@Override public void onCameraChange(CameraPosition cameraPosition) { // Access the center of the camera through: // cameraPosition.target.latitude and // cameraPosition.target.longitude }3. Attach the listener to the Maps fragment:
@Override public void onMapReady(GoogleMap googleMap) { ... mMap.setOnCameraChangeListener(this); ... }Living in the Real World
Android Wear is designed for users to stay connected while on the move. With the Google Maps API on Android Wear, developers can use the power of Google Maps to connect their users to what’s around them. Let’s keep our users moving... in the real world.
Posted by Hoi Lam, Developer Advocate at Google