如何设置新的地图位置 onClick

How to set new Map location onClick

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

这是我的 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);
        }
    });

这是我的地图activity:

   @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));
}

}

在你的 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 是默认值。您可以 select 任何您喜欢的坐标。

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

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