0

我正在尝试在移动设备上获取 GPS 纬度和经度。日志中没有错误,但我无法获得任何信息。无论是否选择了“使用无线网络/GPS”设置。需要帮助

public class MainActivity extends Activity {

    private final static String TAG = "GPSTest";

    private LocationManager mLocationManager;
    private LocationListener mLocationListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String serviceName = Context.LOCATION_SERVICE;
        mLocationManager = (LocationManager) getSystemService(serviceName);

        mLocationListener = new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                Log.e(TAG, "onLocationChanged");
                if (location != null) {
                    Log.e(TAG, "Current Latitude = " + location.getLatitude());
                    Log.e(TAG, "Current Longitude = " + location.getLongitude());
                }
                mLocationManager.removeUpdates(this);
            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                Log.e(TAG, "onProviderDisabled");

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                Log.e(TAG, "onProviderEnabled");
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
                Log.e(TAG, "onStatusChanged");
            }
        };

        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, mLocationListener);
        } else {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, mLocationListener);
        }
    }

    @Override
    protected void onDestroy() {
        mLocationManager.removeUpdates(mLocationListener);
        super.onDestroy();
    }
}
4

3 回答 3

1

您实际上是在设备中使用吗?如果您在模拟器中使用,则必须geo fix <longitude value> <latitude value>通过 Telnet 强制 ()。在设备中,您必须在“设置”中激活定位服务。

另一件事,清单文件中是否有以下行?

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

您需要获得许可才能访问该位置。

编辑

您还可以使用 DDMS 在模拟器上模拟地理位置。

于 2013-01-29T10:25:35.483 回答
0

Please give these permissions:

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

and for more browse this link:

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

于 2013-01-29T10:35:23.033 回答
0

您是否在清单文件中提供了如下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
于 2013-01-29T10:28:00.193 回答