【问题标题】:How to set new Map location onClick如何在单击时设置新的地图位置
【发布时间】:2018-02-12 19:00:07
【问题描述】:

我的代码有什么问题? .为什么当我按下按钮时地图会显示默认位置?

这是我的 setOnClickListener

    holder.mapBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v ) {
            //if you need position, just use recycleViewHolder.getAdapterPosition();
            Toast.makeText(mContext, "OMG! The position is still here and is: "+holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            Uri uri = Uri.parse("geo:35.2137,31.7683");
            Intent intent = new Intent(v.getContext(), MapsActivity.class );
            intent.putExtra("location",uri);
            mContext.startActivity(intent);
        }
    });

这是我的地图活动:

   @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

【问题讨论】:

  • 您需要在帖子中提供更多信息。如果您不向我们展示您的 MapsActivity 代码,我们就不可能知道为什么您的 MapsActivity 没有显示所选位置。
  • 关键是它没有转到新位置。我将编辑帖子。 @谷仓
  • 地图永远不会显示新坐标,因为您永远不会更改它们——您仍在使用“sydney”的 LatLng
  • 你能告诉我点击按钮时如何更改值吗?@Barns

标签: android android-recyclerview onclicklistener google-maps-android-api-2


【解决方案1】:

在您的 onClick 方法中,从您的列表中获取纬度和经度(我在这里硬编码了它们,仅作为示例)。

public void onClick(View v ) {
    double lat = 35.2137;
    double lng = 31.7683;
    Intent intent = new Intent(v.getContext(), MapsActivity.class );
    intent.putExtra("lat",lat);
    intent.putExtra("lng",lng);
    mContext.startActivity(intent);
}

在您的MapsActivity 中添加实例变量:

private LatLng mLatLng;

现在在MapsActivityonCreate 方法中执行以下操作:

Intent intent = getIntent();
double lat = intent.getDoubleExtra("lat", -34);
double lng = intent.getDoubleExtra("lng", 151);

mLatLng = new LatLng(lat, lng);

请注意,-34 和 151 是默认值。您可以选择任何您喜欢的坐标。

现在您可以在onMapReady 方法中使用新坐标设置此值:

mMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));

【讨论】:

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