我正在尝试创建一个允许用户在地图上发布标记的应用程序,该标记是他们捕获的图片以及他们设置的标题。但是,在用户单击dialogFragment上的帖子后,在我的android设备上运行该标记不会显示标记,它只是在悉尼显示一个标记。感谢您在理解为什么标记不出现以及如何解决此错误方面的帮助。谢谢!
这是我为DialogFragment创建的onView以及用于图像捕获的activityforresult,这里的mMap是MapActivity中定义的GoogleMap。>
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { Bitmap bmp = (Bitmap) data.getExtras().get("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); // convert byte array to Bitmap image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); postImg.setImageBitmap(image); } } } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button post = view.findViewById(R.id.btnDone); final EditText popUpName = view.findViewById(R.id.popUpName); String title = getArguments().getString("title", "Post Popup"); final ChipGroup tags = view.findViewById(R.id.chipGroup); getDialog().setTitle(title); postImg = view.findViewById(R.id.imageView2); postImg.setClickable(true); popUpName.requestFocus(); //popUpName.setOnEditorActionListener(this); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); postImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); } }); post.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String popUpTitle = popUpName.getText().toString(); tags.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(ChipGroup group, int checkedId) { Chip chip = group.findViewById(checkedId); if (chip != null) { String chipTagSelected = chip.getText().toString(); } double maxX = 39.333977; double minX = 39.326170; double minY = -76.624140; double maxY = -76.618813; DecimalFormat df = new DecimalFormat(".######"); double diffX = maxX - minX; double randomValueX = minX + Math.random() * diffX; double diffY = maxY - minY; double randomValueY = minY + Math.random() * diffY; Marker marker = MapsView.mMap.addMarker(new MarkerOptions() .position(new LatLng(randomValueX, randomValueY)) .title(popUpTitle) .icon(BitmapDescriptorFactory.fromBitmap(image))); marker.setTag(0); } }); // //popUpName.getText().toString() dismiss(); } }); }
这是我的DialogFragment的xml文件
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="124dp"
android:text="Add PopUp"
app:layout_constraintBottom_toTopOf="@+id/popUpName"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/popUpName"
android:layout_width="296dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="Popup Name"
android:inputType="textCapWords"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
<Button
android:id="@+id/btnDone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="16dp"
android:text="Post"
android:backgroundTint="@color/lightBlue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chipGroup" />
<TextView
android:id="@+id/textView"
android:layout_width="127dp"
android:layout_height="32dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="112dp"
android:text="Tags"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/popUpName" />
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="290dp"
android:layout_height="120dp"
android:layout_marginStart="16dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="16dp"
app:chipSpacing="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:singleSelection="true"
>
<com.google.android.material.chip.Chip
android:id="@+id/Dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dog"
app:chipEndPadding="12dp"
app:chipStartPadding="12dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"/>
<com.google.android.material.chip.Chip
android:id="@+id/Squirrels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Squirrels"
app:chipEndPadding="12dp"
app:chipStartPadding="12dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"/>
<com.google.android.material.chip.Chip
android:id="@+id/Bird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bird"
app:chipEndPadding="12dp"
app:chipStartPadding="12dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"/>
<com.google.android.material.chip.Chip
android:id="@+id/People"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="People"
app:chipEndPadding="8dp"
app:chipStartPadding="8dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"/>
<com.google.android.material.chip.Chip
android:id="@+id/Random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random"
app:chipEndPadding="8dp"
app:chipStartPadding="8dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"/>
<com.google.android.material.chip.Chip
android:id="@+id/Animals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animals"
app:chipEndPadding="8dp"
app:chipStartPadding="8dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"/>
</com.google.android.material.chip.ChipGroup>
<ImageView
android:id="@+id/imageView2"
android:layout_width="240dp"
android:layout_height="134dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimary"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toTopOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
这是我的MapActivity的onCreate,onMapReady只是在悉尼设置标记的MapACtivity的基本实现
public class MapsView extends FragmentActivity implements OnMapReadyCallback { public static GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps_view); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); FloatingActionButton fab = findViewById(R.id.add_post); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG) // .setAction("Action", null).show(); showPostDialog(); } }); } private void showPostDialog() { FragmentManager fm = getSupportFragmentManager(); PostActivity postFragmentDialog = PostActivity.newInstance("Some title"); postFragmentDialog.show(fm, "fragment_edit"); //inal EditText popUpName = findViewById(R.id.popUpName); } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @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)); }
}
我正在尝试创建一个允许用户在地图上发布标记的应用程序,该标记是他们捕获的图片以及他们设置的标题。但是,在我的android设备上运行它不会显示...
您可以在dilaog中添加以下代码onClick,它将在Google地图上添加标记。在此之前,只需检查是否成功调用了MapReady()。