Android谷歌地图:地图视图初始化

时间:2013-02-18 12:07:44

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

我正在尝试使用MapView类在我的Android应用程序中显示一个简单的地图。 我在我的活动中使用了以下onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        MapsInitializer.initialize(this);
    } catch (GooglePlayServicesNotAvailableException e) {
        Log.e("Address Map", "Could not initialize google play", e);
    }

    MapView mapView = new MapView(this);
    CameraUpdate camPos = CameraUpdateFactory.newLatLng(new LatLng(11.562276,104.920292));
    mapView.getMap().moveCamera(camPos);
    setContentView(mapView);
}

我有一个NullPointerException,因为方法mapView.getMap()会返回null。 不明白为什么,Google Play服务显然存在并初始化。

4 个答案:

答案 0 :(得分:5)

无法让MapView工作,我最终使用课程SupportMapFragment结束了。

对于那些可能有用的人,这是我活动的完整代码:

public class AddressMap extends android.support.v4.app.FragmentActivity {

    private final static int FRAGMENT_ID = 0x101;
    private GoogleMap mMap;

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

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        SupportMapFragment fragment = SupportMapFragment.newInstance();
        layout.setId(0x101);
        {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(FRAGMENT_ID, fragment);
            fragmentTransaction.commit();
        }

        setContentView(layout);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }


    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(FRAGMENT_ID)).getMap();
            if (mMap != null) {
                mMap.moveCamera(
                    CameraUpdateFactory.newLatLngZoom(new LatLng(11.562276, 104.920292), 10));
            }
        }
    }
}

答案 1 :(得分:2)

此外,必须调用mapView.onCreate()。

答案 2 :(得分:1)

您正在尝试访问现已已弃用的Google Maps Android v1 Api ,请使用Google Map Android Api v2

答案 3 :(得分:0)

我像这样使用它并且它有效,这是新的api:

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

public class MapViewFragment extends Fragment{
public static final String TAG = MapViewFragment.class.getSimpleName();

MapView mapView;
GoogleMap map;

static MapViewFragment instance;

public static MapViewFragment newInstance(){
    if(instance == null){
        instance = new MapViewFragment();
        return instance;
    }else{
        return instance;
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.i(TAG, "onCreateView: " );

    View view = inflater.inflate(R.layout.map_view_fragment, container, false);
    mapView = (MapView) view.findViewById(R.id.mvMap);
    if(mapView != null){

        mapView.onCreate(null);  //Don't forget to call onCreate after get the mapView!


        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
                //Do what you want with the map!!
            }
        });

        MapsInitializer.initialize(this.getActivity());
    }else{
        Log.i(TAG, "onCreateView: mapView is null");
    }

    return view;
}

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

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

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

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

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

</com.google.android.gms.maps.MapView>

</LinearLayout>