我是一名新的Android开发人员。我正在创建谷歌地图和自定义标记与lat长地图和标记工作并在S4手机中显示正确但未在s8或更高版本的移动设备中显示我授予了所有权限。我还在自定义标记上放置了点击监听器。这些都可以在s4或低版本移动设备上正常工作,但在高版本的api mobile中无法正常工作。在此先感谢..
哪里我错了?请帮我
这是我的代码
thymeleaf
答案 0 :(得分:3)
试试这个
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try {
boolean success =
googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(
MainActivity.this,
R.raw.style_json));
if (!success) {
Log.e("google map", "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e("google map", "Can't find style. Error: ", e);
}
mSetUpMap();
}
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION =
Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSE_LOCATION =
Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private Boolean mLocationPermissionsGranted = false;
private GoogleMap mMap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocationPermission();
}
private void initMap(){
Log.d(TAG, "initMap: initializing map");
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MainActivity.this);
}
private void getLocationPermission(){
Log.d(TAG, "getLocationPermission: getting location permissions");
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = true;
initMap();
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: called.");
mLocationPermissionsGranted = false;
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE : {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getApplicationContext(), "Permission
granted", Toast.LENGTH_SHORT).show();
initMap();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission
denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private void mSetUpMap() {
// your method code
}
}