gpt4 book ai didi

android - 来自服务器的带有数字标记的谷歌地图

转载 作者:行者123 更新时间:2023-11-29 18:54:16 25 4
gpt4 key购买 nike

我想在谷歌地图上显示多个标记。我想在圆形区域中将标记显示为数字。如下图:

enter image description here

对于添加多个标记,我已经完成了:

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));
}
}

}

}

我的结果是:

enter image description here

我知道如何设置自定义标记。但是这个数据是动态的,任何数量的数据都会在那里。所以我想问一下是否有任何方法可以通过任何循环来生成这个数字。因为为每个位置设置标记,我需要数百个标记,这不是很可取。那么有什么选择吗?

注意:我不想要集群。我只想在一个标记上显示 1,在第二个标记上显示 2,...,在百个标记上显示 100,依此类推。

我们将不胜感激。

谢谢你:)

最佳答案

首先你需要为你的标记创建一个自定义布局:

<?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;
}

关于android - 来自服务器的带有数字标记的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50246711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com