Issue
Hi I 'm new to Android programming and I have an app that when user clicks on a country it starts another activity displaying an a map with a marker locating the capital city.
But I need to get the capital from my async task method.So it needs to return country info and then I can update my map.I called my update map method in onPost Execute but the app just crashes?
In my on Create
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync( this);
new GetCountryDetails().execute(selectedCountry);
Then in my Async task
private class GetCountryDetails extends AsyncTask<String, String, CountryInfo> {
@Override
protected CountryInfo doInBackground(String... params) {
CountryInfo countryInfo = null;
In my onPostExecute
protected void onPostExecute(CountryInfo countryInfo) {
countryCapital=countryInfo.getCapital();
updateMap(map);//doesn't work here
onMapReady:
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.getUiSettings().setMapToolbarEnabled(true);
updateMap
public void updateMap(GoogleMap map){
LatLng capitalCity = getCityLatitude(this, countryCapital);
map.moveCamera(CameraUpdateFactory.newLatLng(capitalCity));
MarkerOptions marker = new MarkerOptions().position(capitalCity).title( countryCapital);
map.addMarker(marker);
}
Any help will be appreciated thank!
I
Solution
Create Map varible as activity field.And define variable in onMapReady function.After that use activity variable as you want. I think it should works.
GoogleMap map;
public void onMapReady(GoogleMap map) {
this.map = map;
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.getUiSettings().setMapToolbarEnabled(true);
}
Answered By - Kishan Thakar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.