我目前正在为我的武术组织开发一个应用程序。我想要一个使用SupportMapFragment的片段,该片段显示我州内每个武术道场的不同位置。我目前已经设置了一个导航抽屉,带有一个名为“位置”的选项卡。我将跟着Tutsplus的家伙如何创建他们的Zoo应用程序并在该应用程序中包含地图的方式进行跟踪。我正在查看的代码位于here.中,他们在Youtube上的代码和教程中使用getMap().animateCamera...
来控制摄像机并使地图正常工作。我已经知道getMap()已从Android中删除,并且getMapAsync(this)
是一个更好的选择,但是我正在努力弄清楚如何将getMapAsync()
运用于我的代码中以及当前的代码设置方式,以及我可以帮忙。我浏览了所有Google文档,并通读了该站点上的多个线程,但是运气不高。这是我的LocationFragment.java
下面的代码:
public class LocationFragment extends SupportMapFragment {
public static LocationFragment getInstance() {
LocationFragment fragment = new LocationFragment();
return fragment;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
CameraPosition position = CameraPosition.builder()
.target(new LatLng(42.921966, -85.718533))
.zoom( 16f )
.bearing( 0.0f )
.tilt( 0.0f )
.build();
getMap().animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
getMap().setMapType(GoogleMap.MAP_TYPE_HYBRID);
getMap().setTrafficEnabled(true);
getMap().getUiSettings().setZoomControlsEnabled(true);
MarkerOptions options = new MarkerOptions().position(new LatLng(42.921966, -85.718533));
options.title("KFCOM");
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
getMap().addMarker(options);
getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return true;
}
});
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint("https://gist.githubusercontent.com/anonymous/101ffbf7e1aed60b7caf7d3d5418bfde/raw/43b5d1fa6862fd1dce84044821cdf1a9d48b6ca2")
.build();
PinsApiInterface pinsApiInterface = adapter.create(PinsApiInterface.class );
pinsApiInterface.getStreams(new Callback<List<Pin>>() {
@Override
public void onResponse(Call<List<Pin>> pins, Response<List<Pin>> response) {
for( Pin pin : response.body()) {
MarkerOptions options = new MarkerOptions().position( new LatLng(pin.getLatitude(), pin.getLongitude()));
options.title(pin.getName());
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
getMap().addMarker(options);
}
}
@Override
public void onFailure(Call<List<Pin>> call, Throwable t) {
}
});
}
}
下面是我的主要活动,它实例化导航抽屉并在不同的选项卡之间切换,包括“位置”选项卡。我包含了所有代码,因为我不确定在Main Activity中是否也需要更改某些内容。片段之间切换的代码位于
onDrawerSectionItemClickedEvent
的代码的最底部。public class Home extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private String mCurrentFragmentTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById( R.id.drawer_layout );
mActionBarDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.drawer_opened,
R.string.drawer_closed )
{
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(getSupportActionBar() != null)
getSupportActionBar().setTitle(R.string.drawer_opened);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if(getSupportActionBar() != null)
getSupportActionBar().setTitle(R.string.drawer_closed);
}
};
mDrawerLayout.addDrawerListener( mActionBarDrawerToggle );
displayInitialFragment();
}
private void displayInitialFragment() {
getSupportFragmentManager().beginTransaction().replace(R.id.container, HomeFragment.getInstance() ).commit();
mCurrentFragmentTitle = getString(R.string.Home);
}
@Override
public void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mActionBarDrawerToggle.onOptionsItemSelected( item ) )
return true;
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
EventBus.getInstance().register( this );
}
@Override
protected void onStop() {
EventBus.getInstance().unregister( this );
super.onStop();
}
@Subscribe
public void onDrawerSectionItemClickedEvent(DrawerSectionItemClickedEvent event) {
mDrawerLayout.closeDrawers();
if(event == null || TextUtils.isEmpty( event.section ) || event.section.equalsIgnoreCase(mCurrentFragmentTitle))
{
return;
}
Toast.makeText( this, "Home: Section Clicked: " + event.section, Toast.LENGTH_SHORT).show();
if(event.section.equalsIgnoreCase( "locations" )){
getSupportFragmentManager().beginTransaction().replace(R.id.container, LocationFragment.getInstance()).commit();
} else if (event.section.equalsIgnoreCase( "current members" ))
{
getSupportFragmentManager().beginTransaction().replace(R.id.container, MembersFragment.getInstance()).commit();
} else if (event.section.equalsIgnoreCase( "home" ))
{
getSupportFragmentManager().beginTransaction().replace(R.id.container, HomeFragment.getInstance()).commit();
} else {
return;
}
mCurrentFragmentTitle = event.section;
}
}
最佳答案
步骤#1:让LocationFragment
实现OnMapReadyCallback
。
步骤2:将当前所有onViewCreated()
逻辑移至onMapReady()
(OnMapReadyCallback
要求)
步骤#3:在onViewCreated()
中,调用getMapAsync(this)