gpt4 book ai didi

java - 自定义 map 标记

转载 作者:行者123 更新时间:2023-11-30 02:15:42 32 4
gpt4 key购买 nike

我有一个带标记的 map View 。我想知道是否可以向标记添加几个其他字符串值,例如电话号码和网站。我不希望这些在点击标记时显示在信息窗口中。当点击信息窗口时,它会转到所选标记的详细 Activity 。标记标题和代码段作为附加项传递给详细信息 Activity ,我想将另外两个字符串也作为附加项传递。

这是我创建标记的地方:

for(int i = 0; i < Lat.length; i++) {
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(Lat[i], Lon[i]))
.title(Market[i])
.snippet(Address[i])
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

list.add(marker);
}

这是我开始详细 Activity 的地方。

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
// Show Details
Intent intent = new Intent(getActivity(), FarmMarketDetails.class);
intent.putExtra("selectedTitle", marker.getTitle());
intent.putExtra("selectedAddress", marker.getSnippet());
startActivity(intent);
}
});

最佳答案

我使用以下代码创建了自定义标记,请检查它是否对您有帮助

 Marker marki=map.addMarker(new MarkerOptions()  
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.my, "")))
.position((new LatLng(latitude,longitude)))) ;

自定义标记的方法

private Bitmap writeTextOnDrawable(int drawableId, String text) {

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);

Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

Paint paint = new Paint();

paint.setColor(Color.BLUE);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(convertToPixels(context,11));

Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);

Canvas canvas = new Canvas(bm);

//If the text is bigger than the canvas , reduce the font size
if(textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
paint.setTextSize(convertToPixels(context,7)); //Scaling needs to be used for different dpi's

//Calculate the positions
int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset

//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;

canvas.drawText(text, xPos, yPos, paint);

return bm;
}

public static int convertToPixels(Context context, int nDP)
{
final float conversionScale = context.getResources().getDisplayMetrics().density;

return (int) ((nDP * conversionScale) + 0.5f) ;

}

关于java - 自定义 map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392180/

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