GoogleMap not showing the changes I made

前端 未结 3 1363
日久生厌
日久生厌 2021-01-07 16:08

I have a fragment in which I use (or rather, want to use) Google Maps.

The fragment is between an actionbar and a tabhost, the fragment\'s layout is

相关标签:
3条回答
  • 2021-01-07 16:18

    I think you have not linked map received by onMapReady with map you have in your xml fragment, so your fragment just gets loaded, therefore, you see a map, but your changes not reflected.

    Code from my app working fine for me :

    // In my onCreate(..) :
    ....
    ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
    ....
    

    Then :

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;   // map is your global variable.
    
        // Everything below works fine : 
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setBuildingsEnabled(true);
        map.getUiSettings().setCompassEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(false);
        map.setOnMarkerDragListener(this);
        map.setOnMarkerClickListener(this);
        map.setOnCameraChangeListener(this);
    }
    

    Hope this helps...

    0 讨论(0)
  • 2021-01-07 16:20

    You haven't moved the camera to your position. By animating or moving the camera to your Latitude and Longitude you can get what you are trying to get:

    private static final LatLng SYDNEY = new LatLng(-33.88,151.21);
    private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1);
    
    private GoogleMap map;
    ... // Obtain the map from a MapFragment or MapView.
    
    // Move the camera instantly to Sydney with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
    
    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomIn());
    
    // Zoom out to zoom level 10, animating with a duration of 2 seconds.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    
    // Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
        .zoom(17)                   // Sets the zoom
        .bearing(90)                // Sets the orientation of the camera to east
        .tilt(30)                   // Sets the tilt of the camera to 30 degrees
        .build();                   // Creates a CameraPosition from the builder
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
    0 讨论(0)
  • 2021-01-07 16:40

    With some help from @AbhinavPuri I got it to work. What I had to do was

    Change

    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment" />
    

    to

    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment" />
    

    Made my fragment extend Fragment instead of extend SupportMapFragment and, because I use a GoogleMap in a fragment and not in an activity, I had to move the 'map getting' code to the onCreateView. If you don't, findFragmentById returns null, because in the onCreate, the view is not yet inflated.

    Now using this piece of code

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        /* If you place a mapfragment inside a fragment, it crashes when the fragment is
         * loaded a 2nd time. Below solution was found at http://stackoverflow.com/questions/
         * 14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi
         */
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        try {
            // Inflate the layout for this fragment.
            view = inflater.inflate(R.layout.fragment_search_friends_map, container, false);
        } catch (InflateException e) {
            // Map is already there, just return view as it is.
        }
    
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
        return view;
    }
    

    Using getChildFragmentManager because the SupportMapFragment is nested in another fragment.

    0 讨论(0)
提交回复
热议问题