【问题标题】:Getting Lat and Long values from FusedLocationProviderClient android从 FusedLocationProviderClient android 获取 Lat 和 Long 值
【发布时间】:2019-04-27 05:37:54
【问题描述】:

我正在尝试使用FusedLocationProviderClient 来获取当前位置的纬度和经度。到目前为止我的代码如下:

清单

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

布局:有一个按钮

build.gradle

implementation 'com.google.android.gms:play-services-location:16.0.0'

Java

ArrayList<String> Coord_Array;

OnCreate 方法内部

private FusedLocationProviderClient client;
client = LocationServices.getFusedLocationProviderClient( ShowOutletParams.this );
Coord_Array = new ArrayList<String>();

在 OnCreate 方法之外 - 我在按钮 onclicklistener 内使用getLocation()

private String getLocation() {
        if (ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        client.getLastLocation().addOnSuccessListener( ShowOutletParams.this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    String locat = location.toString();
                    String lat = locat.substring(15,24);
                    String longi = locat.substring(25,34);
                    Coord_Array.add(lat);
                    Coord_Array.add(longi);
                    //return Coord_Array;
                }
            }
        } );

    }

当我添加 Log.i 来记录 lat 和 long 值时,我可以在日志中看到它们,但是我希望将它们保存为字符串,以便以后使用。
我知道void 不会返回任何东西....但是Coord_Array 也不会返回或打印经纬度。

【问题讨论】:

    标签: android location


    【解决方案1】:

    你应该试试:

    client.getLastLocation().addOnSuccessListener( ShowOutletParams.this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                        if (location != null) {
                            double mLatitude = location.getLatitude();
                            double mLongitude = location.getLongitude();                           
                        }
                    });
                } else {
                    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
                }}
    

    如果仍然面临问题,请检查 tutorial

    【讨论】:

      【解决方案2】:

      主要问题是您的方法 private String getLocation() 在任何 Location 实际可用之前返回(而且我很确定通过查看它不会编译)。

      当你在这里时:

      if (ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
         return;
      }
      

      您仍处于方法的标准流程中。但是一旦你通过这里:

      client.getLastLocation().addOnSuccessListener(
      

      您的public void onSuccess(Location location) 中的所有代码现在都在运行之后您对getLocation() 的调用已经返回!

      一个简单的解决方案是将回调传递给您的getLocation() 方法,以便在方法已经返回给其调用者之后触发它们,例如

      public interface LocationAvailableListener {
          public void onLocationAvailable(Location location)
      }
      
      public void getLocation(LocationAvailableListener locationAvaialbleCallback, Runnable locationNotAvailableCallback) {
      ...
      

      然后在检查权限时,你现在可以触发你的失败回调:

      if (ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
         locationNotAvailableCallback.run();
      
         return;
      }
      

      然后在你的onSuccess

      if (location != null) {
         locationAvaialbleCallback.onLocationAvailable(location);
      }
      else {
         locationNotAvailableCallback.run();
      }
      

      然后使用:

      getLocation(
         location -> {
            String latitude = String.valueOf(location.getLatitude());
            String longitude = String.valueOf(location.getLongitude());
      
            // Add to your ArrayList here
            // Save to your database or wherever for later
         },
         { Log.i(TAG, "Failed to get Location"); })
      

      而且由于您询问了在获取位置数据后是否保留它,this reference 应该通过一些简单的 JSON 序列化为您指明正确的方向。

      【讨论】:

      • 谢谢....这是我的第一个应用程序....从来没有用 Java 构建过任何东西....会尝试您的建议。
      • 感谢您的所有解释......我清楚地知道我哪里出错了......
      【解决方案3】:

      您必须直接从位置对象中获取经度和纬度,并根据需要将其转换为字符串。

      在 Kotlin 中:

      lat = location.latitude.toString()

      在 Java 中:

      lat = location.getLatitude() + "";

      【讨论】:

        猜你喜欢
        • 2015-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        • 1970-01-01
        • 2015-01-25
        相关资源
        最近更新 更多