从我目前的位置到附近的城市或城镇

时间:2016-03-02 12:14:09

标签: java android google-maps

如何从我在android中的当前位置查找附近的城市或城镇。我可以到最近的地方,如ATM,医院。但是,我无法获得最近的城镇或城市名称。

3 个答案:

答案 0 :(得分:1)

以下是一个例子:

https://github.com/yangjiandong/proAndroid4DevCode/blob/master/Full%20Worked%20Sample%20Projects/Chapter_13_Where_Am_I_Part_3/src/com/paad/whereami/WhereAmI.java#L14

     private void updateWithNewLocation(Location location) {
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);

    String latLongString = "No location found";
    String addressString = "No address found";

    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "\nLong:" + lng;

      double latitude = location.getLatitude();
      double longitude = location.getLongitude();
      Geocoder gc = new Geocoder(this, Locale.getDefault());

      try {
        List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
        StringBuilder sb = new StringBuilder();
        if (addresses.size() > 0) {
          Address address = addresses.get(0);

          for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
            sb.append(address.getAddressLine(i)).append("\n");

          sb.append(address.getLocality()).append("\n");
          sb.append(address.getPostalCode()).append("\n");
          sb.append(address.getCountryName());
        }
        addressString = sb.toString();
      } catch (IOException e) {}
    }

    myLocationText.setText("Your Current Position is:\n" +
                            latLongString + "\n\n" + addressString);
  }

  private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

答案 1 :(得分:0)

答案 2 :(得分:0)

我正在提供一个链接,您可以在其中找到我在我的应用程序中尝试过的工作源代码在这里,我发布链接希望您发现这很有用。

Web link

here is output of code

相关问题