1

我想在 GoogleMap 上添加标记,例如 UI 控件(缩放控件、指南针、MyLocation 按钮)。这样当我在地图屏幕上滑动时,这些位置不会改变。
https://developers.google.com/maps/documentation/android/interactivity

任何人都可以尝试过,请建议我如何做到这一点。我想在我的应用程序中使用这些标记,例如按钮。是否可以 ?

4

2 回答 2

2

最好的解决方案是使用FrameLayoutRelativeLayout在地图前面设置叠加层,然后在 Activity 中使用它。

您的 xml 将如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>

    <View
        android:id="@+id/your_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#abc" />

<fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:scrollbars="vertical" />

</LinearLayout>
于 2013-05-09T13:45:51.523 回答
1

这是我用来在 MapView 顶部覆盖按钮的代码。

安卓布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+id/home_container"  
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">

    <com.google.android.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="YOURAPIKEY"    
        android:clickable="true"
    />

        <com.kyght.yourproject.TransparentPanel
                android:gravity="center_horizontal"
                android:id="@+id/transparent_panel" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp">

            <Button android:id="@+id/mapbtncenterme" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:padding="5dp"
            android:drawableTop="@drawable/ic_menu_mylocation"
            android:text="Find ME"/>        

Java 类

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class TransparentPanel extends LinearLayout 
{ 
    private Paint   innerPaint, borderPaint ;

    public TransparentPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TransparentPanel(Context context) {
        super(context);
        init();
    }

    private void init() {
        innerPaint = new Paint();
        innerPaint.setARGB(225, 75, 75, 75); //gray
        innerPaint.setAntiAlias(true);

        borderPaint = new Paint();
        borderPaint.setARGB(255, 255, 255, 255);
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Style.STROKE);
        borderPaint.setStrokeWidth(2);
    }

    public void setInnerPaint(Paint innerPaint) {
        this.innerPaint = innerPaint;
    }

    public void setBorderPaint(Paint borderPaint) {
        this.borderPaint = borderPaint;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {

        RectF drawRect = new RectF();
        drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());

        canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
        canvas.drawRoundRect(drawRect, 5, 5, borderPaint);

        super.dispatchDraw(canvas);
    }
}
于 2013-05-09T13:45:41.953 回答