确定用户的当前位置和放置标记

时间:2018-05-26 22:29:11

标签: android

我正在开发一款需要用户在MapView上标记当前位置的应用。我已经浏览了互联网,并且找不到任何与我的代码兼容的内容。我已经在本网站和其他Android教程网站/ YouTube视频上查看过很多问题。我已经尝试获得访问精确位置的权限,然后创建一个包含当前纬度和经度的字符串。但是,这不起作用。

我尝试过使用代码,但我无法弄明白。我想要做的是获取用户的当前位置,然后在MapView上放置一个标记,该标记还包含放置在某个位置的标记。我理解代码,我只是不知道a)为什么它不起作用b)我需要做些什么来解决它。我在下面列出了活动的代码。

.deb

1 个答案:

答案 0 :(得分:0)

这是我从我的某个应用获取位置的方式。

public class LocationProvider extends Service implements LocationListener {

    private final Context mContext;

    //flag gor GPS status
    boolean isGPSEnabled = false;
    //flag for network status
    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    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 = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public LocationProvider(Context context) {
        this.mContext = context;
        location = getLocation();
    }


    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) {
                // No network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    try {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    }catch (SecurityException e) {e.printStackTrace();}
                    //Log.d("NetworkLocation", "Network");
                    if (locationManager != null) {
                        try {
                            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        } catch (SecurityException e){e.printStackTrace();}

                            if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                           // Log.i("LOC_Net:latitude",String.valueOf(latitude));
                           // Log.i("LOC_Net:langitude",String.valueOf(longitude));
                        }
                    }
                }
                // If GPS enabled, get latitude/longitude using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        try {
                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        } catch (SecurityException e){e.printStackTrace();}

                           // Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            try {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            } catch (SecurityException e) {e.printStackTrace();}

                                if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
        }
        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){
            try{
            locationManager.removeUpdates(LocationProvider.this);
              }catch (SecurityException e){e.printStackTrace();}

        }
    }


    /**
     * 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;
    }

    /**
     * Function to check GPS/Wi-Fi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }


    /**
     * Function to show settings alert dialog.
     * On pressing the Settings button it will launch Settings Options.
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("Turn On Location");

        // Setting Dialog Message
        alertDialog.setMessage("Location is not enabled. Do you want to go to settings menu?");

        // On pressing the 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);
            }
        });

        // On pressing the cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

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


    @Override
    public void onLocationChanged(Location location) {
    }


    @Override
    public void onProviderDisabled(String provider) {
    }


    @Override
    public void onProviderEnabled(String provider) {
    }


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


    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

在您的活动中声明变量

私人双重纬度; 私人双重经度;

并创建一个这样的方法:

private boolean getLocation(){

    locationProvider = new LocationProvider(this);
    Location location = locationProvider.getLocation();

    if (locationProvider.canGetLocation()){
        latitude = locationProvider.getLatitude();
        longitude = locationProvider.getLongitude();

        return true;

    } else {
        //Can't get location//GPS or network is not enabled
        //Ask user to enable GPS/Network in settings
        locationProvider.showSettingsAlert();
        return false;
    }
}