预期结果:在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>
答案 0 :(得分:5)
首先,您需要将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);
}
});