【发布时间】:2018-10-23 02:08:24
【问题描述】:
我有一个 Google 地图活动,其中包括一个搜索框和一个交货列表视图(包含 LatLng)。当我单击列表视图内的地址时,我希望相机移动到该地址/标记(列表视图内的所有地址都被标记)。
我在我的自定义 ListAdapter 中有这个方法,我尝试在其中获取更新相机,但它不起作用。这是我的部分代码。
public class DeliveriesListAdapter extends ArrayAdapter<Delivery> implements View.OnClickListener {
private ArrayList<Delivery> deliveries;
public DeliveriesListAdapter(ArrayList<Delivery> data, Context context) {
super(context, R.layout.activity_row_item, data);
this.deliveries = data;
this.mContext = context;
}
@Override
public void onClick(View v) {
// TODO: on click, move camera to selected address on map.
int position = (Integer) v.getTag();
Delivery delivery = (Delivery) deliveries.get(position);
CameraUpdateFactory.newLatLng(delivery.getLatLng());
}
}
The List at the bottom of the screen is supposed to move the camera to that marker on the map.
这是保存指定列表项的 LatLng 的 My Data 类。
public class Delivery {
private String address;
private double distance, time;
private LatLng latLng;
private Place place;
public Delivery(Place place) {
address = place.getAddress().toString();
distance = -1;
time = -1;
latLng = place.getLatLng();
this.place = place;
}
在我的 Google 地图活动中:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private ArrayList<Delivery> deliveryList = new ArrayList<>();
// called inside onCreate(...)
private void setUpListView() {
deliveriesListView = findViewById(R.id.deliveries_listview);
adapter = new DeliveriesListAdapter(addressList, this);
deliveriesListView.setAdapter(adapter);
}
/**
* Used to add Delivery to a list view.
* @param delivery The Delivery containing the address to be added to the view to add the marker.
*/
private void addDeliveryToList(Delivery delivery) {
deliveryList.add(delivery);
adapter.notifyDataSetChanged();
}
edit:我知道CameraUpdateFactory.newLatLng(delivery.getLatLng());是错的,我之前弄错了,删除了我原来的。
解决方案:我设法通过将我的自定义列表适配器重写为 RecyclerViewAdapter 来解决它,并设法将 Overriden onClick 方法移动到我的 Maps Activity,这是我遇到的麻烦。
【问题讨论】:
标签: java android google-maps