如何从Google Places片段存储用户选择的位置

时间:2019-04-14 20:44:05

标签: android firebase google-places-api

我设置了一个活动,用户可以在其中搜索Google地方搜索功能并选择一个位置。我无法弄清楚如何检索用户选择的位置。我需要将此位置存储在 Firebase 中,以便可以使用在Google Maps活动中输入的数据。任何帮助表示赞赏。

Places.initialize(getApplicationContext(), apiKey);
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), apiKey);
    }

    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }

       /* public void getSelectedLocation(Place place){
            String newLocation = place.getName();

        }*/
    });

    addSong.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: Attempting to add object to database.");
            String newArtist = artistName.getText().toString();
            String newSongLink = songLink.getText().toString();
            String newGenre = genreSpinner.getSelectedItem().toString();
            if(!newArtist.equals("") && !newSongLink.equals("") && !newGenre.equals("")){
                FirebaseUser user = firebaseAuth.getCurrentUser();
                String userID = user.getUid();
                databaseReference.child(userID).child("Songs").child("Added Songs").child(newArtist).setValue("true");
                databaseReference.child(userID).child("Songs").child("Added Songs").child(newGenre).setValue("true");
                databaseReference.child(userID).child("Songs").child("Added Songs").child(newSongLink).setValue("true");
                //databaseReference.child(userID).child("Artists").child("Artist Profile").child("Members").child(newArtistMembers).setValue("true");
                toastMessage("Adding new profile");
            }else{
                toastMessage("Please ensure all fields are filled");
            }
            //Return to homescreen
            Intent intent = new Intent(AddSong.this, ArtistHomepage.class);
            startActivity(intent);
        }
    });

是否可以从 getPlaceSelected 获取详细信息并将其设置为字符串,以便可以将其存储在实时数据库中?

0 个答案:

没有答案