如何合并 Google 地图并在 Android 中放置 API
How to combine Google map and Place API in Android
我在项目中使用 Google Map and Place 时遇到问题。我已经尝试了几天来找出最佳解决方案,但我还没有。
我想使用 Goolge 地图和 Place API。我的目标是当我在 EditText 搜索自动完成中输入餐厅位置时,google 地图将识别这个位置,并通过标记显示附近的其他餐厅。
那么,我可以使用google地图和地方结合来实现并使用json来管理标记吗?
我知道Google服务数据库已经申请了用户,但是标记太多,我无法创建新的和手动的
我已经参考了这个 link:
https://mapmaker.google.com/mapmaker
但是不符合我的要求
谢谢!
代码有点长但不难实现
从地图片段开始放置片段.....
Fragment fragmentObject = new AutoCompletePlacesFragment();
Bundle args = new Bundle(2);
args.putInt("userID", preferedUserId);
args.putString("passengerGcm", preferedPassengerGcm);
fragmentObject.setArguments(args);
android.support.v4.app.FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.replace(R.id.container, fragmentObject)
.commit();
这里是名为 AutoCompletePlacesFragment.java 的 placefragment(在找到此行的地方管理你的东西 * //点击此处执行操作.....*)
public class AutoCompletePlacesFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
private static final LatLngBounds BOUND = new LatLngBounds( new LatLng(-0, 0), new LatLng(0, 0));
私有EditText mAutocompleteView;私有 RecyclerView mRecyclerView;私有 LinearLayoutManager mLinearLayoutManager;
私有 PlacesAutoCompleteAdapter mAutoCompleteAdapter;图像视图删除; GoogleApiClient mGoogleApiClient;
查看RootView;
int preferedUserId;字符串 preferredPassengerGcm; PreferenceData preferenceData;
public AutoCompletePlacesFragment() { // 需要空 public 构造函数 }
@Override public void onCreate(Bundle savedInstanceState) { }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 膨胀这个片段的布局 buildGoogleApiClient(); RootView =inflater.inflate(R.layout.fragment_auto_complete_places, 容器, false);
mAutocompleteView = (EditText) RootView.findViewById(R.id.autocomplete_places);
mAutocompleteView.setOnClickListener(this);
delete=(ImageView) RootView.findViewById(R.id.cross);
mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(RootView.getContext(), R.layout.searchview_adapter,
mGoogleApiClient, BOUND, null);
mRecyclerView=(RecyclerView) RootView.findViewById(R.id.recyclerView);
mLinearLayoutManager=new LinearLayoutManager(RootView.getContext());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mAutoCompleteAdapter);
delete.setOnClickListener(this);
mAutocompleteView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
mAutoCompleteAdapter.getFilter().filter(s.toString());
} else if (!mGoogleApiClient.isConnected()) {
Toast.makeText(RootView.getContext(), getResources().getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
// Log.e(Constants.PlacesTag,getResources().getString(R.string.internet_error));
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
mRecyclerView.addOnItemTouchListener(
new AutoCompleteItemClickListener(this, new AutoCompleteItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position);
final String placeId = String.valueOf(item.placeId);
Log.i("TAG", "Autocomplete item selected: " + item.description);
/*
Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
*/
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
// Request did not complete successfully
places.release();
return;
}
if (places.getCount() == 1) {
//Do the things here on Click.....
Toast.makeText(RootView.getContext(), String.valueOf(places.get(0).getLatLng()), Toast.LENGTH_SHORT).show();
Fragment fragmentObject = new PassengerFragmentMap();
Bundle args = new Bundle(5);
args.putString("destination",item.description.toString());
args.putDouble("destLat", places.get(0).getLatLng().latitude);
args.putDouble("destLong",places.get(0).getLatLng().longitude);
fragmentObject.setArguments(args);
preferenceData.setCurrentCabRequestDestinationPreferences(item.description.toString());
android.support.v4.app.FragmentManager fm = getFragmentManager();
if(fm!=null) {
fm.beginTransaction()
.replace(R.id.container, fragmentObject)
.commit();
}
} else {
Toast.makeText(RootView.getContext(), getResources().getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
}
}
});
Log.i("TAG", "Clicked: " + item.description);
Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId);
}
})
);
return RootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Log.v("Google API Callback", "Connection Done");
}
@Override
public void onConnectionSuspended(int i) {
Log.v("Google API Callback", "Connection Suspended");
Log.v("Code", String.valueOf(i));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v("Google API Callback","Connection Failed");
Log.v("Error Code", String.valueOf(connectionResult.getErrorCode()));
Toast.makeText(getActivity(), getResources().getString(R.string.internet_error),Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
if(v==delete){
mAutocompleteView.setText("");
}
}
@Override
public void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()){
Log.v("Google API","Connecting");
mGoogleApiClient.connect();
}
}
public void onLocationSelected(){
}
}
这里是PlacesAutoCompleteAdapter.java(复制粘贴即可,无需更改)
public class PlacesAutoCompleteAdapter
extends RecyclerView.Adapter<PlacesAutoCompleteAdapter.PredictionHolder> implements Filterable {
private static final String TAG = "PlacesAutoCompleteAdapter";
private ArrayList<PlaceAutocomplete> mResultList;
private GoogleApiClient mGoogleApiClient;
private LatLngBounds mBounds;
private AutocompleteFilter mPlaceFilter;
private Context mContext;
private int layout;
public PlacesAutoCompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient,
LatLngBounds bounds, AutocompleteFilter filter) {
mContext = context;
layout = resource;
mGoogleApiClient = googleApiClient;
mBounds = bounds;
mPlaceFilter = filter;
}
/**
* Sets the bounds for all subsequent queries.
*/
public void setBounds(LatLngBounds bounds) {
mBounds = bounds;
}
/**
* Returns the filter for the current set of autocomplete results.
*/
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// Skip the autocomplete query if no constraints are given.
if (constraint != null) {
// Query the autocomplete API for the (constraint) search string.
mResultList = getAutocomplete(constraint);
if (mResultList != null) {
// The API successfully returned results.
results.values = mResultList;
results.count = mResultList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
//notifyDataSetInvalidated();
}
}
};
return filter;
}
private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) {
if (mGoogleApiClient.isConnected()) {
Log.i("", "Starting autocomplete query for: " + constraint);
// Submit the query to the autocomplete API and retrieve a PendingResult that will
// contain the results when the query completes.
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
// This method should have been called off the main UI thread. Block and wait for at most 60s
// for a result from the API.
AutocompletePredictionBuffer autocompletePredictions = results
.await(60, TimeUnit.SECONDS);
// Confirm that the query completed successfully, otherwise return null
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
Toast.makeText(mContext, "Error contacting API: " + status.toString(),
Toast.LENGTH_SHORT).show();
Log.e("", "Error getting autocomplete prediction API call: " + status.toString());
autocompletePredictions.release();
return null;
}
Log.i("", "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
// Copy the results into our own data structure, because we can't hold onto the buffer.
// AutocompletePrediction objects encapsulate the API response (place ID and description).
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
// Get the details of this prediction and copy it into a new PlaceAutocomplete object.
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
prediction.getDescription()));
}
// Release the buffer now that all data has been copied.
autocompletePredictions.release();
return resultList;
}
Log.e("", "Google API client is not connected for autocomplete query.");
return null;
}
@Override
public PredictionHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = layoutInflater.inflate(layout, viewGroup, false);
PredictionHolder mPredictionHolder = new PredictionHolder(convertView);
return mPredictionHolder;
}
@Override
public void onBindViewHolder(PredictionHolder mPredictionHolder, final int i) {
mPredictionHolder.mPrediction.setText(mResultList.get(i).description);
/*mPredictionHolder.mRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGetLatLonCallback.getLocation(resultList.get(i).toString());
}
});*/
}
@Override
public int getItemCount() {
if(mResultList != null)
return mResultList.size();
else
return 0;
}
public PlaceAutocomplete getItem(int position) {
return mResultList.get(position);
}
public class PredictionHolder extends RecyclerView.ViewHolder {
private TextView mPrediction;
private RelativeLayout mRow;
public PredictionHolder(View itemView) {
super(itemView);
mPrediction = (TextView) itemView.findViewById(R.id.address);
mRow=(RelativeLayout)itemView.findViewById(R.id.predictedRow);
}
}
/**
* Holder for Places Geo Data Autocomplete API results.
*/
public class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence description;
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
this.placeId = placeId;
this.description = description;
}
@Override
public String toString() {
return description.toString();
}
}
}
这里是AutoCompleteItemClickListener.java(复制粘贴即可,无需更改)
public class AutoCompleteItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public AutoCompleteItemClickListener(AutoCompletePlacesFragment context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context.getActivity(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildLayoutPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
我在项目中使用 Google Map and Place 时遇到问题。我已经尝试了几天来找出最佳解决方案,但我还没有。
我想使用 Goolge 地图和 Place API。我的目标是当我在 EditText 搜索自动完成中输入餐厅位置时,google 地图将识别这个位置,并通过标记显示附近的其他餐厅。
那么,我可以使用google地图和地方结合来实现并使用json来管理标记吗?
我知道Google服务数据库已经申请了用户,但是标记太多,我无法创建新的和手动的
我已经参考了这个 link:
https://mapmaker.google.com/mapmaker
但是不符合我的要求
谢谢!
代码有点长但不难实现
从地图片段开始放置片段.....
Fragment fragmentObject = new AutoCompletePlacesFragment(); Bundle args = new Bundle(2); args.putInt("userID", preferedUserId); args.putString("passengerGcm", preferedPassengerGcm); fragmentObject.setArguments(args); android.support.v4.app.FragmentManager fm = getFragmentManager(); fm.beginTransaction() .replace(R.id.container, fragmentObject) .commit();
这里是名为 AutoCompletePlacesFragment.java 的 placefragment(在找到此行的地方管理你的东西 * //点击此处执行操作.....*)
public class AutoCompletePlacesFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
private static final LatLngBounds BOUND = new LatLngBounds( new LatLng(-0, 0), new LatLng(0, 0));
私有EditText mAutocompleteView;私有 RecyclerView mRecyclerView;私有 LinearLayoutManager mLinearLayoutManager; 私有 PlacesAutoCompleteAdapter mAutoCompleteAdapter;图像视图删除; GoogleApiClient mGoogleApiClient;
查看RootView;
int preferedUserId;字符串 preferredPassengerGcm; PreferenceData preferenceData;
public AutoCompletePlacesFragment() { // 需要空 public 构造函数 }
@Override public void onCreate(Bundle savedInstanceState) { }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 膨胀这个片段的布局 buildGoogleApiClient(); RootView =inflater.inflate(R.layout.fragment_auto_complete_places, 容器, false);
mAutocompleteView = (EditText) RootView.findViewById(R.id.autocomplete_places);
mAutocompleteView.setOnClickListener(this);
delete=(ImageView) RootView.findViewById(R.id.cross);
mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(RootView.getContext(), R.layout.searchview_adapter,
mGoogleApiClient, BOUND, null);
mRecyclerView=(RecyclerView) RootView.findViewById(R.id.recyclerView);
mLinearLayoutManager=new LinearLayoutManager(RootView.getContext());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mAutoCompleteAdapter);
delete.setOnClickListener(this);
mAutocompleteView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
mAutoCompleteAdapter.getFilter().filter(s.toString());
} else if (!mGoogleApiClient.isConnected()) {
Toast.makeText(RootView.getContext(), getResources().getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
// Log.e(Constants.PlacesTag,getResources().getString(R.string.internet_error));
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
mRecyclerView.addOnItemTouchListener(
new AutoCompleteItemClickListener(this, new AutoCompleteItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position);
final String placeId = String.valueOf(item.placeId);
Log.i("TAG", "Autocomplete item selected: " + item.description);
/*
Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
*/
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
// Request did not complete successfully
places.release();
return;
}
if (places.getCount() == 1) {
//Do the things here on Click.....
Toast.makeText(RootView.getContext(), String.valueOf(places.get(0).getLatLng()), Toast.LENGTH_SHORT).show();
Fragment fragmentObject = new PassengerFragmentMap();
Bundle args = new Bundle(5);
args.putString("destination",item.description.toString());
args.putDouble("destLat", places.get(0).getLatLng().latitude);
args.putDouble("destLong",places.get(0).getLatLng().longitude);
fragmentObject.setArguments(args);
preferenceData.setCurrentCabRequestDestinationPreferences(item.description.toString());
android.support.v4.app.FragmentManager fm = getFragmentManager();
if(fm!=null) {
fm.beginTransaction()
.replace(R.id.container, fragmentObject)
.commit();
}
} else {
Toast.makeText(RootView.getContext(), getResources().getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
}
}
});
Log.i("TAG", "Clicked: " + item.description);
Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId);
}
})
);
return RootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Log.v("Google API Callback", "Connection Done");
}
@Override
public void onConnectionSuspended(int i) {
Log.v("Google API Callback", "Connection Suspended");
Log.v("Code", String.valueOf(i));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v("Google API Callback","Connection Failed");
Log.v("Error Code", String.valueOf(connectionResult.getErrorCode()));
Toast.makeText(getActivity(), getResources().getString(R.string.internet_error),Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
if(v==delete){
mAutocompleteView.setText("");
}
}
@Override
public void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()){
Log.v("Google API","Connecting");
mGoogleApiClient.connect();
}
}
public void onLocationSelected(){
}
}
这里是PlacesAutoCompleteAdapter.java(复制粘贴即可,无需更改)
public class PlacesAutoCompleteAdapter
extends RecyclerView.Adapter<PlacesAutoCompleteAdapter.PredictionHolder> implements Filterable {
private static final String TAG = "PlacesAutoCompleteAdapter";
private ArrayList<PlaceAutocomplete> mResultList;
private GoogleApiClient mGoogleApiClient;
private LatLngBounds mBounds;
private AutocompleteFilter mPlaceFilter;
private Context mContext;
private int layout;
public PlacesAutoCompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient,
LatLngBounds bounds, AutocompleteFilter filter) {
mContext = context;
layout = resource;
mGoogleApiClient = googleApiClient;
mBounds = bounds;
mPlaceFilter = filter;
}
/**
* Sets the bounds for all subsequent queries.
*/
public void setBounds(LatLngBounds bounds) {
mBounds = bounds;
}
/**
* Returns the filter for the current set of autocomplete results.
*/
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// Skip the autocomplete query if no constraints are given.
if (constraint != null) {
// Query the autocomplete API for the (constraint) search string.
mResultList = getAutocomplete(constraint);
if (mResultList != null) {
// The API successfully returned results.
results.values = mResultList;
results.count = mResultList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
//notifyDataSetInvalidated();
}
}
};
return filter;
}
private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) {
if (mGoogleApiClient.isConnected()) {
Log.i("", "Starting autocomplete query for: " + constraint);
// Submit the query to the autocomplete API and retrieve a PendingResult that will
// contain the results when the query completes.
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
// This method should have been called off the main UI thread. Block and wait for at most 60s
// for a result from the API.
AutocompletePredictionBuffer autocompletePredictions = results
.await(60, TimeUnit.SECONDS);
// Confirm that the query completed successfully, otherwise return null
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
Toast.makeText(mContext, "Error contacting API: " + status.toString(),
Toast.LENGTH_SHORT).show();
Log.e("", "Error getting autocomplete prediction API call: " + status.toString());
autocompletePredictions.release();
return null;
}
Log.i("", "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
// Copy the results into our own data structure, because we can't hold onto the buffer.
// AutocompletePrediction objects encapsulate the API response (place ID and description).
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
// Get the details of this prediction and copy it into a new PlaceAutocomplete object.
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
prediction.getDescription()));
}
// Release the buffer now that all data has been copied.
autocompletePredictions.release();
return resultList;
}
Log.e("", "Google API client is not connected for autocomplete query.");
return null;
}
@Override
public PredictionHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = layoutInflater.inflate(layout, viewGroup, false);
PredictionHolder mPredictionHolder = new PredictionHolder(convertView);
return mPredictionHolder;
}
@Override
public void onBindViewHolder(PredictionHolder mPredictionHolder, final int i) {
mPredictionHolder.mPrediction.setText(mResultList.get(i).description);
/*mPredictionHolder.mRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGetLatLonCallback.getLocation(resultList.get(i).toString());
}
});*/
}
@Override
public int getItemCount() {
if(mResultList != null)
return mResultList.size();
else
return 0;
}
public PlaceAutocomplete getItem(int position) {
return mResultList.get(position);
}
public class PredictionHolder extends RecyclerView.ViewHolder {
private TextView mPrediction;
private RelativeLayout mRow;
public PredictionHolder(View itemView) {
super(itemView);
mPrediction = (TextView) itemView.findViewById(R.id.address);
mRow=(RelativeLayout)itemView.findViewById(R.id.predictedRow);
}
}
/**
* Holder for Places Geo Data Autocomplete API results.
*/
public class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence description;
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
this.placeId = placeId;
this.description = description;
}
@Override
public String toString() {
return description.toString();
}
}
}
这里是AutoCompleteItemClickListener.java(复制粘贴即可,无需更改)
public class AutoCompleteItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public AutoCompleteItemClickListener(AutoCompletePlacesFragment context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context.getActivity(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildLayoutPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}