Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Google Map版本2中,将drawable中的图像设置为标记_Android_Marker_Android Maps V2 - Fatal编程技术网

Android 在Google Map版本2中,将drawable中的图像设置为标记

Android 在Google Map版本2中,将drawable中的图像设置为标记,android,marker,android-maps-v2,Android,Marker,Android Maps V2,我使用这部分代码在GoogleMapVersion2的MapFragment中添加了一个标记 MarkerOptions op = new MarkerOptions(); op.position(point) .title(Location_ArrayList.get(j).getCity_name()) .snippet(Location_ArrayList.get(j).getVenue_name()) .draggable(true); m = map.addMa

我使用这部分代码在GoogleMapVersion2的
MapFragment
中添加了一个标记

MarkerOptions op = new MarkerOptions();
op.position(point)
    .title(Location_ArrayList.get(j).getCity_name())
    .snippet(Location_ArrayList.get(j).getVenue_name())
    .draggable(true);
m = map.addMarker(op); 
markers.add(m);

我想使用不同于我的drawable的图像。

这就是如何将
drawable
设置为
标记的方法

BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.current_position_tennis_ball)

MarkerOptions markerOptions = new MarkerOptions().position(latLng)
         .title("Current Location")
         .snippet("Thinking of finding some thing...")
         .icon(icon);

mMarker = googleMap.addMarker(markerOptions);


VectorDrawables
和基于
XML
Drawables
可以使用此功能。

如果您已经通过编程方式创建了
Drawables
(因此您没有相关资源),则可以使用此功能:

Drawable d = ... // programatically create drawable
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
d.draw(canvas);
BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bitmap);

然后您就有了
位图描述符
,您可以将其传递到
标记选项

@Lukas Novak answer没有显示任何内容,因为您还必须设置
可绘制
的边界。
这适用于任何可拉伸的。下面是一个完全有效的示例:


circle_shape.xml


基本上使用位图而不是可绘制的。如果您有可绘制的,请将其转换为位图。
下面是kotlin中的一个代码示例

private fun placeNewMarker(whereToPlaceYourMarker: LatLng){

    val pickupMarkerDrawable = resources.getDrawable(R.drawable.your_marker_drawable,null)


    _pickupMarker =   mMap?.addMarker(MarkerOptions()
            .position(whereToPlaceYourMarker)
            .draggable(true)
            .title("My Marker")
            .icon(BitmapDescriptorFactory.fromBitmap(pickupMarkerDrawable.toBitmap(pickupMarkerDrawable.intrinsicWidth, pickupMarkerDrawable.intrinsicHeight, null))))



}

如果使用的是
矢量可绘制
则使用此扩展名:

fun  Context.bitmapDescriptorFromVector(vectorResId:Int): BitmapDescriptor {
    val vectorDrawable = ContextCompat.getDrawable(this, vectorResId)
    vectorDrawable!!.setBounds(0, 0, vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight)
    val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
    vectorDrawable.draw(Canvas(bitmap))
    return BitmapDescriptorFactory.fromBitmap(bitmap)
}
并设置为标记图标:

val markerOptions = MarkerOptions().position(latLng)
         .title("Current Location")
         .snippet("Thinking of finding some thing...")
         .icon(bitmapDescriptorFromVector("Your icon resource id"))// call extension here

mMarker = googleMap.addMarker(markerOptions)

Do
BitmapDescriptor图标=BitmapDescriptorFactory.fromResource(R.drawable.current\u position\u tennis\u ball)
然后是op.icon(图标);Babar thanx先生,你的回答很好,为我工作…你把它作为一个答案发布,我会接受它。。。“又是Thanx了。”穆罕默德·巴巴尔,我们可以在title@muhammad我已经看过了,但我不能这么做……但它可以扣人心弦……@muhammad我想在图片中传递标题这是正确的,尽管使用“any”drawable这个词是不正确的。这只允许您设置BitmapDrawables。例如,你不能用这个从xml设置一个可绘制的。你可以-你只需要先将它绘制到
画布上(
drawable.draw(Canvas)
),然后将
Canvas
转储到
位图上。伙计们,我现在真的不知道这个!因此,让投票决定谁是正确的:)如果是关于drawable的问题,这是一个很好的问题,已经在代码隐藏中修改过了,但是现在是一个“按下自动完成”的问题。这为我创建了空图像。请参考@vovahost发布的示例,了解此方法的完整工作示例。这个答案不太完整。我的第一个标记画得比其他标记大,我使用的代码与您完全相同。原因可能是什么?它独立于我和其他人试过的图像源。我不知道。发布一些代码:如果以编程方式创建可绘制的xml或用于创建可绘制的cose。@vovahost您可以发布圆\u shape.xml的代码吗。出于某种原因,circle.getIntrinsicHeight/width返回0 For meUpvote,用于显示如何将可绘制的向量设置为标记iconThanks@Machado
fun  Context.bitmapDescriptorFromVector(vectorResId:Int): BitmapDescriptor {
    val vectorDrawable = ContextCompat.getDrawable(this, vectorResId)
    vectorDrawable!!.setBounds(0, 0, vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight)
    val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
    vectorDrawable.draw(Canvas(bitmap))
    return BitmapDescriptorFactory.fromBitmap(bitmap)
}
val markerOptions = MarkerOptions().position(latLng)
         .title("Current Location")
         .snippet("Thinking of finding some thing...")
         .icon(bitmapDescriptorFromVector("Your icon resource id"))// call extension here

mMarker = googleMap.addMarker(markerOptions)