【问题标题】:"How to get actual text value(Place name) from LatLong ?"“如何从 LatLong 获取实际文本值(地名)?”
【发布时间】:2016-01-17 12:12:46
【问题描述】:

我正在 Android 中开发 GoogleMap。

到目前为止,我已经获得了当前位置并在其上显示了标记。

我从当前位置获得了 LatLong 值。

我可以使用以下代码获取城市名称:

            Geocoder gcd = new Geocoder(MainActivity.this, Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = gcd.getFromLocation( mLocation.getLatitude(), mLocation.getLongitude(), 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (addresses.size() > 0)
            System.out.println(addresses.get(0).getLocality());
            Toast.makeText(MainActivity.this,""+addresses.get(0).getLocality(),Toast.LENGTH_LONG).show();
    }

但是,无法获得实际的地区名称,即 Bodakdev char rasta,艾哈迈达巴德。

现在,我的问题是:如何从 LatLong 获取实际文本值(地名)?

【问题讨论】:

    标签: android google-maps google-maps-api-3 geolocation


    【解决方案1】:

    您可以通过 Google 地图中的 Geocoder 对象获取此信息。方法getFromLocation(double, double, int) 完成了这项工作。

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());
    
    addresses = geocoder.getFromLocation(latitude, longitude, 1); //  1 represent max location result to returned, by documents it recommended 1 to 5
    
    String address = addresses.get(0).getAddressLine(0); // If any additional     address line present than only, check with max available address lines by getMaxAddressLineIndex()
    String city = addresses.get(0).getLocality();
    String state = addresses.get(0).getAdminArea();
    String country = addresses.get(0).getCountryName();
    String postalCode = addresses.get(0).getPostalCode();
    String knownName = addresses.get(0).getFeatureName(); 
    

    【讨论】:

    • 是的,我可以使用上面的代码获取城市名称,但是如果我想按照上面的编辑建议做呢?
    • 查看我编辑的答案。这将为您提供所有详细信息。如果该输入集的信息不可用,则为 NULL。 @JaiminModi
    【解决方案2】:

    使用这个,

    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);
    String address = addresses.get(0).getAddressLine(0);
    

    【讨论】:

      【解决方案3】:

      您还可以从该地点的邮政编码或邮政编码获取城市名称

      private fun getLatLngByZipcode(zipcode: String): String {
          var place = context.getString(R.string.location)
          val geocoder = Geocoder(context, Locale.getDefault())
          try {
              val addresses = geocoder.getFromLocationName(zipcode, 5)
              addresses?.forEach{
                  it?.locality?.apply {
                      place=this
                  }
              }
          }
          catch (e: IOException) {
              LogUtils.error(TAG,e.message)
          }
          return place
      } 
      

      【讨论】:

        猜你喜欢
        • 2016-08-20
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多