LocationManager requestLocationUpdates不起作用 [英] LocationManager requestLocationUpdates doesn't work
问题描述
我试图让机器人使用LocationManager和LocationListener的当前位置处的 http://developer.android.com/guide/topics/location/obtaining-user-location.html
I'm trying to get the current location in android using LocationManager and LocationListener as described at http://developer.android.com/guide/topics/location/obtaining-user-location.html
不过,LocationListener的的onLocationChanged方法不会被调用。我用一个真正的Android手机/还使用了仿真器和使用telnet模拟的位置发生改变,如在上面的链接描述。
However, onLocationChanged method of the LocationListener is never called. I've used a real android phone / also used the emulator and simulated location changed using telnet, as described in the link above.
下面是我的code:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new CustomLocationListener(
getApplicationContext());
// Location Providers
String locationProvider = LocationManager.NETWORK_PROVIDER;
// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;
mlocManager
.requestLocationUpdates(locationProvider, 0, 0, mlocListener);
}
}
和我LocationListener的:
and my LocationListener:
public class CustomLocationListener implements LocationListener {
private Context m_context;
public CustomLocationListener(Context context) {
m_context = context;
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String Text = latitude + " " + longitude;
Toast.makeText(m_context, Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO
}
@Override
public void onProviderEnabled(String provider) {
// TODO
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
所以onLocationChanged永远不会被调用。 任何人都可以看到这个问题? 非常感谢
So onLocationChanged is never called. Can anyone see the problem?? Thanks a lot
推荐答案
您还可以让刚刚设置的手机确定了最佳提供商,而不是
You can also let your phone determine the best provider instead of just setting
String locationProvider = LocationManager.NETWORK_PROVIDER;
试试这个来代替:
Try this instead:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String locationProvider = mlocManager.getBestProvider(criteria, true);
此外,有你在你的设备移动到新的位置进行测试?否则,onlocationchanged事件可能不会触发。当我前搞清楚这个东西出来几年,我把一些敬酒,提醒我的纬度/经度坐标的我,因为我走了各地块来验证事件射击。
Also, have you tested by moving your device to a new location? Otherwise the "onlocationchanged" event may not fire. When I was figuring this stuff out a couple years ago, I put in some "toast" to alert me of my lat/long coordinates as I walked around the block to verify the event was firing.
这篇关于LocationManager requestLocationUpdates不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!