GoogleMaps Android API MyLocation问题

时间:2015-07-28 09:43:05

标签: android google-maps google-maps-android-api-2

我有使用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 个答案:

答案 0 :(得分:2)

我试过,您可以使用我使用的以下代码,它运行正常:

1)带地图的片段:

MapView mapView;

@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"更改为上面的片段名称。此时你应该好好去:))