Retrieve location from Firebase and put marker on google map api for android application

北城余情 提交于 2020-01-03 05:31:08

问题


I'm creating a online mess application where there are two logins one for mess owner and another for user so when mess owner put location in his data there should be marker on map in user side.

here is structure of database in imageDatabase structure

please suggest me code to access those latitude n longitude values and create marker on map for each mess registered with location values.


回答1:


EDITED Am proposing a small adjustment to the database structure...under 'MessProviders' create another node called 'locations' with the userID,mProviderLatitude,mProviderLongitude, mProviderBrandName. . It would be good if you had a separate class for getters and setters. My class for that is 'CordinatesModel'

private void messLocation() {
DatabaseReference currentDBcordinates = FirebaseDatabase.getInstance().getReference().child("Users").child("MessProviders").child("locations");
    currentDBcordinates.addValueEventListener(new ValueEventListener() {
    @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //Create an array of markers
            int size = (int) dataSnapshot.getChildrenCount(); //
            Marker[] allMarkers = new Marker[size];
            mMap.clear();   //Assuming you're using mMap 
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
               //Specify your model class here
                CordinatesModel cordinatesModel = new CordinatesModel();
                //lets create a loop
                 for(int i=0;i<=size;i++) {
                    try {
                        //assuming you've set your getters and setters in the Model class
                          cordinatesModel.setmProviderLatitude(s.getValue(CordinatesModel.class).getmProviderLatitude());
                        cordinatesModel.setmProviderLongitude(s.getValue(CordinatesModel.class).getmProviderLongitude());
                        cordinatesModel.setmProviderBrandName(s.getValue(CordinatesModel.class).getmProviderBrandName());
                        //lets retrieve the coordinates and other information
                         Double latitude1 = cordinatesModel.getmProviderLatitude();
                Double longitude1 = cordinatesModel.getmProviderLongitude();
                String brandName=cordinatesModel.getmProviderBrandName();
                        //convert the coordinates to LatLng
                        LatLng latLng = new LatLng(latitude1, longitude1);
                        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        //Now lets add updated markers

                     //lets add updated marker
                        allMarkers[i] = mMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)).position(latLng).title(brandName));enter code here
                 }catch (Exception ex){}
                 }


           }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
 }



回答2:


You can have a root and select it. Then Retrieve all your lat/lng in your snapshot within that root. Here is a sample code:

DatabaseReference locationRef = mRootRef.child("locations");
locationRef.addValueEventListener(new com.firebase.client.ValueEventListener() {
    @Override
    public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {


        for (DataSnapshot snapm: dataSnapshot.getChildren()) {

            Double latitude = snapm.child("0").getValue(Double.class);
            Double longitude = snapm.child("1").getValue(Double.class);
            }
        }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        throw firebaseError.toException();

    }
});



回答3:


 mDb = FirebaseDatabase.getInstance().getReference().child("Users").child("MessProviders").child("zZvkbgUbFohCwtYRfORUM0DBSZR2").child("ProfileInformation");
        mDb.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    String name = dataSnapshot.child("mProviderBrandName").getValue(String.class);
                    double latt = dataSnapshot.child("mProviderLatitude").getValue(Double.class);
                    double lngg = dataSnapshot.child("mProviderLongitude").getValue(Double.class);
                    mMap.addMarker(new MarkerOptions().position(new LatLng(latt, lngg)).title(name));

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

this is the code that worked to put marker on map but problem is I'm providing user id manually so i will create only one marker but i want to create marker for every user how can system will take user id and create marker for each user id ??



来源:https://stackoverflow.com/questions/49324085/retrieve-location-from-firebase-and-put-marker-on-google-map-api-for-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!