現在、NavigationBarでMapのタブを押すとGoogleMapが表示されるアプリを作ろうとしてます。
しかし、Fragmentをいじったりとしてみたのですが表示されません。どうすればNavigationBarからMapが表示されるようになるでしょうか。
もしかして、GoogleMapはアプリとしてそれ単体でしか機能を持たせることは出来ないのでしょうか。
code>
1import android.os.Bundle; 2import android.support.annotation.NonNull; 3import android.support.design.widget.BottomNavigationView; 4import android.support.v7.app.AppCompatActivity; 5import android.view.MenuItem; 6 7public class MainActivity extends AppCompatActivity { 8 9 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 10 = new BottomNavigationView.OnNavigationItemSelectedListener() { 11 12 @Override 13 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 14 switch (item.getItemId()) { 15 case R.id.english: 16 getSupportFragmentManager() 17 .beginTransaction() 18 .replace(R.id.fragment_container, new HomeFragment()) 19 .commit(); 20 return true; 21 case R.id.search: 22 getSupportFragmentManager() 23 .beginTransaction() 24 .replace(R.id.fragment_container, new SearchFragment()) 25 .commit(); 26 return true; 27 case R.id.map: 28 getSupportFragmentManager() 29 .beginTransaction() 30 .replace(R.id.fragment_container, new MapDisplay()) 31 .commit(); 32 return true; 33 case R.id.favorite: 34 getSupportFragmentManager() 35 .beginTransaction() 36 .replace(R.id.fragment_container, new HomeFragment()) 37 .commit(); 38 return true; 39 } 40 return false; 41 } 42 }; 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 getSupportFragmentManager() 50 .beginTransaction() 51 .replace(R.id.fragment_container, new HomeFragment()) 52 .commit(); 53 BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 54 navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 55 } 56} 57 58```java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.support.v4.app.Fragment;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private android.support.v4.app.FragmentActivity aaa; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_map); SupportMapFragment mapFragment = (SupportMapFragment) aaa.getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup contain, @Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); AppCompatActivity appCompatActivity = new AppCompatActivity(); SupportMapFragment mapFragment = (SupportMapFragment) appCompatActivity.getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); return inflater.inflate(R.layout.fragment_map, null); } @Override public void onMapReady(GoogleMap googleMap) { LatLng sydney = new LatLng(-33.852, 151.211); googleMap.addMarker(new MarkerOptions().position(sydney) .title("Marker in Sydney")); googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); }
}
あなたの回答
tips
プレビュー