首页 > 解决方案 > 如何更改地图的“地点自动完成”搜索的字体系列?

问题描述

我想将字体更改为我的字体目录中的 nunito_sans.xml。我已经将 Place Autocomplete 搜索包装在一个线性布局中以获得我想要的背景颜色 - 希望这不会引起问题。此外,一旦输入字符,字体更改将需要扩展到搜索框下方显示的列表。

活动代码:

public class ExploreFragment extends Fragment implements OnMapReadyCallback {

    GoogleMap mapView;
    private Location currentLocation;
    private FusedLocationProviderClient fusedLocationProviderClient;
    private static final int REQUEST_CODE = 101;
    private DatabaseReference mosqueDatabase;
    private String apiKey = "MY_API_KEY";
    private PlacesClient placesClient;
    LatLng userLocation;
    FloatingActionButton buttonCurrentLocation;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_explore, null, false);
        mosqueDatabase = FirebaseDatabase.getInstance().getReference("Timetable");

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
        lastLocation();

        buttonCurrentLocation = view.findViewById(R.id.buttonCurrentLocation);
        buttonCurrentLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mapView.animateCamera(CameraUpdateFactory.newLatLng(userLocation));
            }
        });

        if (!Places.isInitialized()) {
            Places.initialize(getContext(), apiKey);
        }
        placesClient = Places.createClient(getContext());

        final AutocompleteSupportFragment autocompleteSupportFragment = (AutocompleteSupportFragment) getChildFragmentManager()
                .findFragmentById(R.id.fragmentAutocomplete);
        autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.LAT_LNG, Place.Field.NAME));
        autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(@NonNull Place place) {
                final LatLng searchLocation = place.getLatLng();
                mapView.animateCamera(CameraUpdateFactory.newLatLng(searchLocation));
            }

            @Override
            public void onError(@NonNull Status status) {
            }
        });

        return view;
    }

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

        userLocation = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
        mapView.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15.0f));

        mosqueDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    String schoolApproved = snapshot.child("Approved").getValue(String.class);
                    if (schoolApproved.equals("Yes")) {
                        String stringSchool = snapshot.child("Name").getValue(String.class);
                        Double doubleLatitude = snapshot.child("Latitude").getValue(Double.class);
                        Double doubleLongitude = snapshot.child("Longitude").getValue(Double.class);
                        LatLng schoolLocation = new LatLng(doubleLatitude, doubleLongitude);
                        mapView.addMarker(new MarkerOptions().position(schoolLocation).title(stringSchool));
                        //mapView.animateCamera(CameraUpdateFactory.newLatLngZoom(schoolLocation, 15.0f));
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    lastLocation();
                }
                break;
        }
    }

    private void lastLocation() {
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]
                    {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        }
        Task<Location> task = fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    currentLocation = location;
                    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                            .findFragmentById(R.id.mapView);
                    mapFragment.getMapAsync(ExploreFragment.this);
                }
            }
        });
    }
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="6dp"
        android:layout_marginRight="6dp"
        android:background="@drawable/map_search_outline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <fragment
            android:id="@+id/fragmentAutocomplete"
            android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/buttonCurrentLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="64dp"
        android:backgroundTint="#D9FFFFFF"
        android:clickable="true"
        android:tint="#4A89F3"
        app:borderWidth="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/gps" />

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidgoogle-maps

解决方案


首先看看这个问题 如何在android中的地方autocompletefragment中更改文本大小?

Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/epimodem.ttf");
((EditText)placeAutocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setTypeface(face);

推荐阅读