我正在尝试获取我的当前位置,但这显示错误。我也尝试使用以下代码获取位置,但无法正常工作

时间:2019-02-26 17:35:57

标签: android location

我正在使用此代码,但无法正常工作。

     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                     if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
 {
                         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
 PackageManager.PERMISSION_GRANTED &&
 ActivityCompat.checkSelfPermission(this,
 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.
                             return;
                         }
                         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
 0, 0, new LocationListener() {
                             @Override
                             public void onLocationChanged(Location location) {
                                 //get latitude
                                 double latitude = location.getLatitude();
                                 //get Longtitude
                                 double longitude = location.getLongitude();


                                 LatLng latLng = new LatLng(latitude, longitude);

             //                    Toast.makeText(getApplicationContext(), latitude+" "+longitude,
 Toast.LENGTH_LONG).show();

                                 Geocoder geocoder = new Geocoder(getApplicationContext());
                                 try {
                                     List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                                     String str = addresses.get(0).getLocality() + ",";
                                     str += addresses.get(0).getCountryName();
                                     mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                                     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
                                 } catch (IOException e) {
                                     e.printStackTrace();
                                 }
                             }

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

                             }

                             @Override
                             public void onProviderEnabled(String provider) {

                             }

                             @Override
                             public void onProviderDisabled(String provider) {

                             }
                         });
                     }
                     else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                     {
                         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
 0, 0, new LocationListener() {
                             @Override
                             public void onLocationChanged(Location location) {
                                 //get latitude
                                 double latitude = location.getLatitude();
                                 //get Longtitude
                                 double longitude = location.getLongitude();

                                 LatLng latLng = new LatLng(latitude, longitude);


                                 Geocoder geocoder = new Geocoder(getApplicationContext());
                                 try {
                                     List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
                                     String str = addresses.get(0).getLocality()+",";
                                     str +=addresses.get(0).getCountryName();
                                     mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                                     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
                                 } catch (IOException e) {
                                     e.printStackTrace();
                                 }
                             }

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

                             }

                             @Override
                             public void onProviderEnabled(String provider) {

                             }

                             @Override
                             public void onProviderDisabled(String provider) {

                             }
                         });

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

清单中是否具有权限?

$datetime = Carbon::parse('2019-03-13T18:07:54.5810814+03:30');
dump($datetime);

// Carbon @1552487874 {#358
//    date: 2019-03-13 18:07:54.581081 +03:30
// }

这是我可以使用的代码。 您可以忽略某些评论内容,在我的情况下,我使用片段,因此您可能需要进行一些更改以适应您的需求。

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

以及要在哪里使用它:

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Button;


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import static android.content.ContentValues.TAG;
import static android.support.v4.app.ActivityCompat.requestPermissions;

public class GPSTracker extends Service implements LocationListener {

    boolean asked = false;
    public  Boolean update = true;

    private final Context mContext;
    // flag for GPS status
    boolean isGPSEnabled = false;
    // flag for network status
    boolean isNetworkEnabled = false;
    // flag for GPS status
    boolean canGetLocation = false;
    Location location; // location
    double latitude; // latitude
    double longitude; // longitude
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 100000; // 10 meters
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 20; // 1 minute
    // Declaring a Location Manager
    protected LocationManager locationManager;

    private static final int PERMISSION_REQUEST_CODE = 1;

    private static final String TAG_RUNTIME_PERMISSION = "TAG_RUNTIME_PERMISSION";

    public GPSTracker(Context context) {
        this.mContext = context;
        if (getUpdate()) {
            getLocation();
        }
    }


    public static class LocationAddress {
        private static final String TAG = "LocationAddress";

        public void getAddressFromLocation(final double latitude, final double longitude,
                                                  final Context context, final Handler handler) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    String result = null;
                    try {
                        List<Address> addressList = geocoder.getFromLocation(
                                latitude, longitude, 1);
                        if (addressList != null && addressList.size() > 0) {
                            Address address = addressList.get(0);
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                                sb.append(address.getAddressLine(i)).append("\n");
                            }
                            sb.append(address.getLocality()).append("\n");
                            sb.append(address.getPostalCode()).append("\n");
                            sb.append(address.getCountryName());
                            result = sb.toString();
                            Log.e("address", result);
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Unable connect to Geocoder", e);
                    } finally {
                        Message message = Message.obtain();
                        message.setTarget(handler);
                        if (result != null) {
                            Dan latlong = new Dan();
                            float[] izid = new float[1];
//                            Location.distanceBetween(latitude,longitude,latlong.getBiljeLatitude(), latlong.getBiljeLongtitude(),izid);
//                            float min=izid[0];
//                            izid[0] = 0;
//                            Location.distanceBetween(latitude,longitude,latlong.getLjubljanaLatitude(), latlong.getLjubljanaLongtitude(),izid);
//                            if(izid[0]>min){Log.e("Krajsa razdalja je min", String.valueOf(min));}
//                            else Log.e("Krajsa razdalja je izid", String.valueOf(izid[0]));
//                            Log.e("min - Bilje", String.valueOf(min));
//                            Log.e("Izid - Lj", String.valueOf(izid[0]));
                            message.what = 1;
                            Bundle bundle = new Bundle();
                            result = "Latitude: " + latitude + " Longitude: " + longitude +
                                    "\n\nAddress:\n" + result;
                            bundle.putString("address", result);
                            message.setData(bundle);


                        } else {
                            message.what = 1;
                            Bundle bundle = new Bundle();
                            result = "Latitude: " + latitude + " Longitude: " + longitude +
                                    "\n Unable to get address for this lat-long.";
                            bundle.putString("address", result);
                            message.setData(bundle);
                        }
                        message.sendToTarget();
                    }
                }
            };
            thread.start();
        }
    }


    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if (!isGPSEnabled && !isNetworkEnabled) {
                 showSettingsAlert();
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        requestRuntimePermission(getApplication(), Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_CODE);
                        }
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();

                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                setUpdate(false);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }
    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }
    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
        // return latitude
        return latitude;
    }
    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
        // return longitude
        return longitude;
    }

    public boolean getUpdate(){
        return update;
    }

    public void setUpdate(boolean update){
        this.update = update;
    }
    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        if(!asked) {
            requestRuntimePermission(getApplication(), Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_CODE);
            asked=true;
        }
        return this.canGetLocation;
    }
    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");
        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
                Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                positiveButton.setBackgroundColor(Color.parseColor("#FFE1FCEA"));
                positiveButton.setTextColor(Color.parseColor("#000"));
            }
        });
        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

            }
        });
        // Showing Alert Message
        alertDialog.show();
    }

    private boolean hasRuntimePermission(Context context, String runtimePermission) {
        boolean ret = false;

        // Get current android os version.
        int currentAndroidVersion = Build.VERSION.SDK_INT;

        // Build.VERSION_CODES.M's value is 23.
        if (currentAndroidVersion > Build.VERSION_CODES.M) {
            // Only android version 23+ need to check runtime permission.
            if (ContextCompat.checkSelfPermission(context, runtimePermission) == PackageManager.PERMISSION_GRANTED) {
                ret = true;
            }
        } else {
            ret = true;
        }
        return ret;
    }
//
//    /* Request app user to allow the needed runtime permission.
//       It will popup a confirm dialog , user can click allow or deny. */
    private void requestRuntimePermission(Application activity, String runtimePermission, int requestCode) {
        Log.e(TAG, "requesting" );
        requestPermissions((Activity)mContext, new String[]{runtimePermission}, requestCode);
    }

    @Override
    public void onLocationChanged(Location location) {
        setUpdate(true);
    }
    @Override
    public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
    }
    @Override
    public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
        return null;
    }
}

用于测试的布局:

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class Tab5Fragment extends Fragment {
    TextView tvAddress;
    Button btnShowLocation;
    GPSTracker gps;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab5, container, false);
        tvAddress = view.findViewById(R.id.tvAddress);
        btnShowLocation = view.findViewById(R.id.btnShowLocation);
        // Show location button click event


        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // Create class object
                gps = new GPSTracker(getActivity());

                // Check if GPS enabled
                if (gps.canGetLocation()) {
                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();
                    GPSTracker.LocationAddress locationAddress = new GPSTracker.LocationAddress();
                    locationAddress.getAddressFromLocation(latitude, longitude,
                            getContext(), new GeocoderHandler());
                    // \n is for new line
                    Toast.makeText(getActivity(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    String result = "Latitude: " + latitude + " Longitude: " + longitude;
                    tvAddress.setText(result);
                } else {

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
                    // Setting DialogHelp Title
                    alertDialog.setTitle("GPS is settings");
                    // Setting DialogHelp Message
                    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
                    // On pressing Settings button
            alertDialog.setPositiveButton("Settings",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(
                                    Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            getActivity().startActivity(intent);
                        }
                    });
            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();
                }
            }
        });




        return view;
    }

    class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            String locationAddress;
            switch (message.what) {
                case 1:
                    Bundle bundle = message.getData();
                    locationAddress = bundle.getString("address");
                    break;
                default:
                    locationAddress = null;
            }
            tvAddress.setText(locationAddress);
        }
    }
}