我想将我当前的纬度、经度文本转换为语音。我有查找当前纬度、经度的代码。但是我在 Location 类中初始化了 TextToSpeech 方法,我只有 latLongString。我无法得到 EX:你当前的位置是 lat= 1.222,长=22.335。
这是我的代码:
public class SpeakActivity extends Activity implements OnInitListener{
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
tts = new TextToSpeech(this, this);
updateWithNewLocation(location);
}
public void speak(String text2say){
tts.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onInit(int status) {
say("latLongString");
}
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocation;
myLocation= (TextView) findViewById(R.id.myLocation);
if(location!=null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lon;
}else{
latLongString="no location found";
}
myLocation.setText("Your current position is:\n" + latLongString);
speak(latLongString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_speak, menu);
return true;
}
@Override
public void onDestroy() {
if (tts!= null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}}