2017-04-03 88 views
0

我嘗試在我的Fragment中顯示Google Maps。問題是當我運行我的項目,我沒有看到這個地圖。附加信息是這些片段屬於尋呼機視圖。如何創建GPS工具?

這是我的代碼示例:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     thisContext = inflater.getContext(); 
     View view = inflater.inflate(R.layout.tab_fragment_2, container, false); 
     loadMap(); 
     // initializeMap(); 

     locationManager = (LocationManager) thisContext.getSystemService(Context.LOCATION_SERVICE); 

     if (ActivityCompat.checkSelfPermission(thisContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(thisContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return null; 
     } 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this); 

     return view; 

    } 


    private void initializeMap() { 
     if (map == null) { 
      SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
      mapFrag.getMapAsync(this); 
     } 
    } 

    private void loadMap() { 
     SupportMapFragment mSupportMapFragment; 

     mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
     if (mSupportMapFragment == null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      mSupportMapFragment = SupportMapFragment.newInstance(); 
      fragmentTransaction.replace(R.id.map, mSupportMapFragment).commit(); 
     } 

     if (mSupportMapFragment != null) { 
      mSupportMapFragment.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        if (googleMap != null) { 

         googleMap.getUiSettings().setAllGesturesEnabled(true); 

         // -> marker_latlng // MAKE THIS WHATEVER YOU WANT 

         //CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build(); 
         // CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition); 
         // googleMap.moveCamera(cameraUpdate); 

        } 

       } 
      }); 
     } 
    } 

回答

0

問題是,當我跑我的項目,我沒有看到這個地圖。

您可以使用默認的「新建活動」嚮導來創建Google地圖活動。它包括您需要的所有信息。請參閱以下屏幕截圖中的第7項。

0

迄今爲止最好的事情我知道的片段是使用MapView,而不是MapFragment。我在Navigation Drawer片段內使用MapView,其工作正常,我需要的所有callbacksPlacesAutoComplete