2016-07-27 8 views
0

私は、Googleマップ上の選択した場所の情報を表示する簡単なアプリを作ろうとしています。アンドロイドのドキュメントから、私はそれがPlace Pickerを使って行うことができることを発見しました。 Googleが提供するチュートリアルthisに従っています。アンドロイドでプレイスピッカーを実装する方法は?

この文書では、place pickerの起動に次のコードを使用したと言われています。

int PLACE_PICKER_REQUEST = 1; 
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 

startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST); 

また、ユーザーが選択した場所の詳細を取得する方法もあります。

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

しかし、私の問題はどこに私のアプリケーションにこのコードを配置する必要がありますか?私のMapsActivityまたは他のクラスファイルですか?

マップを表示する次のコードを実行しました。私はplace pickerを呼び出すために、クラスの上で行う必要がありますどのような変更

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. 
*/ 

@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)); 


} 
} 

。そして、第1のコードスニペットを実装する方法よりも上のクラスに配置する必要がある場合は、私はこのタイプの例を見つけるのに何度も試みましたが、私の問題を解決できるものは見つかりませんでした。特に、thisチュートリアルを試しました。しかし、それは多くのエラーを与える。

私はアンドロイドで初心者です。前もって感謝します!

答えて

1

2番目のリンク(truiton.com)のチュートリアルは私にとってうまくいきます。このコードを入れる必要があります

int PLACE_PICKER_REQUEST = 1; 
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 

startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST); 

ブロック内のチュートリアルやクラス内の関数のような特定のアクションで実行します。ユーザーアクションオプションがいくつかある場合は、MapsActivityクラス自体で使用できます。

+0

あなたはコード全体を提供できますか? –

関連する問題