我正在尝试执行以下操作。
- 获取存储为字符串的经度和纬度,我可以
- 插入打开 Google 地图的可点击 URL(即http://maps.google.com/long=string1+lat=string2) - 此 URL 包含在通过默认电子邮件应用程序发送的电子邮件中。
我试过查看 URI、Linkify、Reverse GeoCoding,但我似乎什么也得不到。我可以获得坐标并显示为 Toast 通知,但目前尝试将它们保存以在其他地方使用是有问题的。
我有字符串 String lat = Double.toString(location.getLatitude) 但没有从中得到任何东西,或者在执行代码时可能没有坐标,因为它可能没有 GPS 锁定,不太确定。请看下面的代码。
公共类 ConfirmScreen 扩展 Activity{
String mapCoord = "http://maps.google.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_screen);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mLocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocListener);
sendEmail();
playSound();
}
public void backHome(View view)
{
Intent intent = new Intent (this, MainScreen.class);
startActivity(intent);
}
// Method to start playing and looping a sound.
public void playSound()
{
MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean soundCheck = sp.getBoolean("SOUND", false);
if (soundCheck)
{
clickSound.start();
}
}// method end
public void sendEmail()
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{emailValue});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. Please see the co-ords attached and run to their rescue!" +
" If you don't see any co-ords, they didn't check the box and assume you know where they are.\nKind Regards\nDon't Panic! \n\n" + mapCoord);
try
{ startActivity(Intent.createChooser(i, "Send mail...."));
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
}
}
//Location Listener
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String text = "Current Location is \nLat: " + location.getLatitude() + " \nLng: " + location.getLongitude();
//mapCoord = Double.toString(location.getLatitude()) + " " + Double.toString(location.getLongitude());
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
return true;
}
}