我有一个名为AnimalMarker的类,该类应代表普通Google地图Marker(纬度,经度,位置)和对象Animal的最低要求。我正在使用Firebase实时数据库来存储那些AnimalMarker。
我想做的,但不知道如何做,就是创建一个自定义的InfoWindow(我已经创建了新的布局和一个应该在MapActivity中传递的Adapter),即使该窗口绑定了google maps Markers,应该从该Marker的相应AnimalMarker中获取数据。
我的MapActivity的逻辑是:
我现在意识到,与其将谷歌地图标记仅保存在列表中,还不如使用Hashmap
这是我的代码:
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");
}
});
}
MarkerInfoWindowAdapter
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;
}
}
在自定义信息窗口中布局所需的信息:
<?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>
图片:
答案 0 :(得分:0)
在了解了本文的答案之后,我找到了解决方法
custom info window adapter with custom data in map v2
最简单的方法是创建一个匿名适配器,并在其中使用全局HashMap
类似这样的东西
getMap().setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
// set layout, use Marker/AnimalMarker here
}
});