我有一个名为AnimalMarker的类,它应该代表普通谷歌地图标记(纬度,长度,位置)和对象动物的最低限度。我正在使用Firebase实时数据库来存储这些AnimalMarkers。
我想做的,但不知道该怎么做,是创建一个自定义的InfoWindow(我创建了新的布局和一个应该在MapActivity中传递的适配器)即使绑定到谷歌地图标记,应该从该Marker的相应AnimalMarker获取数据。
我的MapActivity的逻辑是:
我现在意识到,不是只保存列表中的google-maps标记,而是Hashmap <AnimalMarker,Marker>会更好,并在数据库监听器注意到更新时更新Hashmap但即便如此,我也不会知道如何使InfoWindowAdapter从Marker的关联AnimalMarker获取数据。
这是我的代码:
MapActivity
public class MapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.OnConnectionFailedListener {
...
private void initListeners() {
Log.d(TAG, "initListeners: initializing SearchText");
firebaseAuth = FirebaseAuth.getInstance();
geocoder = new Geocoder(MapActivity.this);
markersDatabase = new MarkersDatabase();
myMarkersList = new ArrayList<>();
allMarkersList = new ArrayList<>();
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener(){
@Override
public void onMapLongClick(LatLng latLng) {
List<Address> addresses = new ArrayList<>();
try {
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String locationTitle = addresses.get(0).getAddressLine(0);
Log.d(TAG, "onMapLongClick: addresses = " + addresses.get(0));
AnimalMarker marker = new AnimalMarker(latLng.latitude, latLng.longitude, locationTitle, firebaseAuth.getCurrentUser().getUid());
markersDatabase.addMarker(marker);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(marker.getLatitude(), marker.getLongitude()), DEFAULT_ZOOM));
}
});
}
...
private void startMarkersListener() {
markersDatabase.readCurrentUserMarkers(new MarkersDatabaseListener() {
@Override
public void onCurrentUserMarkersCallBack(List<AnimalMarker> list) {
Log.d(TAG, "onCurrentUserMarkersCallBack: myMarkersList updated");
clearAllMyMarkers();
for (AnimalMarker animalMarker : list) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(animalMarker.getLatitude(), animalMarker.getLongitude()))
.title(animalMarker.getLocation())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.visible(myMarkersVisible));
Log.d(TAG, "onCurrentUserMarkersCallBack: created a marker at: " + marker.getTitle());
myMarkersList.add(marker);
}
}
@Override
public void onAllMarkersCallBack(List<AnimalMarker> list) {
Log.d(TAG, "onCurrentUserMarkersCallBack ----> onAllMarkersCallBack: should never reach here");
}
});
}
标记信息窗口适配器
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
private final View mWindow;
private Context context;
public MarkerInfoWindowAdapter(Context context) {
this.context = context;
mWindow = LayoutInflater.from(context).inflate(R.layout.marker_info_window, null);
}
private void renderWindowText(Marker marker, View view) {
// example code to come here
String title = marker.getTitle();
TextView titleTextView = (TextView) view.findViewById(R.id.miwLocation);
if(!title.equals("")) {
titleTextView.setText(title);
}
}
@Override
public View getInfoWindow(Marker marker) {
renderWindowText(marker, mWindow);
return mWindow;
}
@Override
public View getInfoContents(Marker marker) {
renderWindowText(marker, mWindow);
return mWindow;
}
}
在自定义InfoWindow中布置我想要的信息:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/white_border"
android:orientation="horizontal"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/miwLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginStart="150dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:maxLines="2"
android:text="miwLocation"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:src="@drawable/dog_icon"
android:id="@+id/miwImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/miwLocation"
android:layout_alignParentStart="true"
android:layout_marginStart="18dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/miwAnimalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/miwLocation"
android:layout_marginStart="30dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/miwImage"
android:layout_toRightOf="@id/miwImage"
android:ellipsize="end"
android:maxLines="2"
android:text="miwAnimalName"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/miwAnimalAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/miwAdultCB"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/miwImage"
android:text="3 yrs"
android:textColor="#000000"
android:textSize="20sp" />
<CheckBox
android:id="@+id/miwAdultCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/miwAnimalName"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/miwImage"
android:text=" Adult"
android:textSize="20dp" />
<CheckBox
android:id="@+id/miwNeuteredCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/miwAnimalName"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/miwAdultCB"
android:text=" Neutered"
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
它的形象:
在理解了这篇文章中的答案后,我找到了解决方案
custom info window adapter with custom data in map v2
最简单的方法是创建一个匿名适配器并在其中使用全局HashMap <Marker,AnimalMarker>。
像这样的东西
getMap().setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
// set layout, use Marker/AnimalMarker here
}
});