我正在尝试在移动设备上获取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();
}
}
答案 0 :(得分: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在模拟器上模拟地理位置。
答案 1 :(得分:0)
您是否在清单文件中提供了如下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_GPS" />
答案 2 :(得分:0)
请授予这些权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
以及更多浏览此链接:
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/