2015-11-19 75 views
0

我試圖從Google Places API的地方檢索歸屬地,我遵循Google教程,但無法在Google地圖上顯示歸因。Google Places API的顯示歸屬地

我嘗試在同一活動的文本視圖中檢索歸因。

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) { 

} 

}

+0

你能更具體 - 什麼部分不工作?這將幫助我們回答你的問題。例如。你是否得到一個例外,還是僅僅是PlacePicker工作並返回一個結果,但沒有發現歸因? [也就是說我會猜測並試着回答,但更多細節將會有所幫助。] – spiv

回答

0

你太早打電話PlacePicker.getAttributions,它有一個結果之前,所以沒有歸屬準備好了沒有。

改爲在onActivityResult方法中調用它。請參閱PlacePicker sample app以瞭解具體的示例。