GoogleMaps setMyLocationEnabled(true) 不更新 (Genymotion)
GoogleMaps setMyLocationEnabled(true) not updating (Genymotion)
我正在使用新 Android API 23
的权限更新我的代码。如果我想显示用户的位置,我只需要 map.setMyLocationEnabled(true)
。如果我有权限,我可以看到用户的位置,如果我没有权限,我不会进行此 API 调用,但是在请求权限并且用户接受授予位置权限后,我无法使用用户的更新地图地点。我尝试了两种方法:
方法一(重建地图):
private void populateMap() {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
...
if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
map.setMyLocationEnabled(true);
} else {
request permissions...
}
...
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
populateMap();
}
return;
}
}
}
方法二(使用地图实例并更新):
private void populateMap() {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
...
if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
map.setMyLocationEnabled(true);
} else {
request permissions...
}
...
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
if (mMap!= null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
return;
}
}
}
在这两种方法中,都会显示位置按钮,但不会显示带有用户位置的蓝点。
我在这里错过了什么?谢谢
ps:当然,如果我杀死我的片段并再次启动它,一切正常,当我必须提示用户权限,然后在用户授权后用我的位置更新地图时,就会发生这个问题。
能否使用以下代码并将结果反馈给我:-
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
/**
* Id to identity READ_CONTACTS permission request.
*/
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
@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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Sets the map type to be "hybrid"
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// 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 (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ACCESS_FINE_LOCATION);
}
return;
}
mMap.setMyLocationEnabled(true);
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_ACCESS_FINE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startActivity(getIntent());
finish();
}
}
}
}
所以,我正在使用 Genymotion
进行测试,因为我无法使用 Android
6.0 的设备,但现在,我借了一个,我已经测试了这段代码在该物理设备中,我很高兴地注意到它可以正常工作。
猜猜这是一个 Genymotion
问题。哦,好的,代码有效。
谢谢!
我正在使用新 Android API 23
的权限更新我的代码。如果我想显示用户的位置,我只需要 map.setMyLocationEnabled(true)
。如果我有权限,我可以看到用户的位置,如果我没有权限,我不会进行此 API 调用,但是在请求权限并且用户接受授予位置权限后,我无法使用用户的更新地图地点。我尝试了两种方法:
方法一(重建地图):
private void populateMap() {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
...
if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
map.setMyLocationEnabled(true);
} else {
request permissions...
}
...
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
populateMap();
}
return;
}
}
}
方法二(使用地图实例并更新):
private void populateMap() {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
...
if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
map.setMyLocationEnabled(true);
} else {
request permissions...
}
...
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
if (mMap!= null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
return;
}
}
}
在这两种方法中,都会显示位置按钮,但不会显示带有用户位置的蓝点。 我在这里错过了什么?谢谢
ps:当然,如果我杀死我的片段并再次启动它,一切正常,当我必须提示用户权限,然后在用户授权后用我的位置更新地图时,就会发生这个问题。
能否使用以下代码并将结果反馈给我:-
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
/**
* Id to identity READ_CONTACTS permission request.
*/
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
@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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Sets the map type to be "hybrid"
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// 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 (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ACCESS_FINE_LOCATION);
}
return;
}
mMap.setMyLocationEnabled(true);
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_ACCESS_FINE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startActivity(getIntent());
finish();
}
}
}
}
所以,我正在使用 Genymotion
进行测试,因为我无法使用 Android
6.0 的设备,但现在,我借了一个,我已经测试了这段代码在该物理设备中,我很高兴地注意到它可以正常工作。
猜猜这是一个 Genymotion
问题。哦,好的,代码有效。
谢谢!