我想在Google地图上显示多个标记。我想在圆形区域显示标记为数字。如下图所示:
添加多个标记我已经完成了:
Adding multiple markers in Google Maps API v2 Android
Adding Multiple Markers Google Maps
How to show multiple markers on MapFragment in Google Map API v2?
我已通过此代码获得了多个标记:
public class ComplaintsMapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private NetworkManager networkManager = null;
private int reqIdComplaintList = -1;
private ArrayList<ComplaintData> complaintList = new ArrayList<>();
private static final int ZOOM_LEVEL = 15;
private static final int TILT_LEVEL = 0;
private static final int BEARING_LEVEL = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaints_map);
// 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);
networkManager = NetworkManager.getInstance();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
complaintList = (ArrayList<ComplaintData>)getIntent().getSerializableExtra("list");
for (int i = 0; i < complaintList.size(); i++) {
ComplaintData location = complaintList.get(i);
LatLng position = new LatLng(Double.parseDouble(location.getLat()), Double.parseDouble(location.getLng()));
mMap.addMarker(new MarkerOptions().position(position));
if (i == 0) {
CameraPosition camPos = new CameraPosition(position, ZOOM_LEVEL, TILT_LEVEL, BEARING_LEVEL);
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(camPos));
}
}
}
}
我的结果是:
我知道如何设置自定义标记。但是这些数据是动态的,任何数量的数据都将存在。所以我想问一下是否有任何方法可以通过任何循环产生这些数字。因为将标记设置到每个位置,我将需要数百个标记,这是不太优选的。那么有什么选择吗?
注意:我不想要群集。我只想在一个标记上显示1,在第二个标记上显示2,在100个标记上显示100,依此类推。
任何帮助将不胜感激。
谢谢:)
答案 0 :(得分:2)
首先您需要为您的标记创建自定义布局:
<?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/_35sdp"
android:layout_height="@dimen/_45sdp"
android:src="@drawable/ic_marker" />
<TextView
android:id="@+id/num_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingBottom="@dimen/_10sdp"
android:text="20"
android:textColor="@color/white"
android:textSize="@dimen/font_title"
android:textStyle="bold" />
</RelativeLayout>
你的形象可以是一个可绘画的。您可以将imageview或背景带到textview。两者都有效
View marker = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
TextView numTxt = (TextView) marker.findViewById(R.id.num_txt);
Marker myMarker = googleMap.addMarker(new MarkerOptions().position(latLng).title(arrPostList.get(i).getRestaurant_name()).icon(BitmapDescriptorFactory.fromBitmap(Utils.createDrawableFromView(getActivity(), marker))));
从Drawable方法创建位图:
// Convert a view to bitmap
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.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;
}