无法使用GPS获取位置信息。

3

我正在开发一款应用程序。其中,我需要获取用户的当前位置。我使用网络获取当前位置,但是当我启动GPS时,它没有显示我当前的位置。可能的问题是什么?

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

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

        String bestProvider = locationManager.getBestProvider(criteria, true);

        System.out.println("Best Provider:"+bestProvider);

        // 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
            Toast.makeText(mContext, "GPS and Network not enabled!", Toast.LENGTH_SHORT).show();
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        //LocationManager.NETWORK_PROVIDER,
                        bestProvider,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            //.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            .getLastKnownLocation(bestProvider);
                    if (location != null) {
                        startLatitude = location.getLatitude();
                        startLongitude = location.getLongitude();

                        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,
                            bestProvider,
                            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);
                                .getLastKnownLocation(bestProvider);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

我已经添加了权限:

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

请再精确一些:您在状态栏中是否看到 GPS 图标闪烁? - user755
状态面板中闪烁着。 - Saurabh
当闪烁停止时,您应该在onLocationChanged()方法中接收到GPS定位。您是否在您的类中实现了这个方法? - user755
是的,我已经实现了LocationListener类。 - Saurabh
还不清楚,闪烁是否停止了? - user755
显示剩余2条评论
1个回答

0

谷歌在最近的Google IO 2013活动中发布了一个不错的API:

https://developers.google.com/events/io/sessions/324498944

你应该去看看,看看如何最小化你的代码。

请注意,这需要设备安装有 Play Store 应用程序才能正常工作。

与使用普通位置传感器(电池、速度、精度等)相比,这种方法具有许多优点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接