如何解决错误"反向地理失败"在Android Studio中

时间:2018-02-22 14:07:17

标签: android android-studio geolocation reverse-geocoding

我的手机 xiaomi redmi note 2 ,有一个棒棒糖操作系统(API 21)。 如何获取我的点的坐标并将它们转换为相应的地址?为什么我的小工具无法运行并且错误"反向地理失败"出现,但在其他小工具运行得很好。 我在设置gradle中有minSdkVersion 21。

以下代码

public class Emergency extends AppCompatActivity implements LocationListener {

   protected LocationManager locationManager;
   ProgressDialog loading;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_emergency);

       if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
       }
   }

   //onClick button
   public void btn_pick_up(View view) {
       android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(this);
       alertDialogBuilder.setMessage("Are you sure");
       alertDialogBuilder.setCancelable(false);
       alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface arg0, int arg1) {

               loading = ProgressDialog.show(Emergency.this, "Loading", "Please wait", false, false);
               getLocation();
           }
       });

       alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface arg0, int arg1) {}
       });

       android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create();
       alertDialog.show();
   }

   void getLocation() {
       try {
           locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
           assert locationManager != null;
           locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this);

       } catch(SecurityException e) {
           e.printStackTrace();
           loading.dismiss();
       }
   }

   @Override
   public void onLocationChanged(Location location) {
       Toast.makeText(Emergency.this, "Latitude: " + location.getLatitude() + "\n Longitude: " + location.getLongitude(), Toast.LENGTH_LONG).show();

       try {
           Geocoder geocoder = new Geocoder(this, Locale.getDefault());
           List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
           Toast.makeText(Emergency.this,
                addresses.get(0).getAddressLine(0) +", "+
                        addresses.get(0).getAddressLine(1) +", "+
                        addresses.get(0).getAddressLine(2),
                Toast.LENGTH_LONG).show();
           locationManager.removeUpdates(this);
           loading.dismiss();
       } catch(Exception e) {
           Toast.makeText(Emergency.this, e.getMessage(), Toast.LENGTH_LONG).show();
           loading.dismiss();
       }
   }

   @Override
   public void onProviderDisabled(String provider) {
       Toast.makeText(Emergency.this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {}

   @Override
   public void onProviderEnabled(String provider) {
       Toast.makeText(Emergency.this, "GPS Enable", Toast.LENGTH_SHORT).show();
   }
}

0 个答案:

没有答案