2015-11-08 56 views
0

我正在嘗試將googlemap添加到我的應用程序中,但無法使其正常工作。 我在xamarin btw上使用ViewPager。 佈局:在Android應用程序中設置GoogleMap

... 
      <fragment 
       android:id="@+id/map" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:name="com.google.android.gms.maps.MapFragment" /> 
... 

我的片段:

public class InfoFragment : FragmentV4, IOnMapReadyCallback 
{ 
    private GoogleMap mMap; 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     var view = inflater.Inflate(Resource.Layout.InfoFragment, container, false); 

     //Set up google maps coords in infos section 
     SetUpMap(); 
     return view; 
    } 

    private void SetUpMap() 
    { 
     if (mMap == null) { 
      //((MapFragment)FragmentManager.FindFragmentById (Resource.Id.map)).GetMapAsync (this); 

      var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map); 

      mapFragment.GetMapAsync(this); 
     } 
    } 

    //Launching of the actual map 
    public void OnMapReady (GoogleMap googleMap) 
    { 

     //Setup cords and stuff... 
    } 
} 

在我的地圖的設置。當我做

var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map); 

它總是返回null。有人可以幫忙嗎?

回答

0

發現問題

android:name="com.google.android.gms.maps.MapFragment" /> 

應該是:

android:name="com.google.android.gms.maps.SupportMapFragment" /> 

,在我的片段,而不是:

var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map); 

      mapFragment.GetMapAsync(this); 

它應該是:

((SupportMapFragment)ChildFragmentManager.FindFragmentById(Resource.Id.map)).GetMapAsync(this); 
相關問題