2017-11-13 13 views
0

私のアプリケーションは、断片上に空のGoogleマップを表示します。私はMainActivityからその断片を呼び出すだけです。私はGoogleで検索した/読んだが、誰も私を助けなかった。私は何のエラーもなく、私は適切なAPIキーを使用しています。 次は私のクラス/ mxlsです:、何かを見つけるために私を助けてください地図が空白/何も表示されない

public class MainActivity extends AppCompatActivity { 


    private Button map_me; 
    private Button bCordinates; 
    public static Location mLastKnownLocation; 
    private FusedLocationProviderClient mFusedLocationProviderClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); 

     try { 
      if (true) { 
       Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation(); 
       locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() { 
        @Override 
        public void onComplete(@NonNull Task<Location> task) { 
         if (task.isSuccessful()) { 

          //Get the Location of the current device 
          mLastKnownLocation = task.getResult(); 
         } 
        } 
       }); 
      } 
     } catch (SecurityException e) { 
      Log.e("Exception: %s", e.getMessage()); 
     } 

     bCordinates = (Button)findViewById(R.id.bcordinates); 
     bCordinates.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) 
      { 
       Toast.makeText(getApplicationContext(),""+mLastKnownLocation.getLatitude()+" , " + 
         " "+mLastKnownLocation.getLongitude(),Toast.LENGTH_LONG).show(); 
      } 
     }); 


     map_me = (Button)findViewById(R.id.bmap); 
     map_me.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       FragmentManager fragmentManager = getFragmentManager(); 
       android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       MyMapFragment myMapFragment = new MyMapFragment(); 
       fragmentTransaction.add(R.id.fragment_container, myMapFragment); 
       fragmentTransaction.commit(); 
       } 
     }); 
    } 
} 

public class MyMapFragment extends Fragment implements OnMapReadyCallback { 


private GoogleMap googleMap; 
private MapView mapView; 
private View view; 
private FusedLocationProviderClient mFusedLocationProviderClient; 

public MyMapFragment() { 

} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

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


@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    mapView = (MapView) getView().findViewById(R.id.map); 
    if(mapView!=null) 
    { 
     mapView.onCreate(null); 
     mapView.onResume(); 
     mapView.getMapAsync(this); 
    } 
} 

@Override 
public void onMapReady(final GoogleMap mGoogleMap) { 
    { 
     googleMap = mGoogleMap; 
     LatLng mLatLng = new LatLng(mLastKnownLocation.getLatitude(), 
       mLastKnownLocation.getLongitude()); 

     googleMap.addMarker(new MarkerOptions().position(mLatLng).title("You're here")); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng,15)); 
     mapView.onResume(); 
    } 
} 
} 

マップフラグメントレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MyMapFragment"> 

    <com.google.android.gms.maps.MapView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/map" 
     /> 

</FrameLayout> 

メインアクティビティのレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.bdushimi.android2.MainActivity" 
    > 

    <Button 
     android:id="@+id/bmap" 
     android:layout_width="88dp" 
     android:layout_height="wrap_content" 
     android:text="MAP Me!" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginStart="8dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.067" 
     tools:layout_editor_absoluteY="32dp" /> 

    <Button 
     android:id="@+id/btext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TEXT ME!" 
     tools:layout_editor_absoluteY="32dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="0.464" /> 

    <Button 
     android:id="@+id/bcordinates" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Cordinates" 
     android:layout_marginEnd="8dp" 
     tools:layout_editor_absoluteY="32dp" 
     android:layout_marginRight="28dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintHorizontal_bias="0.949" 
     app:layout_constraintLeft_toLeftOf="parent" /> 

    <LinearLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="333dp" 
     android:layout_height="370dp" 
     android:orientation="vertical" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="125dp" 
     app:layout_constraintHorizontal_bias="0.514"></LinearLayout> 
</android.support.constraint.ConstraintLayout> 

私は間違っている。

+1

ここ – diegoveloper

+0

をあなたのlogcatを入れて、あなたがテストしているデバイスが動作し、インターネットを持っています接続? – m0skit0

+0

logcatを確認すると、APIキーまたはデバイスIDの登録がない可能性があります。 – HTMLlama

答えて

0

あなたの使いやすさに応じて、マップレイアウトxmlファイルでMapFragmentまたはSupportMapFragmentを使用する必要があります。今の

: 交換してください:

をして:

<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/headerLayout" 
     tools:context="com.dxs.rnf.winwin.main.ui.fragments.DashboardMapFragment" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

Note : Also check your GOOGLE_API_KEY

関連する問題