android – Get Data(title)From Info-Window of Marker and Put on Dialog Box-ThrowExceptions

Exception or error:

I have Created MapView in which i plot the marker On Map using Longitude and Latitude calling From Firestore Database.
and Also Retrieve The Job Title Name on Info-window as title but Now i have created Dialog Box and in which i want to show Title name which is in Infowindow of which his marker
so what should i do??
please share your answer

This Is My Java Code

@Override
public void onMapReady(final GoogleMap googleMap) {

    mGoogleMap = googleMap;

    FirebaseFirestore mDatabase = FirebaseFirestore.getInstance();
    CollectionReference mOrderRef = mDatabase.collection("Job Post1");

    mOrderRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                if (documentSnapshot.contains("lat") && documentSnapshot.contains("lon")) {
                    String lat = (String) documentSnapshot.get("lat");
                    String lon = (String) documentSnapshot.get("lon");
                    final String title = (String) documentSnapshot.get("title");

                    if (lat != null && lon != null && !lat.isEmpty() && !lon.isEmpty()) {
                        double latitude = Double.parseDouble(lat.trim());
                        double longitude = Double.parseDouble(lon.trim());
                        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(title));
                        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));

                        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                            @Override
                            public void onInfoWindowClick(Marker marker) {
                                Jobpost jobpost = new Jobpost();
                                TextView txtclose, textView26;
                                myDialog.setContentView(R.layout.custompopup);
                                txtclose = (TextView) myDialog.findViewById(R.id.txtclose);
                                textView26 = (TextView) myDialog.findViewById(R.id.textView26);
                                txtclose.setText("X");
                                txtclose.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        myDialog.dismiss();
                                    }
                                });
                                Intent intent = getActivity().getIntent();

                                myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                                myDialog.show();
                            }
                        });

                    }

                }
            }

        }

    });
}

Dialog Open When Click on Infowindow of Marker

How to solve:

You can use marker to find the title that you set during addMarker. Check below:

googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {
        new AlertDialog.Builder(TestActivity.this)
                .setTitle(marker.getTitle())
                .setMessage(marker.getPosition().latitude + " -> " + marker.getPosition().longitude)
                .setPositiveButton("OK", null)
                .create().show();
    }
});

Output:
enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *