I am working on an app that gets the address of an event the user selects. The address is a String and is passed from one activity to a Google Map activity. I know the string arrives in the map activity as I have used a Toast to display it.
However, when I try to get the longitude and latitude of the address it doesn't work. The try catch block doesn't seem to execute unless I hard code and address into the addresses = geocoder.getFromLocationName('Dublin,Ireland', 1);
Any help would be greatly appreciated.
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;
}
}//end Class