我想在GoogleMap上添加标记,例如UI控件(缩放控件,指南针,MyLocation按钮)。所以当我在地图屏幕上滑动时,这些位置不会改变 https://developers.google.com/maps/documentation/android/interactivity
之前有人试过吗,请建议我怎么做。我想在我的应用程序中使用像按钮这样的标记。 有可能吗?答案 0 :(得分:2)
最佳解决方案是使用 FrameLayout 或 RelativeLayout 在地图前设置叠加,然后在您的活动中使用它。
你的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>
答案 1 :(得分:1)
以下是我用来覆盖MapView顶部按钮的代码。
Android布局
<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 Class
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);
}
}