GoogleMap not showing the changes I made

北城余情 提交于 2019-12-03 22:59:51

问题


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

<SeekBar
    android:id="@+id/search_map_seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="50"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/seekbar_thumb"
    android:layout_marginTop="5dp"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/seekbar_min"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/seekbar_mid"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/seekbar_max"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"/>

    </RelativeLayout>

<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" />

and it looks like this

Now I want to actually work with it. I followed Google's Map documentation/tutorial and ended up with this code

public class SearchFriendsMapFragment extends SupportMapFragment implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private static View view;
    private GoogleMap map;
    private GoogleApiClient googleApiClient;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        buildGoogleApiClient(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getMapAsync(this);
    }

    @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.
        }

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        googleApiClient.connect();
    }

    @Override
    public void onStop() {
        googleApiClient.disconnect();

        super.onStop();
    }

    protected synchronized void buildGoogleApiClient(Context context) {
        Log.i(LogUtil.TAG, "BUILDING GOOGLE API CLIENT");
        googleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.i(LogUtil.TAG, "MAP READY");

        Log.i(LogUtil.TAG, String.valueOf(googleMap.getMapType()));
        googleMap.setMyLocationEnabled(true);
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        Log.i(LogUtil.TAG, String.valueOf(googleMap.getMapType()));

        //map = googleMap;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i(LogUtil.TAG, "CONNECTED");

        Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

        if (lastLocation != null) {
            Log.i(LogUtil.TAG, String.valueOf(lastLocation.getLatitude()));
            Log.i(LogUtil.TAG, String.valueOf(lastLocation.getLongitude()));
            LatLng latlng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
            //map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 5));
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e(LogUtil.TAG, "CONNECTION FAILED");
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(LogUtil.TAG, "CONNECTION SUSPENDED");
    }
}

Today I updated to the new Android 24.0.2 SDK and Google Play Services v22. I adjusted the gradle file accordingly and it all builds fine.

compile 'com.google.android.gms:play-services-maps:6.5.87'
compile 'com.google.android.gms:play-services-location:6.5.87'

There are no errors or warnings in the code and the fragment loads everything without complaining. Required permissions and API key are also set in the manifest.

However, I seem unable to actually interact with the map. The overriden methods all run (I see the log for each method in logcat)

but especially these 2 lines seem to not do anything.

googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

The map just shows as in the picture above. The current location isn't shown (GPS, wifi is enabled), nor is the map type changed. Especially the map type not changing I find weird, because in my logs I can see it actually did change (from 1 to 4).

Where am I going wrong? What am I missing?


回答1:


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...




回答2:


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.




回答3:


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));


来源:https://stackoverflow.com/questions/27549521/googlemap-not-showing-the-changes-i-made

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!