Java 在应用程序中激活位置后使用地理位置按钮时出现问题
我有一个大问题已经四个月了,我不知道该怎么解决。当我启动我的应用程序时,应用程序会向用户要求是否要激活该应用程序(这很重要),如果他说“不”,则什么也不会发生,但应用程序稍后会问他,如果他说“是”,则应用程序将打开应用程序的设置位置,用户需要在手机设置上按“激活位置” 问题是,在激活location(它不显示userLocation bc,我不知道怎么做,这不是我的首要任务)后,当你点击GeoLocationButton时,你需要按3次按钮来显示用户的位置(有时2次或4次,视情况而定,我不知道为什么) 这是示例--> 一旦创建代码Java 在应用程序中激活位置后使用地理位置按钮时出现问题,java,android,firebase,geolocation,userlocation,Java,Android,Firebase,Geolocation,Userlocation,我有一个大问题已经四个月了,我不知道该怎么解决。当我启动我的应用程序时,应用程序会向用户要求是否要激活该应用程序(这很重要),如果他说“不”,则什么也不会发生,但应用程序稍后会问他,如果他说“是”,则应用程序将打开应用程序的设置位置,用户需要在手机设置上按“激活位置” 问题是,在激活location(它不显示userLocation bc,我不知道怎么做,这不是我的首要任务)后,当你点击GeoLocationButton时,你需要按3次按钮来显示用户的位置(有时2次或4次,视情况而定,我不知道为
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_map_priere);
supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.google_map);
//location
client = LocationServices.getFusedLocationProviderClient(layout_map_priere.this);
//check Permissions
if (ActivityCompat.checkSelfPermission(layout_map_priere.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(layout_map_priere.this,
Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
//When both permission granted
//Call method
getCurrentLocation();
}else {
//When permissions denied
//Request permission
ActivityCompat.requestPermissions(layout_map_priere.this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
}, 100);
}
trackButton();
}
我的自定义地理定位按钮
private void geoButton() {
locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
}
private void trackButton() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ImageButton camera_track = (ImageButton) findViewById(R.id.back_to_camera_tracking_mode);
geoButton();
camera_track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
getCurrentLocation();
}else {
buildAlertMessageNoGps();
}
}
});
}
@SuppressLint("MissingPermission")
private void getCurrentLocation() {
//Initialize task Location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//check Condition
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
//When Location service is enabled
//Get Last Location
client.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
//Check condition
if (location != null){
//Sync map
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
//initialize Lat Lng
LatLng myposition = new LatLng(location.getLatitude(), location.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(myposition, 12));
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
View toolbar = ((View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("4"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
map.getUiSettings().setCompassEnabled(false);
map.getUiSettings().setMapToolbarEnabled(false);
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
final LinearLayout layout_scroll = (LinearLayout) findViewById(R.id.layout_scroll);
final ViewGroup.LayoutParams params = layout_scroll.getLayoutParams();
layout_scroll.setVisibility(View.INVISIBLE);
params.height = 0;
layout_scroll.setLayoutParams(params);
final LinearLayout layout_more_block = (LinearLayout) findViewById(R.id.layout_more_block);
final ViewGroup.LayoutParams params_more = layout_more_block.getLayoutParams();
layout_more_block.setVisibility(View.INVISIBLE);
params_more.height = 0;
layout_more_block.setLayoutParams(params_more);
Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
layout_more_block.startAnimation(slideDown);
}
});
}
});
}else {
LocationRequest locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10000)
.setFastestInterval(1000)
.setNumUpdates(1);
//Initialize location call back
LocationCallback locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
//initalize location
Location location1 = locationResult.getLastLocation();
}
};
//Request location updates
client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
}
});
}else {
buildAlertMessageNoGps();
}
}
可点击的地理定位按钮
private void geoButton() {
locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
}
private void trackButton() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ImageButton camera_track = (ImageButton) findViewById(R.id.back_to_camera_tracking_mode);
geoButton();
camera_track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
getCurrentLocation();
}else {
buildAlertMessageNoGps();
}
}
});
}
@SuppressLint("MissingPermission")
private void getCurrentLocation() {
//Initialize task Location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//check Condition
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
//When Location service is enabled
//Get Last Location
client.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
//Check condition
if (location != null){
//Sync map
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
//initialize Lat Lng
LatLng myposition = new LatLng(location.getLatitude(), location.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(myposition, 12));
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
View toolbar = ((View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("4"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
map.getUiSettings().setCompassEnabled(false);
map.getUiSettings().setMapToolbarEnabled(false);
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
final LinearLayout layout_scroll = (LinearLayout) findViewById(R.id.layout_scroll);
final ViewGroup.LayoutParams params = layout_scroll.getLayoutParams();
layout_scroll.setVisibility(View.INVISIBLE);
params.height = 0;
layout_scroll.setLayoutParams(params);
final LinearLayout layout_more_block = (LinearLayout) findViewById(R.id.layout_more_block);
final ViewGroup.LayoutParams params_more = layout_more_block.getLayoutParams();
layout_more_block.setVisibility(View.INVISIBLE);
params_more.height = 0;
layout_more_block.setLayoutParams(params_more);
Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
layout_more_block.startAnimation(slideDown);
}
});
}
});
}else {
LocationRequest locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10000)
.setFastestInterval(1000)
.setNumUpdates(1);
//Initialize location call back
LocationCallback locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
//initalize location
Location location1 = locationResult.getLastLocation();
}
};
//Request location updates
client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
}
});
}else {
buildAlertMessageNoGps();
}
}
获取用户位置
private void geoButton() {
locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
}
private void trackButton() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ImageButton camera_track = (ImageButton) findViewById(R.id.back_to_camera_tracking_mode);
geoButton();
camera_track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
getCurrentLocation();
}else {
buildAlertMessageNoGps();
}
}
});
}
@SuppressLint("MissingPermission")
private void getCurrentLocation() {
//Initialize task Location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//check Condition
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
//When Location service is enabled
//Get Last Location
client.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
//Check condition
if (location != null){
//Sync map
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
//initialize Lat Lng
LatLng myposition = new LatLng(location.getLatitude(), location.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(myposition, 12));
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
View toolbar = ((View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("4"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
map.getUiSettings().setCompassEnabled(false);
map.getUiSettings().setMapToolbarEnabled(false);
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
final LinearLayout layout_scroll = (LinearLayout) findViewById(R.id.layout_scroll);
final ViewGroup.LayoutParams params = layout_scroll.getLayoutParams();
layout_scroll.setVisibility(View.INVISIBLE);
params.height = 0;
layout_scroll.setLayoutParams(params);
final LinearLayout layout_more_block = (LinearLayout) findViewById(R.id.layout_more_block);
final ViewGroup.LayoutParams params_more = layout_more_block.getLayoutParams();
layout_more_block.setVisibility(View.INVISIBLE);
params_more.height = 0;
layout_more_block.setLayoutParams(params_more);
Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
layout_more_block.startAnimation(slideDown);
}
});
}
});
}else {
LocationRequest locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10000)
.setFastestInterval(1000)
.setNumUpdates(1);
//Initialize location call back
LocationCallback locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
//initalize location
Location location1 = locationResult.getLastLocation();
}
};
//Request location updates
client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
}
});
}else {
buildAlertMessageNoGps();
}
}
我真的需要您的帮助,我为什么会遇到这个问题,以及我如何解决它我认为“manager.isProviderEnabled()”返回false,因为它需要一些时间来激活GPS位置。您可以检查一下,当您第一次单击按钮时,您是否可以切换到方法
getCurrentLocation()
?是的,我到达了getCurrentLocation方法,但它在if(location!=null)之前停止。是的,我想这就是问题所在,但我如何解决这个问题,这不是一个好的用户体验嗯,是的,我认为这不是一个好的用户体验。一个可行的解决方案是使用requestLocationUpdates()
方法,而不是getLastLocation()
,并在收到第一个位置时向用户显示加载对话框。我认为这是一个非常好的解决方案,感谢您的帮助!我认为“manager.isProviderEnabled()”返回false,因为它需要一些时间来激活GPS位置。您可以检查一下,当您第一次单击按钮时,您是否可以切换到方法getCurrentLocation()
?是的,我到达了getCurrentLocation方法,但它在if(location!=null)之前停止。是的,我想这就是问题所在,但我如何解决这个问题,这不是一个好的用户体验嗯,是的,我认为这不是一个好的用户体验。一个可行的解决方案是使用requestLocationUpdates()
方法,而不是getLastLocation()
,并在收到第一个位置时向用户显示加载对话框。我认为这是一个非常好的解决方案,感谢您的帮助!