我尝试创建自定义位图标记并将其放置在Google地图上的特定Lat-Lng上,因为我已经编写了以下代码 -
我的自定义标记视图是(user_prof_pic_marker.xml) -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="@dimen/sz_sixty_five"
android:layout_height="@dimen/sz_sixty_five"
android:layout_centerInParent="true"
android:src="@drawable/map_marker" />
<com.wikiNews.custom.CircleImageView
android:id="@+id/myImage"
android:layout_width="@dimen/sz_fifty"
android:layout_height="@dimen/sz_fifty"
android:layout_centerInParent="true"
android:src="@drawable/profiledefaultimg" />
</RelativeLayout>
这就是我将图像设置为自定义视图的方式 -
View myUserMarkerLayout = getLayoutInflater().inflate(R.layout.user_prof_pic_marker, null);
myUserImageView = (CircleImageView) myUserMarkerLayout.findViewById(R.id.myImage);
myUserImageView.setBorderColor(Color.WHITE);
myUserImageView.setBorderWidth(2);
//String strPic = UserDetails.getInstance().getCurrentProfilePic();
if(UserDetails.getInstance().getCurrentProfilePic()!=null && !UserDetails.getInstance().getCurrentProfilePic().isEmpty())
{
Picasso.with(this).load(UserDetails.getInstance().getCurrentProfilePic()).into(myUserImageView);
}
我从自定义视图创建BitMap的方法是 -
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
我添加标记的代码是 -
if (googleMap != null)
{
final LatLng currentAddressLatLong = new LatLng(gps.getLatitude(), gps.getLongitude());
//LatLng currentAddressLatLong = new LatLng(latitude, longitude);
Bitmap markerUserBitmap = CommonFunctions.createDrawableFromView(NearByActivity.this, myUserMarkerLayout);
googleMap.addMarker(new MarkerOptions().position(currentAddressLatLong).icon(BitmapDescriptorFactory.fromBitmap(markerUserBitmap)).anchor(0.5f, 1));
}
我检查过,我的代码成功创建了预期的位图,并且在谷歌地图上添加标记的代码正在成功执行,但标记在地图上不可见。如果我可以提供更多相关信息,请指导我并告诉我。谢谢。