显示来自Google Places API的归因

时间:2015-11-19 21:45:55

标签: android google-maps google-places-api

我正在尝试从使用Google Places API的地方检索归因,我按照Google的教程进行操作,但我无法在Google地图上显示归因。

我尝试在同一个活动的Textview中检索归因。

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;
import android.content.Intent;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlacePicker;

public class PlacesActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_places);
    mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    try {
        displayPlacePicker();

    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    } catch (GooglePlayServicesRepairableException e) {
        e.printStackTrace();
    }
    Intent intent = getIntent();
    Display_Attribution(intent);
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}


private void displayPlacePicker() throws GooglePlayServicesNotAvailableException, GooglePlayServicesRepairableException {
    int PLACE_PICKER_REQUEST = 1;
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();


    try {
        startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
    } catch (GooglePlayServicesRepairableException e) {
        Log.d("PlacesAPI Demo", "GooglePlayServicesRepairableException thrown");
    } catch (GooglePlayServicesNotAvailableException e) {
        Log.d("PlacesAPI Demo", "GooglePlayServicesNotAvailableException thrown");
    }


}

    public void Display_Attribution(Intent var) {
    Intent intent = new Intent(PlacesActivity.this, ShowActivity.class);
    TextView attributionsText = (TextView) findViewById(R.id.textPlace);
    String thirdPartyAttributions = PlacePicker.getAttributions(var);
    if (thirdPartyAttributions == null) {
        thirdPartyAttributions = "";
    }
    attributionsText.setText(Html.fromHtml(thirdPartyAttributions));
    startActivity(intent);
}



@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

1 个答案:

答案 0 :(得分:0)

你在结果出现之前过早地调用PlacePicker.getAttributions,所以还没有准备就绪。

而是在onActivityResult方法中调用它。请参阅PlacePicker sample app以获取完全相同的具体示例。