gpt4 book ai didi

java - 使用 Marker 对象外部的数据为 Marker 创建自定义信息窗口

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:58 27 4
gpt4 key购买 nike

我有一个名为 AnimalMarker 的类,它应该代表普通谷歌地图标记(纬度、经度、位置)和动物对象的最低限度。我正在使用 Firebase 实时数据库来存储这些 AnimalMarkers。

我想做,但不知道该怎么做,是创建一个自定义 InfoWindow(我已经创建了新的布局和一个应该在 MapActivity 中传递的适配器),即使绑定(bind)到谷歌地图标记,也应该从该标记的相应 AnimalMarker 中获取数据。

我的MapActivity的逻辑是:

  • 使用纬度、经度创建一个 AnimalMarker,并接受用户输入以向其添加动物。现在 AnimalMarker 对象已创建
  • 将 AnimalMarker 添加到 Firebase 实时数据库
  • 当 DatabaseListener 获取更新时,它将调用 MapActivity 中的方法来重新创建 google-maps 标记列表并重新绘制它们。

我现在意识到,与其仅将 google-maps 标记保存在列表中,不如使用 Hashmap < AnimalMarker, Marker > 更好,并在数据库监听器注意到更新时更新 Hashmap 但即便如此,我也不知道如何使 InfoWindowAdapter 从标记的关联 AnimalMarker 中获取数据。

这是我的代码:

map Activity

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>

它的图像:

Imgur

最佳答案

在理解这篇文章中的答案后我找到了解决方案

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

}
});

关于java - 使用 Marker 对象外部的数据为 Marker 创建自定义信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55512035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com