0

我正在尝试执行以下操作。

- 获取存储为字符串的经度和纬度,我可以

- 插入打开 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;
}
}
4

1 回答 1

0

在您的代码中,您将 mapCoord 设置为"http://maps.google.com". 然后将其添加到您的电子邮件文本中。我还注意到您在以下代码中注释掉了这一行onLocationChanged()

//mapCoord =  Double.toString(location.getLatitude()) + " " +
                  Double.toString(location.getLongitude());

您可以通过这种方式获取用户的坐标,但问题是位置更新回调是异步的。你打电话时

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocListener);

这只是注册您MyLocationListener接收回调。在用户的位置发生变化之前,它实际上不会生成回调。但你不要等待。您立即发送电子邮件。在大多数情况下,该位置将为空,因为您的回调尚未被回调。

您可以尝试使用LocationManager.getLastKnownLocation()(它会立即返回最后一个已知位置),但根据我的经验,它通常返回 null。

您还可以注册更新,打开一个进度对话框告诉用户“正在检测您的位置......”并等到onLocationChanged()被回调。

于 2012-09-27T14:55:48.867 回答