在30秒内获得经度和纬度

我正在尝试获取用户的位置longitude以及latitude从GPS/network哪个位置可用,我想快速获取它,否则,如果现在无法获取新的位置坐标,则会返回以前的位置坐标。


这是我的尝试:


public class GPSTracker extends Service{



    // 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

    private final Context mContext;

    // Declaring a Location Manager

    protected LocationManager locationManager;

    // flag for GPS status

    boolean isGPSEnabled = false;

    // flag for network status

    boolean isNetworkEnabled = false;

    // flag for GPS status

    boolean canGetLocation = false;

    Location location = null; // location

    double latitude; // latitude

    double longitude; // longitude

    myLocationLitener locationLitener;


    public GPSTracker(Context context) {

        this.mContext = context;

        locationLitener = new myLocationLitener();

        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) {

                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,locationLitener);

                    Log.d("Network", "Network Enabled");

                    if (locationManager != null) {

                        location = locationManager

我如何使用该课程?


素胚勾勒不出你
浏览 344回答 3
3回答

鸿蒙传说

签出FusedLocationApi。它做得很好,并且为您提供了最大限度降低电池性能的选择。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android