这是实现此目的的演示实现。
package demo.maps.texttospeech; import android.speech.tts.TextToSpeech; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import java.util.Locale; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private TextToSpeech textToSpeech; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { int ttsLang = textToSpeech.setLanguage(Locale.US); if (ttsLang == TextToSpeech.LANG_MISSING_DATA || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) { Log.e("TTS", "The Language is not supported!"); } else { Log.i("TTS", "Language Supported."); } Log.i("TTS", "Initialization success."); } else { Toast.makeText(getApplicationContext(), "TTS Initialization failed!", Toast.LENGTH_SHORT).show(); } } }); } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")).setSnippet("Very nice food here"); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { String data = marker.getTitle() + " " + marker.getSnippet(); Log.i("TTS", "button clicked: " + data); int speechStatus = textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null); if (speechStatus == TextToSpeech.ERROR) { Log.e("TTS", "Error in converting Text to Speech!"); } return false; } }); } }
重要的部分是添加setOnMarkerClickListener以获取标记点击事件,然后将您设置为标记的字符串构建为标题和/或片段。
并在onCreate中初始化TextToSpeech对象并最终调用 textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null); 在标记的单击侦听器中构建字符串之后。
textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null);
如果有效,请竖起大拇指!
干杯!