Android 如何在单击时设置新地图位置
我的代码怎么了。当我按下按钮时,为什么地图显示为默认位置 这是我的setOnClickListenerAndroid 如何在单击时设置新地图位置,android,android-recyclerview,onclicklistener,google-maps-android-api-2,Android,Android Recyclerview,Onclicklistener,Google Maps Android Api 2,我的代码怎么了。当我按下按钮时,为什么地图显示为默认位置 这是我的setOnClickListener holder.mapBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v ) { //if you need position, just use recycleViewHolder.getAdapterPo
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));
}
}在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;
现在,在MapsActivity的onCreate方法中执行以下操作:
请注意,-34和151是默认值。您可以选择您喜欢的任何坐标
现在,您可以使用onMapReady方法中的新坐标设置此值:
mMap.moveCameraCameraUpdateFactory.newLatLngmLatLng 你需要在帖子中提供更多信息。如果不向我们显示您的MapsActivity代码,我们不可能知道为什么您的MapsActivity没有显示所选位置。问题是它不会转到新位置。我将编辑此帖子@Barns地图从不显示新坐标,因为您从未更改它们您仍在使用Sydney的LatLng值您能告诉我当我单击按钮时如何更改这些值吗?@Barns
Intent intent = getIntent();
double lat = intent.getDoubleExtra("lat", -34);
double lng = intent.getDoubleExtra("lng", 151);
mLatLng = new LatLng(lat, lng);