2017-04-19 40 views
2

預期結果:在autocomplete小部件中查詢返回嵌入式Google地圖片段中用標記指示的位置。如何在Android中將自動填充搜索與谷歌地圖集成?

到目前爲止的代碼:

活動類

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

回答

4

首先,您需要保存在實例的GoogleMap的參考變量,並將其分配在中回調:

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

我很欣賞你的答案:) – k4s

相關問題