Download a Google Google snippet managed by ViewPager

I can load the Google map into an Android fragment that is inside the action. This is working fine.

But now I want to use the ViewPager to navigate between views ( android.support.v4.app.Fragment class). Unable to load 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)); } } 

Statement

 getFragmentManager().findFragmentById(R.id.holeViewMap); 

causes a compiler error (incompatible types).

Instead, I tried using SupportMapFragment, which fixes the compiler errors, but then when I launch the application, it immediately terminates with the message "I / O error: connection refused." Google docs seem to indicate that in order to use the support library you need a special β€œWork” account. It's right? If so, I think I’m out of luck.

The only way I can do this is to use actions instead of fragments to post my views, i.e. get rid of ViewPager .

Any suggestions?

+7
android android-fragments google-maps fragmentpageradapter
source share
4 answers

In your sole_map.xml you should 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 identifier view.findViewById(R.id.soleViewMap); . This will return MapView to you, then you can do what you are 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)); } } 

Remember to associate your mapview with the fragment life cycle that onCreate , onLowMemory , onPause , onResume and onDestroy (I hope they haven't disappeared) and call MapsInitializer.initialize(getContext()); , and

+12
source share

The accepted answer is a good starting point, but if we try to use it, it will not work, it does not describe the complete solution. Full working snippet with a map that can be used in ViewPager and Tabs ( notification using a map in the fragmeny life cycle ):

 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 - this is necessary if we use ScrollView as a parent, this makes it possible to move the map. If you are not using ScrollView on this screen, then the 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); } } 

The last thing is the 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" /> 
+6
source share
 public class MapViewFragment extends Fragment implements OnMapReadyCallback{ private GoogleMap mMap; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.activity_maps, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //FragmentManager fragment = getActivity().getSupportFragmentManager(); // Fragment fragment=(Fragment) getChildFragmentManager().findFragmentById(R.id.mapView); final SupportMapFragment myMAPF = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map); myMAPF.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(25.412383, 55.508085); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } 
0
source share

Enter this code in your activity_maps.xml <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> file <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" />

0
source share

All Articles