将 google 个地图位置 api activity 转换为片段

Converting google map places api activity to fragment

我在将下面的代码转换为片段以便将其放入选项卡时遇到问题。我不确定哪些代码应该放在哪些方法中,例如 onCreateView,以及我应该如何为适配器设置函数。

package ma.mycompany.com.googleautocomplete;

import...

public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks {
private static final String LOG_TAG = "MainActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private AutoCompleteTextView mAutocompleteTextView;
private TextView mNameTextView;
private TextView mAddressTextView;
private TextView mIdTextView;
private TextView mPhoneTextView;
private TextView mWebTextView;
private TextView mAttTextView;
private GoogleApiClient mGoogleApiClient;
private PlaceArrayAdapter mPlaceArrayAdapter;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
        new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(Places.GEO_DATA_API)
            .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
            .addConnectionCallbacks(this)
            .build();
    mAutocompleteTextView = (AutoCompleteTextView) findViewById(R.id
            .autoCompleteTextView);
    mAutocompleteTextView.setThreshold(3);
    mNameTextView = (TextView) findViewById(R.id.name);
    mAddressTextView = (TextView) findViewById(R.id.address);
    mIdTextView = (TextView) findViewById(R.id.place_id);
    mPhoneTextView = (TextView) findViewById(R.id.phone);
    mWebTextView = (TextView) findViewById(R.id.web);
    mAttTextView = (TextView) findViewById(R.id.att);
    mAutocompleteTextView.setOnItemClickListener(mAutocompleteClickListener);
    mPlaceArrayAdapter = new PlaceArrayAdapter(this, android.R.layout.simple_list_item_1,
            BOUNDS_MOUNTAIN_VIEW, null);
    mAutocompleteTextView.setAdapter(mPlaceArrayAdapter);
}

private AdapterView.OnItemClickListener mAutocompleteClickListener
        = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final PlaceArrayAdapter.PlaceAutocomplete item = mPlaceArrayAdapter.getItem(position);
        final String placeId = String.valueOf(item.placeId);
        Log.i(LOG_TAG, "Selected: " + item.description);
        PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                .getPlaceById(mGoogleApiClient, placeId);
        placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
        Log.i(LOG_TAG, "Fetching details for ID: " + item.placeId);
    }
};

private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
        = new ResultCallback<PlaceBuffer>() {
    @Override
    public void onResult(PlaceBuffer places) {
        if (!places.getStatus().isSuccess()) {
            Log.e(LOG_TAG, "Place query did not complete. Error: " +
                    places.getStatus().toString());
            return;
        }
        // Selecting the first object buffer.
        final Place place = places.get(0);
        CharSequence attributions = places.getAttributions();

        mNameTextView.setText(Html.fromHtml(place.getName() + ""));
        mAddressTextView.setText(Html.fromHtml(place.getAddress() + ""));
        mIdTextView.setText(Html.fromHtml(place.getId() + ""));
        mPhoneTextView.setText(Html.fromHtml(place.getPhoneNumber() + ""));
        mWebTextView.setText(place.getWebsiteUri() + "");
        if (attributions != null) {
            mAttTextView.setText(Html.fromHtml(attributions.toString()));
        }
    }
};

@Override
public void onConnected(Bundle bundle) {
    mPlaceArrayAdapter.setGoogleApiClient(mGoogleApiClient);
    Log.i(LOG_TAG, "Google Places API connected.");

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(LOG_TAG, "Google Places API connection failed with error code: "
            + connectionResult.getErrorCode());

    Toast.makeText(this,
            "Google Places API connection failed with error code:" +
                    connectionResult.getErrorCode(),
            Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionSuspended(int i) {
    mPlaceArrayAdapter.setGoogleApiClient(null);
    Log.e(LOG_TAG, "Google Places API connection suspended.");
}
}

我该怎么做?

我正在发布一个 example.This 是我如何在 Fragment 中使用 google 地图,它将在 tab.Please 中作为单独的视图调用一次并进行必要的必要更改根据您的申请。

我的Fragment.java:-

public class MyFragment extends Fragment {

SupportMapFragment mMapView;
private GoogleMap googleMap;
ListView Dealcloseby_listView;
String[] deal_data;
adapter2 ad;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflat and return the layout
    View v = inflater.inflate(R.layout.dealscloseby, container,
            false);
    mMapView = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 13.0294278;
    double longitude =80.24667829999999;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps").icon(BitmapDescriptorFactory.defaultMarker());

    // Changing marker icon
   // marker.icon(BitmapDescriptorFactory
           // .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
   googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(13.0294278, 80.24667829999999)).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));

    // Perform any camera updates here
    return v;
}

希望对您有所帮助。