【问题标题】:How to get the street address as title for marker如何获取街道地址作为标记的标题
【发布时间】:2021-03-03 13:18:16
【问题描述】:

以下是我的代码..

fetchlastlocation() 是一个在点击 gps icon(imageView) 时调用的方法。此代码有效,但我想获取/获取街道地址并将其附加到标记标题。 我应该怎么做才能得到这个结果?

 private void fetchlastlocation() {
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]
                    {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);

            return;
        }
        Task<Location> task=fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location1) {
                if(location1!=null){
                    currentlocation=location1;

                    LatLng latLng=new LatLng(currentlocation.getLatitude(),currentlocation.getLongitude());
                    MarkerOptions markerOptions=new MarkerOptions().position(latLng);
                    nMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
                    nMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,50));
                }
            }
        });
    }

【问题讨论】:

    标签: java android android-studio google-maps google-maps-api-3


    【解决方案1】:

    您要做的称为反向地理编码。 check docs

    由于您已经有了地理坐标(纬度和经度),因此将它们与 Geocoder 类一起使用来查找您想要的人类可读地址

    【讨论】:

      【解决方案2】:

      使用Geocoder从latlong获取地址

      public static String getCountryName(Context context, double latitude, double longitude) {
      Geocoder geocoder = new Geocoder(context, Locale.getDefault());
      List<Address> addresses = null;
      try {
          addresses = geocoder.getFromLocation(latitude, longitude, 1);
          Address result;
      
          if (addresses != null && !addresses.isEmpty()) {
              return addresses.get(0).getAddressLine(0);
             //     String city = addresses[0].locality;
             //     String state = addresses[0].adminArea;
            //      String country = addresses[0].countryName;
            //      String postalCode = addresses[0].postalCode;
            //      String knownName = addresses[0].featureName;
          }
          return null;
      } catch (IOException ignored) {
         //do something
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多