简体   繁体   English

如何在 android 中设置 map 切换按钮

[英]How to set up a map toggle button in android

I have a menu with a button that says 'Toggle Map'.我有一个带有“切换地图”按钮的菜单。 When this is clicked, I want the map type to toggle between standard map and satellite view.单击此按钮后,我希望 map 类型在标准 map 和卫星视图之间切换。 I don't know the code to do this or what I would have to do to achieve this.我不知道执行此操作的代码或要实现此目标我必须做什么。

Please help me code it and help me understand where to put the code and why.请帮我编写代码并帮助我了解将代码放在哪里以及为什么。 I have the following which somebody helped me write to start this off, but really don't know the code or actions to take from here.我有以下内容,有人帮我写了这篇文章,但真的不知道从这里开始的代码或操作。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapview :

default :

return super.onOptionsItemSelected(item);
}
}

Anybody?有人吗?

So, your "somebody" help you with the menu button, which is a start.因此,您的“某人”可以帮助您使用菜单按钮,这是一个开始。 When the user will press menu, and choose the menu button "map", with the id "mapview", it will call your code in the switch.当用户按下菜单,并选择菜单按钮“地图”,ID为“地图视图”,它将调用您在开关中的代码。 You just need to check if you're in the sattelite mode, and, if you are, stop it:您只需要检查您是否处于卫星模式,如果是,请停止它:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapview :
   if(mMapView.isSatellite()) {
      mMapView.setSatellite(false); 
   } else {
      mMapView.setSatellite(true); 
   }


default :

return super.onOptionsItemSelected(item);
}
}

But you need to get the MapView mMapView.但是你需要得到 MapView mMapView。 For that, in the onCreate of your Activity, just use findViewById(R;id.mapviewid);为此,在您的活动的 onCreate 中,只需使用 findViewById(R;id.mapviewid);

mapviewid is an example, but you will find it in the file you use in the onCreate(), where you do setContentView(R.layout.something). mapviewid 是一个示例,但您会在 onCreate() 中使用的文件中找到它,您可以在其中执行 setContentView(R.layout.something)。 Go to this file, and search the MapView, to get the id. Go 到这个文件,并搜索 MapView,得到 id。

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

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