简体   繁体   English

Android Google地图和菜单

[英]Android Google Maps and Menu

I Have this bit of code and what happens when i run the app is that i get the map and when i click on the menu and i select the map option this happens again. 我有这段代码,当我运行该应用程序时,会得到地图,而当我单击菜单并选择地图选项时,会再次发生这种情况。 Is there any way so I could run the app and I would click on the map option then I would get the map ? 有什么办法可以运行该应用程序,然后单击map选项,然后获取地图? At the moment the map is still displayed and when i click on the map option it just keeps happening. 此刻地图仍然显示,当我单击地图选项时,它一直在发生。 I need a way so that the map is not visible when the application starts and make it visible when i hit the map option on my menu. 我需要一种方法,使该地图在应用程序启动时不可见,并在我点击菜单上的map选项时使其可见。 Anyone could give me some advice.Please help me out thank you. 任何人都可以给我一些建议。请帮助我,谢谢。

public class MainActivity extends Activity {
GoogleMap gMap;
private final LatLng LOCATION_AMERICA=new LatLng(someNo , someNo);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    gMap.addMarker(new MarkerOptions().position(LOCATION_AMERICA).title("AMERICA"));


}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
    case R.id.map:
        // write logic here for open map
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
        }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;

}

remove this code from onCreate and put in onOptionsItemSelected onCreate删除此代码,并放入onOptionsItemSelected

 gMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    gMap.addMarker(new MarkerOptions().position(LOCATION_AMERICA).title("AMERICA"));

also, if the item is clicked again, check if the fragment is already loaded, then don't load it again. 另外,如果再次单击该项目,请检查片段是否已加载,然后不要再次加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM