0

在我想要开发的 android 应用程序中,我希望用户可以找到他们的位置。为此,我在 MainActivity 中有此代码,但在设备上(当我运行它时)它找不到纬度经度和地址。为什么?

public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private TextView addressField; //Add a new TextView to your activity_main to display the address
private LocationManager locationManager;
private String provider;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    addressField = (TextView) findViewById(R.id.TextView05); //Make sure you add this to activity_main


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);


    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
    } else {
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
    }
}


@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}


@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {

    double lat = location.getLatitude();
    double lng = location.getLongitude();

    Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
    StringBuilder builder = new StringBuilder();
    try {
        List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
        int maxLines = address.get(0).getMaxAddressLineIndex();
        for (int i=0; i<maxLines; i++) {
            String addressStr = address.get(0).getAddressLine(i);
            builder.append(addressStr);
            builder.append(" ");
        }

        String fnialAddress = builder.toString(); //This is the complete address.

        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
        addressField.setText(fnialAddress); //This will display the final address.

    } catch (IOException e) {}
    catch (NullPointerException e) {}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {


}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

}

4

3 回答 3

1

注意:这是一个旧答案,无助于解决特定问题。但是,它是有价值的信息,所以我不会删除它。

闪烁的 GPS 图标是一个好兆头。这意味着您的应用程序正在向操作系统询问位置并且操作系统尝试获取它。

闪烁表示操作系统没有完成通过 GPS 获取位置。如果此问题持续存在,例如超过 1 或 2 分钟,则可能有以下原因:

  • 您没有收到 GPS 信号(例如,因为您在墙壁太厚的建筑物中)。
  • 您没有与互联网的数据连接(在某些版本的手机和/或安卓系统上,如果没有数据连接,GPS 将无法工作。听起来很愚蠢,但这是真的。我是这样一部手机的自豪拥有者。)
  • 还有一些其他错误会导致您的 GPS 处于不再生成任何位置更新的状态。这有时发生在我身上,我不知道更多的背景信息。重新启动手机后,它总是再次工作。
于 2013-08-14T23:37:52.353 回答
0

我的猜测是没有启用最好的提供者。尝试调用 getBestProvider(criteria, true) 也 Log.d 提供程序,您可以使用 isProviderEnabled(provider) 查看提供程序是否已启用。

于 2013-08-14T23:26:29.283 回答
0

我已经编译了您的示例并在 Galaxy S2 上对其进行了测试。以下是我的发现:

您的示例中有用于获取位置的两种不同方法的代码。一种方法是LocationManager.getLastKnownLocation(...)直接获取位置,另一种方法是实现 LocationListener 接口并注册位置更新,以便稍后获得有关新位置更新的通知。

前期信息:我让第二种方法工作正常,但我没有让使用 getLastKnownLocation 方法的方法可靠地工作。

位置获取不起作用,因为作为提供者,"network"返回。尽管 GPS 已打开,但仍会发生这种情况。随后的效果是,因为我关闭了网络位置提供程序,所以 getLastKnowLocation 方法返回 null ,如此处所述:“如果提供程序当前被禁用,则返回 null 。” (来自getLastKnownLocation

您可以通过更改来解决此问题

provider = locationManager.getBestProvider(criteria, false);

provider = locationManager.getBestProvider(criteria, true);

.

这将为您提供 GPS 提供商,如果它可用(打开)。但是,该getLastKnownLocation(...)方法仍然为我返回 null,尽管选择了正确的提供程序 (gps) 并且提供程序可用。这意味着 getLastKnownLocation 方法的文档在返回 null 时缺少有关另一种情况的信息。这似乎是因为没有为此提供者保存最后一个已知位置。您在启动应用程序时无法知道是否是这种情况,因此您不能依赖此方法在应用程序启动时返回非空值。

好消息是:现在我们获得了正确的位置提供程序,通过第二种方法(注册未来位置更新通知)的位置更新可以通过 gps 提供程序按预期工作。更新即将到来,并且在我的测试手机上的文本字段中显示和更新了位置。

于 2013-08-15T13:35:39.993 回答