在Google Maps Android API v2.0下使用Fragment中的MapView

时间:2014-06-07 10:37:21

标签: android google-maps android-fragments

我在APIV2下的Fragment中使用mapview时遇到错误

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map, container, false);

        // try {
               // MapsInitializer.initialize(getActivity());
        //    } catch (GooglePlayServicesNotAvailableException e) {
                // TODO handle this situation
        //    }


        mMapView = (MapView) inflatedView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
       setUpMapIfNeeded(inflatedView);
        mMapView.onResume();

     //   try {
        //    MapsInitializer.initialize(getActivity());
     //   } catch (GooglePlayServicesNotAvailableException e) {
     //       e.printStackTrace();
     //   }

        mMap = mMapView.getMap();
        return inflatedView;
    }


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

    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(55,-4)).title("title"));
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

}

这是我的map.xml

   <?xml version="1.0" encoding="utf-8"?>
<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 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
    /> 



     <!-- 
<fragment
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
/>
    -->
</RelativeLayout>

错误发生在:** mMapView.onCreate(savedInstanceState); ** 然后,如果我取消命令

`// try {
               // MapsInitializer.initialize(getActivity());
        //    } catch (GooglePlayServicesNotAvailableException e) {
                // TODO handle this situation
        //    }`

错误将在此处:GooglePlayServicesNotAvailableException 它说这个

的无法搜索的阻止

需要别人帮忙

1 个答案:

答案 0 :(得分:0)

更改此部分代码:

} catch (GooglePlayServicesNotAvailableException e) {

为:

}catch (Exception e) {
   throw new RuntimeException(e);
}