[英]Trouble getting location data using LTE rather than WiFi
I am trying to get the Lat/Long for the device when using 4G/LTE. 使用4G / LTE时,我正在尝试获取设备的纬度/经度。 The code below works great when using WiFi but the
onLocationChanged
method doesn't get called at all when using 4G/LTE. 下面的代码在使用WiFi时效果很好,但是在使用4G / LTE时根本不会调用
onLocationChanged
方法。 Any idea why? 知道为什么吗?
I only have a limited window to get the location coordinates as they need to be appended to an audit log at the beginning. 我只有一个有限的窗口来获取位置坐标,因为它们需要在开始时附加到审核日志中。
Does LTE/4G usually take much longer than WiFi to pinpoint the lat/long? LTE / 4G通常比WiFi花费更长的时间来确定经纬度吗?
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (networkType.equals("WiFi")) {
lp = LocationManager.NETWORK_PROVIDER;
}
else {
lp = LocationManager.GPS_PROVIDER;
}
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
gpsTestLocation = locationStringFromLocation(location);
System.out.println("mcsoutput location: " + gpsTestLocation);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
// Register the listener with the Location Manager to receive location updates
lm.requestLocationUpdates(lp, 0, 0, locationListener);
Network Type is figured out by: 网络类型可以通过以下方式得出:
private String checkNetworkState() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// NetworkInfo mEthernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
TelephonyManager telephonyService = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (mWifi!=null) isOnWifi = mWifi.isConnected();
if (m3G!=null) is3G = m3G.isConnected();
if(isOnWifi == true) {
return "WiFi";
}
else {
int cellType = telephonyService.getNetworkType();
return isConnectionFast(cellType);
}
}
You're using NETWORK_PROVIDER when on wifi and GPS when off. 开启wifi时使用NETWORK_PROVIDER,关闭时使用GPS。 Odds are you aren't getting a GPS synch.
奇怪的是您没有获得GPS同步。 Its actually very hard to do when indoors.
在室内实际上很难做到。 Are you getting a flashing GPS symbol in your notification bar?
通知栏中是否有闪烁的GPS符号? If so, you aren't getting a full GPS synch and thus onLocationChanged won't be called.
如果是这样,您将无法获得完整的GPS同步,因此不会调用onLocationChanged。
When on wifi using NETWORK_PROVIDER you'll get an almost instant location because NETWORK_PROVIDER needs no satellites and is almost always available, it just isn't nearly as accurate. 当使用NETWORK_PROVIDER进行wifi上网时,您将获得几乎即时的位置,因为NETWORK_PROVIDER不需要卫星,并且几乎总是可用,它几乎不那么准确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.