我有用户报告我的GPS并不是最准确的。我想知道somone是否可以仔细查看我的代码并让我知道我是否遗漏了一些东西:
public class FindBrewery extends Fragment implements LocationListener, GetNearbyBreweries.OnArticleSelectedListener {
private TextView latituteField;
private TextView longitudeField;
LocationManager locationManager;
private String provider;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Context c = getActivity().getApplicationContext();
locationManager =(LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//todo change view
View rootView = inflater.inflate(R.layout.beer_location_list,container, false);
// Get the location manager
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
}
return rootView;
}
/* Request updates at startup */
@Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
//latituteField.setText(String.valueOf(lat));
//longitudeField.setText(String.valueOf(lng));
//Toast.makeText(this, "Finding your loaction",Toast.LENGTH_SHORT).show();
//call asycn task for location
String url = "myURLWithLatAndLng";
Log.d("urlTest", url);
//async task goes here
new GetNearbyBreweries(this.getActivity()).execute(url);
locationManager.removeUpdates(this);
//todo: get json
GetNearbyBreweries task = new GetNearbyBreweries(getActivity());
task.setOnArticleSelectedListener(this);
task.execute(url);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
//Toast.makeText(this, "Enabled new provider " + provider,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
//Toast.makeText(this, "Disabled provider " + provider,Toast.LENGTH_SHORT).show();
}
@Override
public void onArticleSelected(String b){
//code to execute on click
Fragment Fragment_one;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_one = new BreweryTabs();
final Bundle bundle = new Bundle();
bundle.putString("breweryIDSent", b);
Fragment_one.setArguments(bundle);
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
}
}
答案 0 :(得分:0)
您正在使用getLastKnownLocation。这将为您提供它拥有的最后一个位置(如果有的话),这可能不是最新的。相反,您应该使用requestLocationUpdates或requestSingleUpdate来获取当前位置。此外,您正在使用未初始化的Criteria for provider。这可能会让你获得GPS或网络提供商。如果您需要GPS,请明确请求。
答案 1 :(得分:0)
在应用程序标记之前在清单中添加这些行...它们可能有所帮助..
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
-----
</application>