1

Is there any possibility that in autocomplete fragment will show my location when I click on the map?

What I mean is, when somebody wants to search for a place via the map rather than by location address, i.e. by looking on the map for the place and pressing/touching/clicking on a street, then that place will be marked and I want to get the street name sent to my fragment.

This is my current code - I hope that you understand my problem:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);



    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .build();
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    SharedPreferences locationsp = PreferenceManager.getDefaultSharedPreferences(this);
    final String sp = locationsp.getString("Location2","No location found");
    //search.setText(sp);
    if(!sp.equals("No location found")){
        autocompleteFragment.setText(sp);
        tmp=sp;
    }


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


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

    try {
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                        .setFilter(typeFilter)
                        .build(this);
    } catch (GooglePlayServicesRepairableException e) {
        // TODO: Handle the error.
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO: Handle the error.
    }




    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.i(TAG, "Place: " + place.getName());
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            // TODO: Handle the error.
            Log.i(TAG, status.getStatusMessage());

        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    setUpMap();

    locationt = findViewById(R.id.mapViewfrag);
    savelocation = findViewById(R.id.savelocation);
    //search = findViewById(R.id.GameLocation);

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("You are here");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

            PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

            map.clear();
            Marker marker = map.addMarker(markerOptions);
            marker.showInfoWindow();
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(marker.getPosition().latitude,marker.getPosition().longitude), 15));
            latitude = marker.getPosition().latitude;
            longtitude = marker.getPosition().longitude;
            try {
                Geocoder geo = new Geocoder(MapActivity.this.getApplicationContext(), Locale.getDefault());
                addresses = geo.getFromLocation(latitude, longtitude, 1);
                System.out.println(addresses);


                if (addresses.isEmpty()) {
                    locationt.setText("Waiting for Location");
                } else {
                    if (addresses.size() > 0) {                          locationt.setText(addresses.get(0).getAddressLine(0));
                        //autocompleteFragment.getText(); <<<Thats what i try but it makes me error
                       locationname=locationt.getText().toString();
}
jtlz2
  • 7,700
  • 9
  • 64
  • 114
Kertuj
  • 33
  • 5

0 Answers0