2016-07-27 2 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 또는 다른 클래스 파일에 있습니까?

다음 코드는지도를 표시합니다.

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


} 
} 

은 내가 place picker를 호출하는 클래스 위의 어떤 변화에해야합니다. 그리고 첫 번째 코드 스 니펫을 구현하는 방법보다 위의 클래스에 배치해야한다면? 이런 유형의 예제를 찾으려고 여러 번 시도했지만 내 문제를 해결할 수없는 것을 찾을 수 없습니다. 특히 this 튜토리얼을 사용해 보았습니다. 그러나 그것은 많은 오류를줍니다.

나는 안드로이드에서 초보자입니다. 미리 감사드립니다!

답변

1

두 번째 링크 (truiton.com)의 자습서는 저에게 적합합니다. 당신은 튜토리얼 및 클래스 내부의 함수와 같은 특정 작업을 실행하는 블록이 코드

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

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

을 넣어해야합니다. 사용자 작업 옵션이있는 경우 MapsActivity 클래스 자체에서이 작업을 수행 할 수 있습니다.

+0

전체 코드를 제공 할 수 있습니까? –

관련 문제