簡體   English   中英

Android MapView Api-更改位置標記

[英]Android MapView Api - Change location marker

我正在使用com.google.android.gms.maps.MapView,並且我想隱藏當前位置“標記”(下一張圖片中的藍色)

在此處輸入圖片說明

紅色標記是我當前位置的標記,我想放置在位置的中心,現在放置的位置就像是一個向下的箭頭。 你知道我該怎么做嗎?

編輯:我認為有足夠的地方將我的標記放在藍色的標記上,它將覆蓋它。 我怎樣才能做到這一點?

我像這樣放置標記:

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

您可以設置標記圖像的錨點:

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

您可以隱藏我的位置圖層

googleMap.setMyLocationEnabled(false);

您需要這樣做: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;
}
}

在地圖就緒回調中添加您的自定義標記。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM