位置状态不一致 Android Studio
Posted
技术标签:
【中文标题】位置状态不一致 Android Studio【英文标题】:Location Status Inconsistent Android Studio 【发布时间】:2017-10-13 18:34:30 【问题描述】:我正在尝试获取用户的当前位置并在 MAP 上显示一个标记。这是我的代码
public class MapsActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback
private GoogleMap mMap;
LocationManager locationManager;
String provider;
Location mLocation;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
mLocation=locationManager.getLastKnownLocation(provider);
if (mLocation != null)
Log.i("Location Status: ", "Location Found");
else if (mLocation == null)
Log.i("Location Status: ", "Location Not Found");
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
if(mLocation!=null)
try
Double lat = mLocation.getLatitude();
Double lng = mLocation.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 12));
catch (Exception ex)
Log.e("Location Error:","Exception was "+ex);
@Override
protected void onResume()
super.onResume();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
locationManager.requestLocationUpdates(provider, 400, 1, this);
@Override
public void onLocationChanged(Location location)
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12));
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
@Override
public void onProviderDisabled(String provider)
Toast.makeText(getApplicationContext(),"Please enable your location",Toast.LENGTH_LONG).show();
@Override
protected void onPause()
super.onPause();
locationManager.removeUpdates(this);
现在,问题是大多数时候我没有得到任何位置,地图显示没有任何标记,并且在日志中显示“位置状态:未找到位置”,如果位置是,则设置为显示空值。 我试图重建并再次运行。现在在一台设备上检测到该位置,但在其他设备上显示相同的尴尬输出。我希望它每次都能正常工作,但它的行为很奇怪,因为相同的代码在相同的设备上以相同的设置在不同的时间显示两种不同的行为。 附言在设备上启用位置。 任何形式的帮助或提示将不胜感激。谢谢!
【问题讨论】:
【参考方案1】:试试我编写的这段代码
有时 GPS 无法定位您的位置,这就是您在mLocation
的对象中获得 null
的原因
所以我的建议是在 NETWORK_PROVIDER 和 GPS_PROVIDER
的基础上获得 location
这是我的代码
public class MapsActivity extends FragmentActivity implements LocationListener, OnMapReadyCallback
private GoogleMap mMap;
LocationManager locationManager;
Location mLocation;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60; // 1 minute
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
@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.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled)
mLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation == null)
mLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (locationManager != null)
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
if (mLocation != null)
Log.e("Location Status: ", "Location Found");
else
Log.e("Location Status: ", "Location Not Found");
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
/*// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
if (mLocation != null)
try
Double lat = mLocation.getLatitude();
Double lng = mLocation.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 12.5f));
catch (Exception ex)
Log.e("Location Error:", "Exception was " + ex);
@Override
public void onLocationChanged(Location location)
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
Toast.makeText(getApplicationContext(), "enable your location", Toast.LENGTH_LONG).show();
@Override
public void onProviderDisabled(String provider)
Toast.makeText(getApplicationContext(), "Please enable your location", Toast.LENGTH_LONG).show();
@Override
protected void onPause()
super.onPause();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
locationManager.removeUpdates(this);
【讨论】:
okk :-) 但如果您有任何疑问,请不要忘记点击右标记问我以上是关于位置状态不一致 Android Studio的主要内容,如果未能解决你的问题,请参考以下文章