简体   繁体   English

如何在Android中将自动完成搜索与谷歌地图集成?

[英]How to integrate autocomplete search with google maps in Android?

Expected outcome: querying in autocomplete widget returns location indicated with a marker in the embedded google maps fragment. 预期结果:在autocomplete窗口小部件中查询返回在嵌入式谷歌地图片段中使用标记指示的位置。

Code so far: 代码到目前为止:

Activity class: 活动类:

// Get the SupportMapFragment and request notification
// when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().
        findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);

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

EditText attributeText = (EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input);
attributeText.setHint("find your space!");
autocompleteFragment.getView().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_search));

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

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

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

@Override
public void onMapReady(GoogleMap googleMap) {

// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
googleMap.setMapStyle(
        MapStyleOptions.loadRawResourceStyle(
                getContext(), R.raw.style_json));

// adjust camera view
LatLng kekistan = new LatLng(54.60651219, -9.931456);
googleMap.addMarker(new MarkerOptions().position(kekistan)
        .title("Kekistan City Centre"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(kekistan));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(kekistan, 16));
}

xml layout xml布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        map:cameraTilt="50"
        map:uiCompass="false"
        map:uiZoomGestures="true"
        map:uiZoomControls="false"
        map:uiRotateGestures="false"
        map:uiScrollGestures="true" />

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/topper2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@android:color/transparent"
        android:orientation="vertical">

        <fragment
            android:id="@+id/place_autocomplete_fragment"
            app:layout_widthPercent="90%"
            android:layout_marginTop="15dp"
            app:layout_marginStartPercent="5%"
            android:layout_height="40dp"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
    </android.support.percent.PercentRelativeLayout>
</RelativeLayout>

First, you'll need to save the GoogleMap reference in an instance variable, and assign it in the onMapReady() callback: 首先,您需要将GoogleMap引用保存在实例变量中,并在onMapReady()回调中指定它:

GoogleMap mMap;

@Override
public void onMapReady(GoogleMap googleMap) {

  mMap = googleMap;

  //...........

}

Then, you just need to get the name and the location, and place the Marker: 然后,您只需要获取名称和位置,然后放置标记:

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Log.i(TAG, "Place: " + place.getName());
        String name = (String) place.getName();
        LatLng latLng = place.getLatLng();

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title(name);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM