2013-04-10 205 views
1

我想在显示的GoogleMap上添加一个按钮。我的地图显示为如何将按钮添加到GoogleMap上

GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

我使用该片段来显示地图和布局XML是

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

我发现一些信息来加载按钮拖到视图,但是他们仅仅是一个普通视图而不是GoogleMap。 谢谢

回答

1

不能直接添加一个按钮到谷歌地图的对象,所以你有几种选择:

覆盖的SupportMapFragment对象与自己的实现,并添加按钮,将其。

2.易于选项是设置fragment一个FrameLayout内,并使用LinearLayout或直接添加一个按钮此FrameLayout,例如:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

     <Button 
      android:id="@+id/bLocateMe" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/locate_me_button_selector" 
      android:clickable="true" 
      android:onClick="locateMeButtonOnClick" 
      android:text="@string/locate_me" 
      android:textColor="@color/my_white" /> 
    </LinearLayout> 
    <fragment 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 
+0

这是按钮和地图在同一时间加载。是否有可能以编程方式加载按钮,以便我可以加载时间。 – Bryanyan 2013-04-10 15:04:57

+0

您可以使用相同的布局并将按钮的可见性设置为不可见,然后在需要时更改其可见性。这样它会在你想要的时候出现。 – 2013-04-10 15:18:55

+0

我使用map =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap();来加载地图。如何加载整个布局? – Bryanyan 2013-04-11 13:34:27

0

为什么要在地图上放置按钮?

我有一些解决方案:

  1. 如果你想静态按钮,您可以将地图片段插入RelativeView和地点按钮RelativeView。
  2. 否则,如果你想要的按钮,这将与地图一起移动,你可以在地图上放置一些形状,例如..使用addCircle方法圈选并使用setOnMapClickListener处理地图上的点击。见GoogleMap API Reference
+0

如果是这样,我怎样才能显示静态文本?我可以以编程方式添加textview吗?我只是想在必要时加载textview。 – Bryanyan 2013-04-10 15:59:49

+0

为此,您必须使用Paint对象来创建所需文本的位图,然后使用该位图作为图标或图像在googlemap上放置标记/地面叠加层。 – 2014-08-07 18:47:28