private GoogleMap mMap;
private LocationManager locationManager;
private LocationListener locationListener;//listten for location changes
String provider;
boolean gpsFix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(criteria, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]
{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
}, 10);
configureRequest();
}
}
else
configureRequest();
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LatLng loc=new LatLng(location.getLatitude(),location.getAltitude());
mMap.addMarker(new MarkerOptions().position(loc));
mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
private void configureRequest() {
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
//locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
configureRequest();
return;
}
}
}
当我改变
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
至
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, locationListener);
并删除
provider = locationManager.getBestProvider(criteria, true);
这个语句程序运行,但我的目的是尽可能快地占用位置 . 当我将其更改为网络提供商时,位置信息变得非常慢 . 如何修复标准问题?我哪里做错了?有没有办法在一秒钟内获得100个位置?
谢谢..
1 回答
我真的很想知道在一秒钟内获得太多位置的确切原因是什么? (100个地点)
即使积极使用位置提供商也永远不会提供您想要的东西 . 如果您想体验一下,请尝试以下代码(尽快耗尽电量)
最后,我认为它没有引起你的注意
改成
Update
首先,请注意,无法尽快获取新的位置数据(约1或2秒) . 还有一些条件你必须处理,如首次修复,电池,准确性 . 要获得满意的位置不要在5秒内使用,所以这是我的意见 .
您可以使用融合位置提供程序,它包含gps和网络提供商,并为您提供更好的位置数据 . 它只是做你想要的 .
否则,您可以继续改进以下代码
它将过度使用提供程序,并可能冻结应用程序UI(甚至它的工作回调机制)所以你可以使用其他版本的requestLocationUpdates方法 .
将代码移动到新线程中并将其传递给looper,然后onLocationChanged方法永远不会占用应用程序主线程 . 因此,您可以对位置的精度值进行每个数学运算