【问题标题】:How to get the location of user after getting longitude and latitude获取经纬度后如何获取用户的位置
【发布时间】:2012-06-06 16:32:52
【问题描述】:

我是 android 移动开发的新手。我已经使用了 Location Manager 类并成功地找出了用户的经度和纬度。我想使用这些值来查找城市名称。我不想要地图,我只想得到城市名称。我该怎么做呢?

【问题讨论】:

标签: android location


【解决方案1】:

首先使用 Location 和 LocationManager 类(您已完成)获取纬度和经度。现在试试下面的代码获取城市,地址信息

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder gc = new Geocoder(this, Locale.getDefault());
    try {
    List<Address> addresses = gc.getFromLocation(lat, lng, 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());

城市信息现在是 sb。现在将 sb 转换为 String(使用 sb.toString() )。

【讨论】:

    【解决方案2】:

    【讨论】:

    • 无意冒犯,但您的链接并没有真正提供这样的解决方案。请编辑您的答案。 Location Manager 用于检索经纬度,但该人并没有真正询问它。该问题表明答案是 Reverse Geo-coding 。建议您在开始获得反对票之前编辑您的答案..
    【解决方案3】:

    您可以使用Geocoder

    Geocoder myLocation = new Geocoder(context, Locale.getDefault());
    List<Address> myList = null;
    try {
        myList = myLocation.getFromLocation(latitude, longitude, 1);
    } catch (IOException e) {}
    

    经度和纬度是网络或 GPS 检索到的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多