【问题标题】:How do you add a marker on google map after button click on dialogFragment?按钮单击dialogFragment后如何在谷歌地图上添加标记?
【发布时间】:2020-07-19 05:01:42
【问题描述】:

我正在尝试创建一个应用程序,允许用户在地图上发布标记,该标记是他们捕获的图片以及他们设置的标题。但是,在用户单击 dialogFragment 上的帖子后,在我的 android 设备上运行它不会显示标记,它只会在 sydney 上显示一个标记。我将不胜感激任何帮助理解为什么标记没有出现,以及我可以做些什么来修复这个错误。谢谢!

这是我为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 的基本实现,标记设置在 sydney

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 google-maps marker


    【解决方案1】:

    您可以从 dilaog 添加以下代码 onClick ,它将在谷歌地图上添加标记。在此之前只需检查 mapReady() 是否成功调用。

       mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
    
        @Override
        public void onMapClick(LatLng destination) {
    
            MarkerOptions options = new MarkerOptions();
            options.position(destination);
            options.title("Lat=" + destination.latitude + ", Long=" + destination.longitude);
            Marker marker = mGoogleMap.addMarker(options);
            mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
        }
    });
    

    【讨论】:

    • mGoogleMap 是以前定义的 mMap 吗?我添加了这些更改,但不幸的是,单击发布后标记仍然没有出现在地图上。如果对话不包含地图,我也很困惑为什么 onMapClick 会出现在对话中。
    猜你喜欢
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2016-03-16
    相关资源
    最近更新 更多