I can load a google map into an Android fragment that's within an activity. That has been working fine.
But now I want to use ViewPager to navigate between views (class android.support.v4.app.Fragment
). It doesn't seem possible to load a com.google.android.gms.maps.MapFragment
into such a fragment.
For example, in:
SoleMap.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
public class SoleMap extends Fragment implements OnMapReadyCallback {
MapFragment gMapFragment;
GoogleMap gMap = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sole_map, container, false);
gMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.soleViewMap);
gMapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(49.39,-124.83), 20));
}
}
The statement
getFragmentManager().findFragmentById(R.id.holeViewMap);
causes a compiler error (incompatible types).
I have tried to use SupportMapFragment instead, which eliminates the compiler errors, but then when I run the app, it quits immediately with the message "I/O Error: Connection refused." The Google docs seem to indicate that you need a special "For Work" account to use the Support library. Is that correct? If so, I guess I'm out of luck.
The only other way I can see to do it is to use activities instead of fragments to host my views, i.e., get rid of the ViewPager.
Any suggestions?
In your sole_map.xml
you must add a MapView, let's say something like this:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
android:id="@+id/soleViewMap"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then get it from your SoleMap fragment with the id view.findViewById(R.id.soleViewMap);
This will return you a MapView, then you can do what you're doing now. It should look something like this:
public class SoleMap extends Fragment implements OnMapReadyCallback {
MapView gMapView;
GoogleMap gMap = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sole_map, container, false);
gMapView = (MapView) view.findViewById(R.id.soleViewMap);
gMapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(49.39,-124.83), 20));
}
}
Don't forget to associate your mapview with the fragment lifecycle calling the events onCreate
, onLowMemory
, onPause
, onResume
and onDestroy
(hope not missing any of them), and call to MapsInitializer.initialize(getContext());
as well