我正在尝试检测用户当前位置何时在我的Android应用中更改。但是没有触发onLocationChanged。我尝试在Fragment btw中实现它。
片段:
public class Fragment_Map extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_fragment__live_map, container, false);
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
// The minimum time (in miliseconds) the system will wait until checking if the location changed
int minTime = 1000;
// The minimum distance (in meters) traveled until you will be notified
float minDistance = 1;
// Create a new instance of the location listener
MyLocationListener myLocListener = new MyLocationListener();
// Get the location manager from the system
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Get the criteria you would like to use
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);
// Get the best provider from the criteria specified, and false to say it can turn the provider on if it isn't already
String bestProvider = locationManager.getBestProvider(criteria, false);
// Request location updates
locationManager.requestLocationUpdates(bestProvider, minTime, minDistance, myLocListener);
return rootView;
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
Log.d("CHECKLOCA", "fired");
Toast.makeText(getActivity(), "fired", Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String arg0) {
// Do something here if you would like to know when the provider is disabled by the user
}
@Override
public void onProviderEnabled(String arg0) {
// Do something here if you would like to know when the provider is enabled by the user
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// Do something here if you would like to know when the provider status changes
}
}
为什么位置没有改变听众的射击?是因为它是片段吗?在一个黑客马拉松的家伙中间,你能帮我解决这个讨厌的错误吗?
答案 0 :(得分:1)
您可以使用GoogleApiClient
请求位置并将优先级设置为PRIORITY_HIGH_ACCURACY
。您可以让Fragment
为callbacks
实施GoogelApiClient
- 然后像这样实例化和连接GoogleAPIClient
:
googleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
然后,一旦您的客户端连接到位置服务API,您就可以请求更新:
public void onConnected(Bundle bundle){
...
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(180000); // update interval - eg (3) minutes
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, myLocListener);
...
}
我希望这会有所帮助。
答案 1 :(得分:1)
像这样创建界面:
public interface OnLocationReceived
{
public void onLocationReceived(LatLng latlong);
public void onLocationReceived(Location location);
public void onConntected(Bundle bundle);
public void onConntected(Location location);
}
2.将其放在MyLocationListener
@Override
public void onLocationChanged(Location location)
{
if (!isLocationReceived) {
mLocationReceived.onConntected(location);
isLocationReceived = true;
}
if (mLocationReceived != null) {
mLocationReceived.onLocationReceived(location);
}
latLong = getLatLng(location);
if (mLocationReceived != null && latLong != null) {
mLocationReceived.onLocationReceived(latLong);
}
}
答案 2 :(得分:0)
也许您的设备无法提供您设置的标准以查找位置。 可以在onCreateView()中使用此代码:
sqrt