在获取设备上的纬度和经度位置时,我在logcat中得到null。
但是在genymotion模拟器中,如果我在genymotion模拟器的右侧打开gps,我将获得gps纬度和经度位置。
我不知道为什么我无法获得设备中的纬度和经度位置。
下面我发布了相关的代码。
logcat的:
E:/ gpsLocation: null
FragmentDiscovery.java:
AppLocationService appLocationService;
appLocationService = new AppLocationService(getActivity());
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getLocation();
Log.e("GET_USER_LAT_STR", ""+GET_USER_LAT_STR );
Log.e("GET_USER_LON_STR", ""+GET_USER_LON_STR );
editor.putString("GetLat", GET_USER_LAT_STR);
editor.putString("GetLon", GET_USER_LON_STR);
editor.commit();
Fragment fragment = new CardsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
// Inflate the layout for this fragment
return rootView;
}
void getLocation(){
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
Log.e("gpsLocation", ""+gpsLocation);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
GET_USER_LAT_STR = String.valueOf(latitude);
GET_USER_LON_STR = String.valueOf(longitude);
} else {
showSettingsAlert("GPS");
}
}
public void showSettingsAlert(String provider) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle(provider + " SETTINGS");
alertDialog
.setMessage(provider + " is not enabled! Want to go to settings menu?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
getActivity().startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
修改
在设备中它没有获得gpslocation.Instead只是在设置中显示打开gps窗口。但我已经打开gps
答案 0 :(得分:0)
使用onLocationChanged获取更新的位置。