How to put Google Maps V2 on a Fragment using ViewPager

前端 未结 13 2152
攒了一身酷
攒了一身酷 2020-11-22 04:56

I am trying to do a tab layout same in Play Store. I got to display the tab layout using a fragments and viewpager from androidhive. However, I can\'t implement google maps

13条回答
  •  无人共我
    2020-11-22 05:39

    public class DemoFragment extends Fragment {
    
    
    MapView mapView;
    GoogleMap map;
    LatLng CENTER = null;
    
    public LocationManager locationManager;
    
    double longitudeDouble;
    double latitudeDouble;
    
    String snippet;
    String title;
    Location location;
    String myAddress;
    
    String LocationId;
    String CityName;
    String imageURL;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater
                    .inflate(R.layout.fragment_layout, container, false);
    
        mapView = (MapView) view.findViewById(R.id.mapView);
            mapView.onCreate(savedInstanceState);
    
      setMapView();
    
    
     }
    
     private void setMapView() {
        try {
            MapsInitializer.initialize(getActivity());
    
            switch (GooglePlayServicesUtil
                    .isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                // Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT)
                // .show();
    
                // Gets to GoogleMap from the MapView and does initialization
                // stuff
                if (mapView != null) {
    
                    locationManager = ((LocationManager) getActivity()
                            .getSystemService(Context.LOCATION_SERVICE));
    
                    Boolean localBoolean = Boolean.valueOf(locationManager
                            .isProviderEnabled("network"));
    
                    if (localBoolean.booleanValue()) {
    
                        CENTER = new LatLng(latitude, longitude);
    
                    } else {
    
                    }
                    map = mapView.getMap();
                    if (map == null) {
    
                        Log.d("", "Map Fragment Not Found or no Map in it!!");
    
                    }
    
                    map.clear();
                    try {
                        map.addMarker(new MarkerOptions().position(CENTER)
                                .title(CityName).snippet(""));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    map.setIndoorEnabled(true);
                    map.setMyLocationEnabled(true);
                    map.moveCamera(CameraUpdateFactory.zoomTo(5));
                    if (CENTER != null) {
                        map.animateCamera(
                                CameraUpdateFactory.newLatLng(CENTER), 1750,
                                null);
                    }
                    // add circle
                    CircleOptions circle = new CircleOptions();
                    circle.center(CENTER).fillColor(Color.BLUE).radius(10);
                    map.addCircle(circle);
                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
                }
                break;
            case ConnectionResult.SERVICE_MISSING:
    
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
    
                break;
            default:
    
            }
        } catch (Exception e) {
    
        }
    
    }
    

    in fragment_layout

    
    

提交回复
热议问题