【问题标题】:How to integrate autocomplete search with google maps in Android?如何在 Android 中将自动完成搜索与谷歌地图集成?
【发布时间】:2017-04-19 22:38:04
【问题描述】:

预期结果:在 autocomplete 小部件中查询会返回嵌入的谷歌地图片段中用标记指示的位置。

到目前为止的代码:

活动类:

// 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 布局

<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>

【问题讨论】:

    标签: java android google-maps google-places-api


    【解决方案1】:

    首先,您需要将 GoogleMap 引用保存在一个实例变量中,并在 onMapReady() 回调中分配它:

    GoogleMap mMap;
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
    
      mMap = googleMap;
    
      //...........
    
    }
    

    然后,您只需要获取名称和位置,并放置标记:

    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);
        }
    });
    

    【讨论】:

    • 感谢您的回答 :)
    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 2018-07-04
    • 2012-11-21
    • 2020-12-12
    • 2017-01-25
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多