本文介绍了Google地图中的自定义标记不显示在Samsung S8或同一版本7.0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一名新的Android开发者。我创建谷歌地图和自定义标记与纬度长的地图和标记工作和显示适当的S4手机,但没有显示在s8或更高版本的手机我授予所有权限。我也把点击监听器在自定义标记。这些在s4或低版本手机中都可以正常工作,但不适用于高版本api手机。在此先感谢..
我错了吗?请帮助我
这里是我的代码
$ public $ mSetUpMap(){
googleMap.clear() ;
/ **创建虚拟标记列表* /
列表<标记> markersList = new ArrayList< Marker>();
(City item:cityList)
{
if(googleMap!= null){
Marker m1 = googleMap.addMarker(new MarkerOptions()。position新的
LatLng(item.getLatitude(),
item.getLongitude()))。title(item.getName())。anchor(0.5f,
0.5f).icon(BitmapDescriptorFactory .fromBitmap(getCustomMarker(
item.getDrawableId(),item.getName()))));
markersList.add(m1);
$ b googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(final Marker marker ){
ValueAnimator ani = ValueAnimator.ofFloat(0,1); //如果您想要在
中淡入,请更改
(0,1)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);
$ b else if(marker.getTitle()。equals(cityList.get(2).getName()))
{
AppUtil.city = cityList获得(2);
$ b else if(marker.getTitle()。equals(cityList.get(3).getName()))
{
AppUtil.city = cityList获得(3);
$ b else if(marker.getTitle()。equals(cityList.get(4).getName()))
{
AppUtil.city = cityList获得(4);
FragmentTransaction transaction =
getFragmentManager()。beginTransaction();
transaction.replace(R.id.frame_container,new
CityDetailFragment());
transaction.commit();
/ *((CityDetailFragment)adapter.getItem(1))。setupData();
viewPager.setCurrentItem(1,false); * /
返回true;
}
});
/ **创建for循环以从标记列表中获取latLngbuilder * /
builder = new LatLngBounds.Builder(); (Marker m:markersList){
builder.include(m.getPosition());
}
/ **初始化地图边界的填充* /
final int padding = 150;
/ **从latlngBuilder创建边界以设置为地图相机* /
final LatLngBounds bounds = builder.build();
/ **创建带有边界和填充的摄像头以设置为地图* /
cu = CameraUpdateFactory.newLatLngBounds(bounds,padding);
/ **调用地图回调以知道地图是否加载* /
在此输入代码
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded(){
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,
padding));
googleMap.animateCamera(CameraUpdateFactory.zoomTo (12));
}
});
私人位图createStoreMarker(){
ViewGroup continer = null;
查看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());
最终位图位图=
Bitmap.createBitmap(markerLayout.getMeasuredWidth(),
markerLayout.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
画布画布=新画布(位图);
markerLayout.draw(canvas);
返回位图;
解决方案
试试这个
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback {
@Override
public void onMapReady( GoogleMap googleMap){
mMap = googleMap;
尝试{
布尔成功=
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 not 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;
私有布尔mLocationPermissionsGranted = false;
私有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;
$ b switch(requestCode){
case LOCATION_PERMISSION_REQUEST_CODE:{
//如果请求被取消,结果数组是空的。
if(grantResults.length> 0
&&& amp; grantResults [0] ==
PackageManager.PERMISSION_GRANTED){
//许可被授予, !做你需要做的
//联系人相关的任务。
Toast.makeText(getApplicationContext(),Permission
granted,Toast.LENGTH_SHORT).show();
initMap();
} else {
// permission denied,boo!禁用依赖此权限的
//功能。
Toast.makeText(getApplicationContext(),Permission
denied,Toast.LENGTH_SHORT).show();
}
return;
private void mSetUpMap(){
//你的方法代码
}
}
I am a new Android Developer. I am creating google map and custom marker with lat long map and markers work and show proper in S4 mobile but not shown in s8 or higher version mobile I grant all permission for that.I also put click listener on Custom Marker. These all work fine in s4 or low version mobile but not work in high version api mobile. Thanks in Advance..
where i am wrong ? please help me Here is my code
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*/
enter code here
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;
}
解决方案
Try This
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
}
}
这篇关于Google地图中的自定义标记不显示在Samsung S8或同一版本7.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!