【问题标题】:How can i get current location in my android app using GPS..?如何使用 GPS 在我的 android 应用程序中获取当前位置..?
【发布时间】:2021-02-10 07:20:02
【问题描述】:

我想创建一个应用程序来获取用户位置如城市名称、国家/地区等。我想按当前位置命名城市,但我有纬度和经度。如何获得?

【问题讨论】:

  • 你试过什么?什么不工作?请在您的问题中添加代码
  • 您可以通过以下代码使用经纬度检查来获取地址,城市名称,国家名称。可以通过你的经纬度代替经纬度。 val geoCoder = Geocoder(this) val addresses = geoCoder.getFromLocation(latitude, longitude, 1) if (addresses.size > 0) { val address = addresses[0] locationCountry = address?.countryName ?: "" locationState = address?.adminArea ?: "" locationCity = address.locality ?: "

标签: android android-layout gps android-gps


【解决方案1】:
 Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
 String cityName = addresses.get(0).getAddressLine(0);
 String stateName = addresses.get(0).getAddressLine(1);
 String countryName = addresses.get(0).getAddressLine(2);

将 MyLat ,My Long 替换为您的 lat long

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2018-03-20
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多