如何获取谷歌地图步行导航指令及语音导航关键词转为字符串?
Hey there! I've got a couple of solid solutions for grabbing those walking directions from Google Maps into string format—including extracting key terms like left and right—and fixing that background speech recognition issue you ran into. Let's break them down:
1. Use the Google Maps Directions API (Recommended)
This is the cleanest approach because it avoids the mess of speech-to-text entirely. Instead of trying to capture audio from the app, you pull structured, text-based directions directly from Google's API. Here's how it works:
- First, sign up for a Google Cloud API key and enable the Maps Directions API in your project.
- When you call the API, specify
walkingas your travel mode, plus your start and end coordinates. - Parse the JSON response: look for the
legs -> stepsarray. Each step includes two useful fields:maneuver: This gives you standardized direction keywords liketurn-left,turn-right, orstraight—perfect for quick keyword extraction.html_instructions: This is the full human-readable instruction (e.g., "Turn left onto Main Street").
Here's a quick Python example to get you started:
import requests # Replace with your actual API key API_KEY = "your_google_cloud_api_key" origin = "37.7749,-122.4194" # Example: San Francisco destination = "37.7895,-122.401" # Example: Fisherman's Wharf mode = "walking" # Build the API request URL directions_url = f"https://maps.googleapis.com/maps/api/directions/json?origin={origin}&destination={destination}&mode={mode}&key={API_KEY}" response = requests.get(directions_url).json() # Extract and process each step if "routes" in response and len(response["routes"]) > 0: for step in response["routes"][0]["legs"][0]["steps"]: # Get the standardized maneuver keyword maneuver = step.get("maneuver", "straight") # Get the full instruction text full_instruction = step["html_instructions"] print(f"Direction: {maneuver} | Full Instruction: {full_instruction}")
2. Android Accessibility Service (For Foreground Google Maps)
If you absolutely need to work with the Google Maps app running in the foreground (instead of using the API), you can use Android's Accessibility Service to capture the on-screen navigation text (Google Maps displays directions as text at the bottom of the screen, alongside audio). This avoids needing to handle audio at all.
- The core idea: Accessibility Services let you monitor UI changes in other apps. You can target Google Maps specifically, listen for the text elements that show navigation instructions, and extract the text from there.
- Steps to implement:
- Create a custom class that extends
AccessibilityService. - In the
onAccessibilityEventmethod, filter for Google Maps' package name (com.google.android.apps.maps). - Traverse the UI nodes to find the TextView elements that display navigation instructions.
- Extract the text and use simple string checks or regex to pull out
left,right, etc.
- Create a custom class that extends
Here's a snippet of what that might look like in Java:
@Override public void onAccessibilityEvent(AccessibilityEvent event) { // Only listen to events from Google Maps if ("com.google.android.apps.maps".equals(event.getPackageName())) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); if (rootNode != null) { // Find nodes that contain direction text (adjust the search term as needed) List<AccessibilityNodeInfo> directionNodes = rootNode.findAccessibilityNodeInfosByText("Turn"); for (AccessibilityNodeInfo node : directionNodes) { String instructionText = node.getText().toString(); // Extract keywords if (instructionText.contains("left")) { // Handle "left" instruction Log.d("Navigation", "Turn left detected!"); } else if (instructionText.contains("right")) { // Handle "right" instruction Log.d("Navigation", "Turn right detected!"); } node.recycle(); // Don't forget to recycle nodes to avoid memory leaks } rootNode.recycle(); } } }
Why Background Speech Recognition Won't Work
Just to clarify why your initial approach hit a wall: Both Android and iOS restrict background access to the microphone for privacy reasons. Plus, Google Maps doesn't expose its audio output stream to third-party apps, so capturing the navigation audio directly isn't feasible. The two methods above are far more reliable.
内容的提问来源于stack exchange,提问作者shivam kumar rajput

