开发者

Android Map View Toggle

I'm trying to toggle my map view from the standard map view to satellite. I've been given the following code by somebody on this site. But 'mMapView' is underlined in red as an error and carrying out the suggested solutions doesn't work.

Please explain to me what I'm doing wrong and what I can do right to fix this. Thanks.

@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);
}
}

My onCreate is:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomCo开发者_开发知识库ntrols(true);
mapView.setSatellite(false);
}


Create a field with the mapView and use it in the "OnOptionItemSelected". Something like this:

private MapView mapView;
...
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.map);
  mapView = (MapView) findViewById(R.id.mapview);
  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(false);
}
...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
//this should be the id for the item in the menu, no for the map.
//if you select the item in the menu that toggle the view of your map, then, change the map
//I copied your code, but becareful because I think this can give you problems (about 
//having the same ids for the map and for the item in the same spacename ("id")
              case R.id.mapview : 
                if(mapView.isSatellite()) {
                   mapView.setSatellite(false); 
                } else {
                   mapView.setSatellite(true); 
                }
              default :
               return super.onOptionsItemSelected(item);
              }
            }


@Finuka - This will not help the problem. It will instead cause them to have error in:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.map);
  mapView = (MapView) findViewById(R.id.mapview);
  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(false);
}

It should be this, which they originally used:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);
}

I tried this in my map app. (I haven't added a toggle feature as I don't need one)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜