我正在创建一个小应用程序,可以帮助用户找到一个体育俱乐部。根据之前活动中从旋转器中选择的运动类型,地图上将显示一些代表不同俱乐部的标记。(见下面的截图)
我试图在 Google 地图中将开始另一个活动的标记的信息窗口(包含标题和片段)中添加一个按钮。它现在会弹出一个吐司。目前我正在产生以下结果:
我遇到的问题是,当我尝试单击按钮时,我无法与信息窗口中的按钮交互,而是单击了整个信息窗口(导致窗口从白色背景变为灰色背景以显示它已被单击)。无论我尝试单击按钮还是信息窗口的任何其他部分,都会发生这种情况。
为此,我创建了一个 XML 布局文件(custom_window_info.xml),如果单击标记,该文件将显示在地图活动中。它显示在MapActivity.java 类中声明的标题、片段和按钮方法。
然后我创建了一个 java 类(CustomInfoWindowAdapter.java),它为上面提到的布局文件提供了 java 功能,例如将值分配给标题和片段文本字段、getInfoWindow() 和 getInfoContents() 方法等。
我不确定我是否完全以错误的方式处理了这个问题,或者我犯了一个小错误。任何提示/建议?
custom_window_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#000"
android:textSize="14sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:id="@+id/snippet"
android:maxLines="5" />
<Button
android:id="@+id/joinCLub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClubButtonClick"
android:text="Join Club"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
自定义信息窗口适配器.java
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private final View mWindow;
private Context mContext;
public CustomInfoWindowAdapter(Context context) {
this.mContext = mContext;
this.mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window, null);
}
private void rendowWindowText(Marker marker, View v){
String title = marker.getTitle();
TextView clubtitle = (TextView) v.findViewById(R.id.title);
if(!title.equals(" ")){
clubtitle.setText(title);
}
String snippet = marker.getSnippet();
TextView clubsnippet = (TextView) v.findViewById(R.id.snippet);
if(!title.equals(" ")){
clubsnippet.setText(snippet);
}
}
@Override
public View getInfoWindow(Marker marker) {
rendowWindowText(marker, mWindow);
return mWindow;
}
@Override
public View getInfoContents(Marker marker) {
rendowWindowText(marker, mWindow);
return mWindow;
}
}
地图活动
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Assigning coordinates to a club (I've left out the 4 other markers to reduce the code)
LatLng vincentsGAA = new LatLng(53.374042,-6.2300634);
// Adding marker to map with the coordinates declared above, a title for the name of the club and a snippet for the address
mMap.addMarker(new MarkerOptions().position(vincentsGAA).title("St Vincents GFC").snippet("Malahide Road, Dublin"));
//Assigning the default coordinates to Dublin, Ireland when the map opens
LatLng dublin = new LatLng(53.3242381,-6.385785);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dublin, 10));
// Sets the custom info window to this map activity's markers
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivity.this));
}
// This is the back button in the top right of the map activity that will
// bring the user back to the previous activity that had the spinner that
// allows the user to pick the type of sport they want to see clubs for
public void ButtonClick(View v) {
Intent intent1 = new Intent(this, Welcome_Activity.class);
startActivity(intent1);
}
}
实际上,这是不可能的,Google Map 会将您自定义的 InfoWindow 的内容呈现为图像并将其作为图像显示给您。因此,您仅将单击侦听器设置为整个窗口,而不是其中的视图。
您唯一的选择是将 ClickListener 设置为整个 InfoWindow 并弹出一个包含您想要的可点击内容的 Dialog,而不是直接在 InfoWindow 内进行。
来自 Google 文档:https : //developers.google.com/maps/documentation/android/marker#info_windows
注意:绘制的信息窗口不是实时视图。视图在返回时呈现为图像(使用 View.draw(Canvas))。这意味着对视图的任何后续更改都不会反映在地图上的信息窗口中。要稍后更新信息窗口(例如,加载图像后),请调用 showInfoWindow()。此外,信息窗口将不考虑正常视图的任何典型交互,例如触摸或手势事件。但是,您可以在整个信息窗口上收听通用点击事件,如下节所述。`
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句