Now that you have the Maps API set up, you need to create a map. Maps are displayed, appropriately enough, in a MapView. MapView is like other views, mostly, except in one way: For it to work correctly, you have to forward all of your lifecycle events, like this:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMapView.onCreate(savedInstanceState); }
This is a huge pain in the neck. It is far easier to let the SDK do that work for you instead by using a MapFragment or, if you are using support library fragments, SupportMapFragment. The MapFragment will create and host a MapView for you, including the proper lifecycle callback hookups.
Your first step is to wipe out your old UI entirely and replace it with a SupportMapFragment. This is not as painful as it might sound. All you need to do is switch to using a SupportMapFragment, delete your onCreateView(…) method, and delete everything that uses your ImageView.
Listing 34.3 Switching to SupportMapFragment
(LocatrFragment.java
)
public class LocatrFragment extends SupportMapFragmentFragment{ private static final String TAG = "LocatrFragment"; private static final String[] LOCATION_PERMISSIONS = new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, }; private static final int REQUEST_LOCATION_PERMISSIONS = 0;private ImageView mImageView;private GoogleApiClient mClient; ...@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View v = inflater.inflate(R.layout.fragment_locatr, container, false);mImageView = (ImageView) v.findViewById(R.id.image);return v;}... private class SearchTask extends AsyncTask<Location,Void,Void> { ... @Override protected void onPostExecute(Void result) {mImageView.setImageBitmap(mBitmap);} } }
SupportMapFragment has its own override of onCreateView(…), so you should be all set. Run Locatr to see a map displayed (Figure 34.1).
18.191.223.123