2013-02-18 124 views
0

我想在我的Android应用程序中使用MapView类显示简单的地图。 我用下面的方法onCreate在我的活动:Android谷歌地图:地图视图初始化

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播放服务显然存在并初始化。

+0

看看这个教程的https: //developers.google.com/maps/documentation/android/v1/hello-mapview – Vivek 2013-02-18 12:15:25

回答

4

无法获取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

您试图访问Google Maps Android v1 Api这是现在弃用,请使用Google Map Android Api v2

+2

但我看看Google Maps Android API v2 API文档,我可以看到类MapView [here](https:// developer s.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView)? – 2013-02-19 02:33:44

2

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

+0

谢谢!这正是我错过的!现在,我可以使用自定义布局创建自己的自定义片段,包括MapView及其工作!现在我甚至可以在ViewPager中使用它,而不使用SupportMapFragment – 2017-06-29 19:12:40

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>