2

我有一個SupportMapFragment,我想添加一個簡單的標記,但由於某種原因,我無法在地圖上看到它...我沒有看到logcat中的任何錯誤理念?無法在SupportMap中查看標記或縮放谷歌地圖

public class MapUserFragment extends SupportMapFragment 
{ 

    private GoogleMap googleMap; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     super.onCreateView(inflater, container, savedInstanceState); 
     return inflater.inflate(R.layout.map_fragment_layout, container, false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) 
    { 

     super.onActivityCreated(savedInstanceState); 
     googleMap = getMap(); 
     if (googleMap != null) 
     { 
      try 
      { 

       googleMap.addMarker(new MarkerOptions().position(new LatLng(27.174840, 78.042500)).title("Hello World") 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_drawer))); 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
<fragment 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:id="@+id/map" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
class="com.google.android.gms.maps.SupportMapFragment" 
map:uiCompass="true" 
map:mapType= "normal" 
map:uiRotateGestures="true" 
map:uiScrollGestures="true" 
map:uiTiltGestures="true" 
map:uiZoomControls="true" 
map:uiZoomGestures="true" 
/> 

谷歌,播放清單services_lib

<?xml version="1.0" encoding="utf-8"?> 

package="com.google.android.gms" 
android:versionCode="3159130" 
android:versionName="3.1.59 (744626-30)" > 

<uses-sdk android:minSdkVersion="8"/> 

MyManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.tailgate" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="17" /> 
<!-- GCM connects to Google Services. --> 
<uses-permission android:name="android.permission.INTERNET" /> 

<!-- GCM requires a Google account. --> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

<!-- Keeps the processor from sleeping when a message is received. --> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="com.tailgate.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<!-- 
Creates a custom permission so only this app can receive its messages. 

NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE, 
     where PACKAGE is the application's package name. 
--> 
<permission 
    android:name="com.tailgate.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 


<!-- Location permission --> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<uses-feature 
    android:name="android.hardware.location" 
    android:required="true" /> 
<uses-feature 
    android:name="android.hardware.location.gps" 
    android:required="false" /> 

<application 
    android:name=".TailGateApplication" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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


    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyCKHkansQagp34Q5hiEEDm5XMPhT_Izntc" /> 
</application> 

編輯:

我也着放大到一定的LatLong

LatLng cur_Latlng =new LatLng(31.763160705566406, 35.20343017578125); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(cur_Latlng)); 
      googleMap.animateCamera(CameraUpdateFactory.zoomTo(10)); 
      googleMap.getUiSettings().setZoomControlsEnabled(true); 
+2

getMap()是否返回null? – CommonsWare

+1

你確定googleMap不爲null嗎? – tyczj

+0

否googlemap不爲空... – user1163234

回答

2

找到下面的soltution:

但我不知道這是爲什麼?是否因爲我需要在OnCreateView中添加標記?

public class MapUserFragment extends SupportMapFragment 
{ 
     public MapUserFragment() { 
     super(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) { 
     View v = super.onCreateView(arg0, arg1, arg2); 
     initMap(); 
     return v; 
    } 

    private void initMap(){ 
     LatLng lat = new LatLng(0, 0); 
     UiSettings settings = getMap().getUiSettings(); 
     settings.setAllGesturesEnabled(false); 
     settings.setMyLocationButtonEnabled(false); 

     // getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(lat,16)); 
     getMap().addMarker(new MarkerOptions().position(lat)); 
    } 
1

沒錯,你現在叫initMap() < - 方法是調用在onCreateView。在您嘗試在SupportMapFragment中創建標記之前。在創建地圖之前調用此方法,即使在之前onCreate Here you can read more about it