getMap()函数出错

时间:2016-08-02 17:12:26

标签: android mapactivity

我只是创建一个Map项目并执行此代码。但是有getMap()错误。 我已尝试过一些来自互联网的示例代码,但getMap()函数中也存在相同的错误。守则是:

    private GoogleMap mMap;
    static final LatLng TutorialsPoint = new LatLng(21 , 57);
     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);


    if (mMap == null) {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    try {
        if (mMap == null) {

                   mMap = ((MapFragment) getFragmentManager().
                   findFragmentById(R.id.map)).getMap();
                    //findFragmentById(R.id.map)).getMap();
        }
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        Marker TP = mMap.addMarker(new MarkerOptions().
                position(TutorialsPoint).title("TutorialsPoint"));
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}




@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

这是我的Android清单文件。

 <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.priya.finalproject">

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/.
    -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCBp-ide4ue3DKt-tud4SdzwzR652OSnGY" />
    <!--  <meta-data
         android:name="com.google.android.geo.API_KEY"
         android:value="@string/google_maps_key" />-->

     <activity
         android:name=".MapsActivity"
         android:label="@string/title_activity_maps">
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />

             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
     </activity>
 </application>

1 个答案:

答案 0 :(得分:2)

你应该在触发onMapReady功能时将标记添加到你的地图中,当地图准备就绪时会发生这种情况(比如添加标记等)。只需初始化地图并调用getMapAsync函数,如< / p>

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this); 

使用googleMap对象操纵你在onMapReady内给你的地图。