我目前正在为我的武术组织制作应用程序。我想要一个使用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) {
}
});
}
}
以下是我的主要活动,它实例化导航抽屉并在不同标签之间切换,包括“位置”标签。我包含了所有代码,因为我不确定是否还需要在主要活动中更改某些内容。用于在片段之间切换的代码位于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;
}
}
答案 0 :(得分:0)
第1步:LocationFragment
实施OnMapReadyCallback
。
步骤2:将您当前的所有onViewCreated()
逻辑移至onMapReady()
(OnMapReadyCallback
所需)
第3步:在onViewCreated()
中,拨打getMapAsync(this)