【问题标题】:on ListView item click, move Google Map camera to the specified marker在 ListView 项目上单击,将 Google Map 相机移动到指定标记
【发布时间】: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


    【解决方案1】:

    您还需要告诉地图移动相机。喜欢这个mMap.moveCamera(CameraUpdateFactory.newLatLng(delivery.getLatLng()))。还有一件事,你不应该处理适配器中的点击。它们应该包含在主机 UI 部分中。您可以使用接口在主机 UI 部分进行回调。您可以在here 获得有关如何执行此操作的提示。

    【讨论】:

    • 如果它解决了您的问题,请保持接受答案。
    【解决方案2】:

    在您的 onCLick 函数中,将 CameraUpdateFactory.newLatLng(delivery.getLatLng()); 替换为 animateCamera(CameraUpdateFactory.newLatLng(delivery.getLatLng())

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多