2016-08-02 110 views
1

我只是創建一個Map項目並執行此代碼。但有getMap()錯誤。 我嘗試了一些來自互聯網的示例代碼,但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> 

+0

你會得到什麼錯誤?理想情況下,包括整個堆棧應該有所幫助 – Acapulco

+0

@Acapulco Error Mesg顯示爲: - 12:59:34 AM IllegalStateException:由於ConnectorServiceRegistry已關閉,無法找到DefaultGradleConnector類型的對象的工廠。 1:01:55 AM IllegalStateException:由於ConnectorServiceRegistry已關閉,無法找到DefaultGradleConnector類型的對象的工廠。 上午1:02:47 IllegalStateException:無法找到DefaultGradleConnector類型的對象的工廠,因爲ConnectorServiceRegistry已關閉。 – RedEyes

+0

@Acapulco你能幫我嗎? – RedEyes

回答

2

你應該添加標記到地圖時onMapReady功能被觸發,當你的地圖就可以使用(如添加標記等)。只是初始化您的地圖,並呼籲getMapAsync功能像

這種情況發生
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

使用googleMap對象來操縱你在onMapReady內給你的地圖。