Google 地图 Android API 我的位置问题

GoogleMaps Android API MyLocation issue

我有一个使用 GoogleMaps API 的应用程序,但遇到了点击 myLocationButton 无反应的问题。所以按钮是可见的,我看到了我的位置,但是当我点击按钮时它不会将相机放在 y 位置的中心。这是我的代码:

1) 带地图的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setRetainInstance(true);
    mapView = (MapView) v.findViewById(R.id.mapview);
    mapView.onCreate(null);
    MapsInitializer.initialize(this.getActivity());

    mMap = mapView.getMap();
    mMap.setMyLocationEnabled(true);

    UISettings = mMap.getUiSettings();
    UISettings.setMapToolbarEnabled(false);
    UISettings.setZoomControlsEnabled(true);
}

2) XML 与地图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:slidingLayer="http://schemas.android.com/apk/res-auto">

<com.google.android.gms.maps.MapView android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

我试过了,你可以使用我用过的以下代码,它工作正常:

1) 带地图的片段:

地图视图地图视图;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.mapview, container, false);
    // Gets the MapView from the XML layout and creates it

    MapsInitializer.initialize(getActivity());


    switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
        case ConnectionResult.SUCCESS:
            Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
            mapView = (MapView) v.findViewById(R.id.map);
            mapView.onCreate(savedInstanceState);
            // Gets to GoogleMap from the MapView and does initialization stuff
            if (mapView != null) {
                 mapView.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                        googleMap.setMyLocationEnabled(true);
                        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
                        googleMap.animateCamera(cameraUpdate);
                    }
                });

            }
            break;
        case ConnectionResult.SERVICE_MISSING:
            Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
            break;
        default:
            Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
    }

    return v;
}

2) XML 与地图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name=".YOUR_FRAGMENT_NAME_HERE"/>

</RelativeLayout>

请注意,您需要将 android:name=".YOUR_FRAGMENT_NAME_HERE" 更改为上面的片段名称。此时你应该可以开始了:)