Search code examples
androidfirebasefirebase-realtime-databasegoogle-places-apigmsplacepicker

get stored LatLng to show location on map when clicked


I am using Android PlacePicker to store a location's latitude and longitude in my Firebase Database using the place.getLatLng(); method.

I also stored the location's name using the place.getName(); method.

the Name of the location is visible in the user's post but the Latitude and Longitude are not. My intention is in the case of any user clicking the name of the place in the viewholder, they will be taken to an activity where PlacePicker will show that location in the map.

here's an example of the firebase JSON

{
  "dPicture" : "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/Profile_images%2Fcropped175425077.jpg?alt=media&token=d40abdda-d4e7-450b-b215-a8bb0ee1ce4d",
  "image" : "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/Post_Images%2Fcropped945668710.jpg?alt=media&token=2db5ee7c-9c98-4512-98ea-7ec984e5306b",
  "latLong" : "lat/lng: (-6.190593300000001,106.8061219)",
  "summary" : "yo",
  "place" : "the biggest mall in Asia",
  "uid" : "bFVSFnu6uJcfOEmCGeYpmmRsjQA2",
  "username" : "Rambo"
}

I know how to send the stored Lat and Long as extra to another activity such as

postRef = FirebaseDatabase.getInstance().getReference().child("posts");
viewHolder.mPlaceName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        postRef.child(post_key).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                final String latLng = dataSnapshot.child("latLong").getValue().toString();
                                Intent mapIntent = new Intent(MainActivity.this, MapActivity.class);

                                mapIntent.putExtra("LatLng",latLng);
                                startActivity(mapIntent);

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                    }
                });

but how do i automatically use that Lat and Long value to automatically have the PlacePicker show that location in the map in the MapActivity?

Thanks!


Solution

  • First, changes your schema like this

    {
      "dPicture" : "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/Profile_images%2Fcropped175425077.jpg?alt=media&token=d40abdda-d4e7-450b-b215-a8bb0ee1ce4d",
      "image" : "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/Post_Images%2Fcropped945668710.jpg?alt=media&token=2db5ee7c-9c98-4512-98ea-7ec984e5306b",
      "lat": "-6.190593300000001",
      "lng": "106.8061219",
      "summary" : "yo",
      "place" : "the biggest mall in Asia",
      "uid" : "bFVSFnu6uJcfOEmCGeYpmmRsjQA2",
      "username" : "Rambo"
    }
    

    Treat them as two separate values or if you want them to be treated as one you could get it like this

    {
      "dPicture" : "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/Profile_images%2Fcropped175425077.jpg?alt=media&token=d40abdda-d4e7-450b-b215-a8bb0ee1ce4d",
      "image" : "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/Post_Images%2Fcropped945668710.jpg?alt=media&token=2db5ee7c-9c98-4512-98ea-7ec984e5306b",
      "coords": {
          "lat": "-6.190593300000001",
          "lng": "106.8061219",
      }
      "summary" : "yo",
      "place" : "the biggest mall in Asia",
      "uid" : "bFVSFnu6uJcfOEmCGeYpmmRsjQA2",
      "username" : "Rambo"
    }
    

    To query them:

    For the first version of the schema:

    postRef = FirebaseDatabase.getInstance().getReference().child("posts");
    viewHolder.mPlaceName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            postRef.child(post_key).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                   final String lat = dataSnapshot.child("lat").getValue().toString();
                   final String lng = dataSnapshot.child("lng").getValue().toString();
                   Intent mapIntent = new Intent(MainActivity.this, MapActivity.class);
                   mapIntent.putExtra("lat",lat);
                   mapIntent.putExtra("lng",lng);
                   startActivity(mapIntent);
    
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
    
        }
    });
    

    For the second version of the schema:

    postRef = FirebaseDatabase.getInstance().getReference().child("posts");
    viewHolder.mPlaceName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            postRef.child(post_key).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                   final String lat = dataSnapshot.child("coords").child("lat").getValue().toString();
                   final String lng = dataSnapshot.child("coords").child("lng").getValue().toString();
                   Intent mapIntent = new Intent(MainActivity.this, MapActivity.class);
                   mapIntent.putExtra("lat",lat);
                   mapIntent.putExtra("lng",lng);
                   startActivity(mapIntent);
    
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
    
        }
    });
    

    You could implement what would work for you but I suggest the 2nd one for organizational preferences.

    To show them in the next activity, assuming you have your Google Maps setup in your app, use this code:

    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions()
            //get this LatLng values from the Intent
            .position(new LatLng(<yourLat>, <yourLng>))
            .title("Hello world"));
    }
    

    Try to read on this as well: https://developers.google.com/maps/documentation/android-api/marker