无法设置 Google 个地图的自定义信息 windows
Unable to set Custom info windows of Google Maps
我正在尝试使用此 tutorial 设置 Custom info windows
。
这是我的代码:
public class MyActivity extends AppCompatActivity implements OnMapReadyCallback, ClickListenerChatFirebase, GoogleMap.InfoWindowAdapter {
public void showMap() {
venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
venueMarker.showInfoWindow();
getInfoWindow(venueMarker);
}
public AcceptedRequest(LayoutInflater inflater){
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
// Getting view from the layout file
// inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.venue_infowindow_background, null);
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
@Override
public View getInfoContents(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
}
}
这是venue_infowindow_background.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/venue_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="venue"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="@android:color/white"/>
</LinearLayout>
问题 没有发生任何变化,信息 window 显示默认的文本颜色和背景颜色。
请帮忙解决这个问题。
您不能直接调用 getInfoWindow()
方法覆盖。
您需要将 Google 映射的 InfoWindowAdapter 设置为您的 Activity,它实现了 GoogleMap.InfoWindowAdapter
接口。
public void showMap() {
venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
venueMarker.showInfoWindow();
//This won't work:
//getInfoWindow(venueMarker);
//Do this instead:
mMap.setInfoWindowAdapter(this);
}
为了对该标记使用不同的布局,您可以为所有其他标记扩展不同的自定义布局:
@Override
public View getInfoWindow(Marker marker) {
View v = null;
if (marker.equals(venueMarker)) {
v = inflater.inflate(R.layout.venue_infowindow_background, null);
} else {
v = inflater.inflate(R.layout.some_other_layout, null);
}
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
为了将 "speech bubble" 与默认标记一起使用,而不是与一个自定义标记一起使用,您可以将 getInfoWindow()
与自定义标记一起使用,并将 getInfoContents()
用于默认标记标记。 (更多信息 )。这样的事情可能会奏效。:
@Override
public View getInfoWindow(Marker marker) {
if (!marker.equals(venueMarker)) {
return null;
}
View v = inflater.inflate(R.layout.venue_infowindow_background, null);
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
@Override
public View getInfoContents(Marker arg0) {
if (marker.equals(venueMarker)) {
return null;
}
View v = inflater.inflate(R.layout.some_other_layout, null);
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
我正在尝试使用此 tutorial 设置 Custom info windows
。
这是我的代码:
public class MyActivity extends AppCompatActivity implements OnMapReadyCallback, ClickListenerChatFirebase, GoogleMap.InfoWindowAdapter {
public void showMap() {
venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
venueMarker.showInfoWindow();
getInfoWindow(venueMarker);
}
public AcceptedRequest(LayoutInflater inflater){
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
// Getting view from the layout file
// inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.venue_infowindow_background, null);
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
@Override
public View getInfoContents(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
}
}
这是venue_infowindow_background.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/venue_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="venue"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="@android:color/white"/>
</LinearLayout>
问题 没有发生任何变化,信息 window 显示默认的文本颜色和背景颜色。
请帮忙解决这个问题。
您不能直接调用 getInfoWindow()
方法覆盖。
您需要将 Google 映射的 InfoWindowAdapter 设置为您的 Activity,它实现了 GoogleMap.InfoWindowAdapter
接口。
public void showMap() {
venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
venueMarker.showInfoWindow();
//This won't work:
//getInfoWindow(venueMarker);
//Do this instead:
mMap.setInfoWindowAdapter(this);
}
为了对该标记使用不同的布局,您可以为所有其他标记扩展不同的自定义布局:
@Override
public View getInfoWindow(Marker marker) {
View v = null;
if (marker.equals(venueMarker)) {
v = inflater.inflate(R.layout.venue_infowindow_background, null);
} else {
v = inflater.inflate(R.layout.some_other_layout, null);
}
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
为了将 "speech bubble" 与默认标记一起使用,而不是与一个自定义标记一起使用,您可以将 getInfoWindow()
与自定义标记一起使用,并将 getInfoContents()
用于默认标记标记。 (更多信息
@Override
public View getInfoWindow(Marker marker) {
if (!marker.equals(venueMarker)) {
return null;
}
View v = inflater.inflate(R.layout.venue_infowindow_background, null);
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}
@Override
public View getInfoContents(Marker arg0) {
if (marker.equals(venueMarker)) {
return null;
}
View v = inflater.inflate(R.layout.some_other_layout, null);
TextView title = (TextView) v.findViewById(R.id.venue_txt);
title.setText(marker.getTitle());
return v;
}