2013-05-09 25 views
1

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

有人可以试试它,请告诉我该怎么做。我想在应用程序中使用像按钮这样的标记。 有可能吗?

+0

您要添加额外的控制,而不是标记? – 2013-05-09 13:44:21

+0

我只想在地图上显示小图标,通过点击它们我想做一些操作 – Raja 2013-05-09 13:46:29

回答

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类

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

最好的解决办法是使用在地图的前面设置覆盖的FrameLayoutRelativeLayout的,然后在使用它你活动。

您的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> 
+0

感谢Bhavesh,你给我整个想法 – Raja 2013-05-09 14:05:49