dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.support:wearable:1.2.0' compile 'com.google.android.gms:play-services:7.5.0'}
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) wearApp project(':wear') compile 'com.google.android.gms:play-services:7.5.0' compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.firebase:firebase-client-android:2.3.0+'}
// Setup onclick event listener for the mapmMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { // Logic to add markers to the map on click. … LatLng origin = (LatLng) bikeMarkerList.get(0).getPosition(); LatLng dest = (LatLng) bikeMarkerList.get(1).getPosition(); // Get URLs for the Directions API String url = getDirectionsUrl(origin, dest); // Create an AsyncTask to fetch JSON data from Directions API DirectionsDownloadTask downloadTask = new DirectionsDownloadTask(); downloadTask.execute(url); }});
// Building the url to the Directions API web serviceprivate String getDirectionsUrl(LatLng origin,LatLng dest){// String for the origin of the routeString str_origin = "origin="+origin.latitude+","+origin.longitude;// String for the destination of the routeString str_dest = "destination="+dest.latitude+","+dest.longitude;// Enable bicycling modeString mode = "mode=bicycling";// We build the parameters for our URL stringString parameters = str_origin+"&"+str_dest+"&"+mode;// Construction of the entire URL to the Directions API. // IMPORTANT: Notice how we proxy the requests through a web server to// protect your API key.String url = "https://<YOUR_PROXY_SERVER>/directions?"+parameters;return url;}
protected void onPostExecute(String directionsJSON) { super.onPostExecute(directionsJSON); try { JSONObject jObject = new JSONObject(directionsJSON); DirectionsJSONParser parser = new DirectionsJSONParser(); // Parse the data. directionsResult is a List of decoded poly directionsResult = parser.parse(jObject); // This is the encoded polyline to pass on the Elevation API call String overviewPolyline = parser.getEncodedPoly(jObject); //Now that we have route, we need to get the Elevation data. ElevationDownloadTask elevationDownloadTask = new ElevationDownloadTask(); String url = getElevationUrl(overviewPolyline); elevationDownloadTask.execute(url); } catch (Exception ex) { ex.printStackTrace(); }}
private String getElevationUrl(String encodedPolyline) { // Build parameters to the web service String parameters = "locations=enc:" + encodedPolyline; // Build URL to the web service String url = "https://<YOUR_PROXY_SERVER>/elevation?" + parameters; return url;}
protected void onPostExecute(String elevationJSON) { super.onPostExecute(elevationJSON); try { JSONObject jObject = new JSONObject(elevationJSON); ElevationJSONParser parser = new ElevationJSONParser(); // Start parsing data elevationResult = parser.parse(jObject); // We use later send this to the Wearable device to recreate poly. String routeForWearable = ""; // Go through all segments for (int i = 1; i < directionsResult.size(); i++) { LatLng prevPosition = directionsResult.get(i - 1); LatLng position = directionsResult.get(i); double prevElevation = Double.valueOf(elevationResult.get(i - 1)); double elevation = Double.valueOf(elevationResult.get(i)); double elevationDiff = elevation - prevElevation; // Get color based on elevation change. // Green --> Red (and vice versa) gradient logic. int color = getColorByElevChange(elevationDiff); // Create the polyline segment. PolylineOptions polyOptions = new PolylineOptions() .add(prevPosition, position) .color(color) .width(8); // Draw polyline for segment mMap.addPolyline(polyOptions); // We maintain this String variable to pass over via the DataApi to the Wearable // app. From there we parse the response and also create the polyline there. routeForWearable += prevPosition.latitude + "," + prevPosition.longitude + ":" + position.latitude + "," + position.longitude + ":" + color + "|"; } // Here we send over the polyline string to the Wearable device. PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/route"); putDataMapReq.getDataMap().putString(ROUTE_TAG, routeForWearable); PutDataRequest putDataReq = putDataMapReq.asPutDataRequest(); PendingResult pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq); } catch (Exception e) { e.printStackTrace(); }}
public void onDataChanged(DataEventBuffer dataEvents) {… // Initialization logicfor (DataEvent event : dataEvents) { if (event.getType() == DataEvent.TYPE_CHANGED) { // DataItem changed DataItem item = event.getDataItem(); if (item.getUri().getPath().compareTo("/route") == 0) { DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap(); String routeFromMobile = dataMap.getString(ROUTE_TAG); // Go back to main UI thread and draw polyline // Broadcast message to wearable activity for display Intent messageIntent = new Intent(); messageIntent.setAction(Intent.ACTION_SEND); messageIntent.putExtra(ROUTE_TAG, routeFromMobile); LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent); } } else if (event.getType() == DataEvent.TYPE_DELETED) { // DataItem deleted }}}
public class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String routeFromMobile = intent.getStringExtra(ROUTE_TAG); // Draw the elevation polyline based on parsing the String. drawElevationPolyline(routeFromMobile); }}
Give us feedback in our Product Forums.