在谷歌地图自定义标记未显示在三星 S8 或 Android 的相同版本 7.0
Posted
技术标签:
【中文标题】在谷歌地图自定义标记未显示在三星 S8 或 Android 的相同版本 7.0【英文标题】:In Google Maps Custom Marker not Showing in Samsung S8 or same version 7.0 in Android 【发布时间】:2018-09-14 20:32:07 【问题描述】:我正在创建一个带有自定义纬度和经度标记的谷歌地图。标记在三星 S4 手机上正常显示,但在 S8 或更高版本上不显示。我为此授予所有许可。我还将点击监听器放在自定义标记上。这些在 S4 或低版本中都可以正常工作,但在高版本 api mobile 中无法正常工作。
我哪里错了?这是我的代码:
if (ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED)
requestPermission();
// 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 false;
else
mSetUpMap();
private void requestPermission()
ActivityCompat.requestPermissions(getActivity(), new
String[]Manifest.permission.ACCESS_FINE_LOCATION, 1);
public void mSetUpMap()
googleMap.clear();
/**Create dummy Markers List*/
List<Marker> markersList = new ArrayList<Marker>();
for (City item : cityList)
if (googleMap != null)
Marker m1 = googleMap.addMarker(new MarkerOptions().position(new
LatLng(item.getLatitude(),
item.getLongitude())).title(item.getName()).anchor(0.5f,
0.5f).icon(BitmapDescriptorFactory.fromBitmap(getCustomMarker(
item.getDrawableId(), item.getName()))));
markersList.add(m1);
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
@Override
public boolean onMarkerClick(final Marker marker)
ValueAnimator ani = ValueAnimator.ofFloat(0, 1); //change for
(0, 1)if you want a fade in
ani.setDuration(2000);
ani.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
@Override
public void onAnimationUpdate(ValueAnimator animation)
marker.setAlpha((float) animation.getAnimatedValue());
);
ani.start();
if (marker.getTitle().equals(cityList.get(0).getName()))
AppUtil.city = cityList.get(0);
else if (marker.getTitle().equals(cityList.get(1).getName()))
AppUtil.city = cityList.get(1);
else if (marker.getTitle().equals(cityList.get(2).getName()))
AppUtil.city = cityList.get(2);
else if (marker.getTitle().equals(cityList.get(3).getName()))
AppUtil.city = cityList.get(3);
else if (marker.getTitle().equals(cityList.get(4).getName()))
AppUtil.city = cityList.get(4);
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, new
CityDetailFragment());
transaction.commit();
/*((CityDetailFragment) adapter.getItem(1)).setupData();
viewPager.setCurrentItem(1,false);*/
return true;
);
/**create for loop for get the latLngbuilder from the marker list*/
builder = new LatLngBounds.Builder();
for (Marker m : markersList)
builder.include(m.getPosition());
/**initialize the padding for map boundary*/
final int padding = 150;
/**create the bounds from latlngBuilder to set into map camera*/
final LatLngBounds bounds = builder.build();
/**create the camera with bounds and padding to set into map*/
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
/**call the map call back to know map is loaded or not*/
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback()
@Override
public void onMapLoaded()
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,
padding));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
);
private Bitmap createStoreMarker()
ViewGroup continer = null;
View markerLayout = view.inflate(null,
R.layout.store_marker_layout, continer);
markerLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED));
markerLayout.layout(0, 0, markerLayout.getMeasuredWidth(),
markerLayout.getMeasuredHeight());
final Bitmap bitmap =
Bitmap.createBitmap(markerLayout.getMeasuredWidth(),
markerLayout.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
markerLayout.draw(canvas);
return bitmap;
【问题讨论】:
如果它显示在 S4 中,则标记代码不是问题。请将您的代码发布在您要求权限的地方。 我授予所有权限 所以你在授予权限后添加标记?你确定是这样吗? 首先我授予所有权限,然后设置地图并添加标记 你的onRequestPermissionsResult
方法在哪里?
【参考方案1】:
试试这个
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
【讨论】:
以上是关于在谷歌地图自定义标记未显示在三星 S8 或 Android 的相同版本 7.0的主要内容,如果未能解决你的问题,请参考以下文章
如何在谷歌地图中创建一个带有气泡聊天背景的自定义标记,以及像调情地图这样的图像右上角的数字?