2016-11-29 70 views
-1

Im面臨在我的項目中使用Google Map和Place的問題。我已經嘗試了幾天找出最佳解決方案,但我還沒有。如何在Android中結合Google地圖和Place API

我想使用Goolge地圖和地方API。我的目標是當我將餐館位置輸入到EditText搜索自動完成功能中時,Google地圖將識別此位置,並通過標記向周圍顯示其他餐廳。

那麼,我可以使用結合谷歌地圖和地方來實現和使用JSON來管理標記?

我知道谷歌服務數據庫已申請的用戶,但也有太多的標記,我不能創建新的和手動

我有指此鏈接:

https://mapmaker.google.com/mapmaker 

但它不能滿足需求我的要求

謝謝!

+0

沒錯這是可以結合兩者...... 你必須做以下事項... 1)創建另一個從地圖片段調用的片段...實現將API放置在那裏然後捆綁lat long併發回t o地圖片段...! –

+0

我將分享代碼...我已經實現了它...但是您必須根據您的要求與您的項目進行整合。 –

+0

感謝您的建議,我想可能使用兩者都會更好,如果您有任何有關代碼的查詢,我會參考您的代碼 –

回答

0

代碼是有點冗長,但不難imlement

  • 從地圖碎片開始的地方片段.....

    下面是placefragment命名爲AutoCompletePlacesFragment.java(管理你的東西,你找到這條線* //做這裏的東西點擊..... *)

    public class AutoCompleteP lacesFragment延伸片段實現GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {

    私有靜態最後的LatLngBounds BOUND =新的LatLngBounds(新經緯度(-0,0),新的經緯度(0,0));

    private EditText mAutocompleteView;私人RecyclerView mRecyclerView;私人LinearLayoutManager的mLinearLayoutManager; 私人PlacesAutoCompleteAdapter mAutoCompleteAdapter; ImageView刪除; GoogleApiClient mGoogleApiClient;

查看RootView;

int preferredredUserId; String preferredredPassengerGcm; PreferenceData preferenceData;

公共AutoCompletePlacesFragment(){//需要空公共構造}

@覆蓋公共無效的onCreate(捆綁savedInstanceState){}

@覆蓋公共查看onCreateView(LayoutInflater吹氣,ViewGroup中容器,包savedInstanceState) {//膨脹這個片段的佈局buildGoogleApiClient(); RootView = inflater.inflate(R.layout.fragment_auto_complete_places,container,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) { 

} 

}

相關問題