【问题标题】:Android - Get location ( Geocoder / Google Play Service )?Android - 获取位置(地理编码器/谷歌播放服务)?
【发布时间】:2018-10-23 21:11:46
【问题描述】:

我想以city, address - latitude longitude 的形式获取用户的位置。 我已经看到使用 Google Play Service 和 Geocoder 可以做到这一点。是对的吗? 您更喜欢哪种方法,为什么?

使用 Google Play 服务,它看起来更容易实现。 有什么我需要考虑的吗?

【问题讨论】:

    标签: android geolocation google-play-services geocoding


    【解决方案1】:

    您可以阅读how to display a location address in android 上包含纬度和经度坐标的完整文档。

    使用 Geocoder Android 框架位置 API 中的类,您可以转换 地址到相应的地理坐标。您可以使用 getFromLocation() 将地理位置转换为地址的方法。这种方法 返回对应于给定纬度的估计街道地址 和经度。

    【讨论】:

      【解决方案2】:

      您可以使用以下方法获取用户当前的位置坐标和位置详情。

       //Get last known location coordinates
          private void getLastLocationNewMethod() {
              FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
              if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                      ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  // TODO: Consider calling
                  //    ActivityCompat#requestPermissions
                  // here to request the missing permissions, and then overriding
                  //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                  //                                          int[] grantResults)
                  // to handle the case where the user grants the permission. See the documentation
                  // for ActivityCompat#requestPermissions for more details.
                  //session.saveCurrentLocation("Everywhere");
                  return;
              }
              mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                  @Override
                  public void onSuccess(Location location) {
                      // GPS location can be null if GPS is switched off
                      if (location != null) {
                          double myLat = location.getLatitude();
                          double myLon = location.getLongitude();
      
                          getAddress(myLat, myLon);
      
      
                      }
                  }
              }).addOnFailureListener(new OnFailureListener() {
                  @Override
                  public void onFailure(@NonNull Exception e) {
                      Log.d("MapDemoActivity", "Error trying to get last GPS location");
                      e.printStackTrace();
      
                  }
              });
          }
      
          //get location name from coordinates
          public void getAddress(double lat, double lng) {
              String currentLocation = "";
      
              Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);
              try {
                  List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
                  Address obj = addresses.get(0);
                  String add = obj.getAddressLine(0);
                  add = add + "\n" + obj.getCountryName();
                  add = add + "\n" + obj.getCountryCode();
                  add = add + "\n" + obj.getAdminArea();
                  add = add + "\n" + obj.getPostalCode();
                  add = add + "\n" + obj.getSubAdminArea();
                  add = add + "\n" + obj.getLocality();
                  add = add + "\n" + obj.getSubThoroughfare();
      
                  Log.v("IGA", "Address" + add);
      
      
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-27
        相关资源
        最近更新 更多