Android MapView Api - 更改位置标记

时间:2016-11-11 20:51:54

标签: android google-maps android-mapview currentlocation

我正在使用com.google.android.gms.maps.MapView,我想要隐藏当前位置"标记" (下一张图片中的蓝色)

enter image description here

红色的是我当前位置的标记,我想放在我所在位置的中心,现在放置就像是指向下方的箭头。你知道我该怎么办?

编辑:我认为有足够的标记放在蓝色标记上,它会覆盖它。我怎么能这样做?

我按照这样放置我的标记:

  

currentPositionMarker = googleMap.addMarker(new MarkerOptions()。position(location).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

2 个答案:

答案 0 :(得分:1)

您可以设置标记图像的锚点:

currentPositionMarker = 
    googleMap.addMarker(
        new MarkerOptions()
            .position(location)
            .anchor(0.5f, 0.5f)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

您可以隐藏我的位置图层

googleMap.setMyLocationEnabled(false);

答案 1 :(得分:0)

你需要这样做: view_custom_marker.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_marker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/marker_mask">

<ImageView
    android:id="@+id/profile_image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center_horizontal"
    android:contentDescription="@null"
    android:src="@drawable/avatar" />
 </FrameLayout>

使用下面的代码将此视图转换为位图

    private Bitmap getMarkerBitmapFromView(@DrawableRes int resId) {

    View customMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_custom_marker, null);
    ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.profile_image);
    markerImageView.setImageResource(resId);
    customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
    Drawable drawable = customMarkerView.getBackground();
    if (drawable != null)
        drawable.draw(canvas);
    customMarkerView.draw(canvas);
    return returnedBitmap;
}
}

在Map ready回调中添加自定义标记。

@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady() called with");
mGoogleMap = googleMap;
MapsInitializer.initialize(this);
addCustomMarker();
}
private void addCustomMarker() {
Log.d(TAG, "addCustomMarker()");
if (mGoogleMap == null) {
    return;
}

// adding a marker on map with image from  drawable
mGoogleMap.addMarker(new MarkerOptions()
.position(mDummyLatLng).icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(R.drawable.avatar))));
}