2016-07-26 2 views
0

プレイスピッカーを使用するにはthisチュートリアルに従います。ここでは、onActivityResult()メソッドをどこに置くべきかについて言及していない。だから私は選択された場所の詳細を取得するために次のコードを試してみました。しかし、何の反応も得られない。プレイスピッカーを使用して選択したプレイス情報を取得できません

私の主な活動は、MapActivityの名前:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 


/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 

int PLACE_PICKER_REQUEST = 1; 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(-34, 151); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 



    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 
    try { 
     startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST); 
    } 
    catch (Exception e){ 

    } 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == PLACE_PICKER_REQUEST) { 
     if (resultCode == RESULT_OK) { 
      Place place = PlacePicker.getPlace(this, data); 
      String toastMsg = String.format("Place: %s", place.getName()); 
      Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

} 

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();が置かれるべき場所を私も知りません。

私はこれを検索するのに多くの時間をかけましたが、完璧な解決策を得ることはできません。どんな助けもありがとう。

ありがとうございました!

答えて

0

マップアクティビティでこのコードをstartActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);と呼ぶべきではありません。マップアクティビティ以外からアクティビティを呼び出す必要があります。これはおそらくボタンをクリックすると実行されます。いくつかの擬似コードで

例:

public class MyActivity extends AppCompatActivity { 

private Button mButton; 

    @Override 
    onCreate(Bundle savedInstanceState) { 

    mButton.setOnCLickListener(...); 
} 

private void onButtonClicked() { 
// this opens a new activity with a place picker 
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 
    startActivityForResult(builder.build(this),PLACE_PICKER_REQUEST); 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // when you click back from place picker, it is handled in this method 
    if (requestCode == PLACE_PICKER_REQUEST) { 
     if (resultCode == RESULT_OK) { 
      Place place = PlacePicker.getPlace(this, data); 
      String toastMsg = String.format("Place: %s", place.getName()); 
      Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show(); 
     } 
    } 

} 
関連する問題