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?
Accepted answer is good start point but if we will try to use it it will fail, it not describe full solution. Full working fragment with map which can be used in ViewPager
and Tabs
( notice using map in fragmeny lifecycle ):
public class AttractionMapTabFragment extends AttractionTab implements OnMapReadyCallback {
private ScrollMapView gMapView;
private GoogleMap gMap;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, null);
gMapView = (ScrollMapView) view.findViewById(R.id.map);
gMapView.getMapAsync(this);
//most important use onCreate;
gMapView.onCreate(getArguments());
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(51.3623329,21.7719342), 8));
}
@Override
public void onResume() {
super.onResume();
if (gMapView != null)
gMapView.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
if (gMapView != null)
gMapView.onDestroy();
}
@Override
public void onStart() {
super.onStart();
if (gMapView != null)
gMapView.onStart();
}
@Override
public void onStop() {
super.onStop();
if (gMapView != null)
gMapView.onStop();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (gMapView != null)
gMapView.onSaveInstanceState(outState);
}
}
Custom MapView
class - it is needed if we use ScrollView
as parent, it give possibility to move map.If You are not using ScrollView
in this screen then standard MapView
can be used.
public class ScrollMapView extends MapView {
public ScrollMapView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/**
* Request all parents to relinquish the touch events
*/
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
}
Last thing is layout - R.layout.fragment_map
:
<?xml version="1.0" encoding="utf-8"?>
<your.app.ScrollMapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With