2017-11-23 5 views
1

사용자가 선택한 이벤트의 주소를 가져 오는 앱을 개발 중입니다. 주소는 문자열이며 한 활동에서 Google지도 활동으로 전달됩니다. 토스트를 사용하여 표시 한 문자열이지도 활동에 도착한다는 것을 알고 있습니다. 그러나 주소의 위도와 경도를 얻으려고하면 작동하지 않습니다. try catch 블록은 하드 코드 및 주소가 없으면 실행되지 않는 것 같습니다. addresses = geocoder.getFromLocationName('Dublin,Ireland', 1);의도를 통해 주소를 Google지도 활동에 전달 :

어떤 도움을 주시면 감사하겠습니다.

public class EventMapActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 
private Intent MyTournamentsActivityIntent; 
private String eventAddress = ""; 
double latitude = 0; 
double longitude = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_event_map); 
    // 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); 

    MyTournamentsActivityIntent = getIntent(); 
    eventAddress = MyTournamentsActivityIntent.getStringExtra("event_location"); 
    Toast.makeText(getApplicationContext(),"Address from intent" + eventAddress,Toast.LENGTH_SHORT).show(); 
}//end onCreate 


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

    Geocoder geocoder = new Geocoder(this); 
    List<Address> addresses; 

    try{ 

     /* 
     This doesnt execute unless I hard code the eventAddress 
     */ 
     addresses = geocoder.getFromLocationName(eventAddress, 1); 
     latitude= addresses.get(0).getLatitude(); 
     longitude= addresses.get(0).getLongitude(); 
     Toast.makeText(getApplicationContext(),String.valueOf(latitude) + " " + String.valueOf(longitude) ,Toast.LENGTH_SHORT).show(); 
    }catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    LatLng eventLocation = new LatLng(latitude, longitude); 
    mMap.addMarker(new MarkerOptions().position(eventLocation).title("Your Event")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(eventLocation)); 
}//end onMapReady 

public void setEventAddress(String eventAddress) { 
    this.eventAddress = eventAddress; 
} 

} // 최종 클래스

답변

1

변경이 : 여기에

eventAddress = MyTournamentsActivityIntent.getStringExtra("event_location"); 

:

eventAddress = getIntent().getStringExtra("event_location"); 

그리고는 전화 mapFragment.getMapAsync(this); :

eventAddress = getIntent().getStringExtra("event_location"); 
mapFragment.getMapAsync(this); 

eventAddress를받은 후

+0

답장을 보내 주셔서 감사합니다. 아래의 방법을 시도했지만 여전히 작동하지 않습니다. 'SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById (R.id.map); eventAddress = getIntent(). getStringExtra ("event_location"); mapFragment.getMapAsync (this); ' – Pony1487

+0

이전 활동에서 여분을 올바르게 보내시겠습니까? – diegoveloper

+0

이것이 내가 보내는 방법입니다. 그것의 ListActivity 클래스. 아이템을 클릭하면 다음을 수행합니다. Intent intent = new Intent (MyTournamentsActivity.this, EventMapActivity.class); intent.putExtra ("event_location", userEventList.get (position) .getAddress()); startActivity (의도);' – Pony1487

관련 문제