The code I have works when it feels like it. It will either work straight away or will take a very long time, or not work at all. The code is as follow:
我觉得它的代码是有效的。它可以直接工作,也可能需要很长时间,或根本不工作。代码如下:
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationLis = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
lat= location.getLatitude();
log = location.getLongitude();
locationManager.removeUpdates(locationLis);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle){
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
System.out.println("Not Found");
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationLis);
The device I am using to test is a Nexus 7 (2012)
我用来测试的设备是Nexus 7(2012)
2 个解决方案
#1
Rather than use the GPS_PROVIDER
I changed it to NETWORK_PROVIDER
.
我没有使用GPS_PROVIDER,而是将其更改为NETWORK_PROVIDER。
This used the network to get the location rather than the GPS which is what took so long. The code was:
这使用网络获取位置而不是GPS,这是花了这么长时间。代码是:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationLis);
and now reads:
现在读到:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationLis);
#2
That may be because your GPS is not getting fixed. Check for the GPS icon, It may be blinking. Thats purely because the GPS is not getting Fixed.
这可能是因为您的GPS没有得到修复。检查GPS图标,它可能正在闪烁。这纯粹是因为GPS没有固定。
So, I strongly recommend you to test the app under Open Sky. This helps in fixing the GPS faster. And while testing under open sky, you can see that the GPS icon will stop blinking and remains steady.
所以,我强烈建议您在Open Sky下测试应用程序。这有助于更快地修复GPS。在开阔天空下进行测试时,您可以看到GPS图标将停止闪烁并保持稳定。
#1
Rather than use the GPS_PROVIDER
I changed it to NETWORK_PROVIDER
.
我没有使用GPS_PROVIDER,而是将其更改为NETWORK_PROVIDER。
This used the network to get the location rather than the GPS which is what took so long. The code was:
这使用网络获取位置而不是GPS,这是花了这么长时间。代码是:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationLis);
and now reads:
现在读到:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationLis);
#2
That may be because your GPS is not getting fixed. Check for the GPS icon, It may be blinking. Thats purely because the GPS is not getting Fixed.
这可能是因为您的GPS没有得到修复。检查GPS图标,它可能正在闪烁。这纯粹是因为GPS没有固定。
So, I strongly recommend you to test the app under Open Sky. This helps in fixing the GPS faster. And while testing under open sky, you can see that the GPS icon will stop blinking and remains steady.
所以,我强烈建议您在Open Sky下测试应用程序。这有助于更快地修复GPS。在开阔天空下进行测试时,您可以看到GPS图标将停止闪烁并保持稳定。