当我从第3个标签中选择一个城市时,我希望它显示在“地图”标签中。例如,当我第一次选择“奥斯汀”作为城市时,它会显示在地图上(如下图所示)。但是当我再次返回“城市”标签并选择其他城市时,它不会在“地图”标签中更新。你有没有办法告诉我,每次点击“地图”选项卡,我都可以刷新“MapsActicity”。
public class HelloTabWidget extends TabActivity implements OnTabChangeListener {`
private TabHost mtabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
mtabHost = getTabHost();
mtabHost.setOnTabChangedListener(this);
intent = new Intent().setClass(this, BarActivity.class);
spec = tabHost.newTabSpec("Name").setIndicator("Name",res.getDrawable(R.drawable.ic_tab_bar)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MapsActivity.class);
spec = tabHost.newTabSpec("Map").setIndicator("Map",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CityActivity.class);
spec = tabHost.newTabSpec("city").setIndicator("City", res.getDrawable(R.drawable.ic_tab_city)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
public void onTabChanged(String tabId) {
int a = mtabHost.getCurrentTab();
String b = Integer.toString(a);
if(b.equals("1"))
{
Toast.makeText(getApplicationContext(), "Map Selected", Toast.LENGTH_LONG).show();
}
}
}
更新
`public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;`
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
String []coordinates = new String[4];
coordinates[0] = "33.477863"; coordinates[1] = "-101.855166"; //Just in-case user didn't choose a city
if(CityActivity.value.equals("Austin")) { //This 'value' is recived from CityActivity. Depending on what city user chooses.
coordinates[0] = "30.260053"; coordinates[1] = "-97.738593"; }
else if(CityActivity.value.equals("Dallas")) {
coordinates[0] = "33.554519"; coordinates[1] = "-101.855621"; }
else if(CityActivity.value.equals("Slaton")) {
coordinates[0] = "33.577863"; coordinates[1] = "-101.855166"; }
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc = mapView.getController();
mc.animateTo(p);
mc.setZoom(16);
mapView.invalidate();
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
return false;
} }
答案 0 :(得分:1)
String []coordinates = new String[4];
coordinates[0] = "33.477863"; coordinates[1] = "-101.855166"; //Just in-case user didn't choose a city
if(CityActivity.value.equals("Austin")) { //This 'value' is recived from CityActivity. Depending on what city user chooses.
coordinates[0] = "30.260053"; coordinates[1] = "-97.738593"; }
else if(CityActivity.value.equals("Dallas")) {
coordinates[0] = "33.554519"; coordinates[1] = "-101.855621"; }
else if(CityActivity.value.equals("Slaton")) {
coordinates[0] = "33.577863"; coordinates[1] = "-101.855166"; }
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc = mapView.getController();
mc.animateTo(p);
mc.setZoom(16);
mapView.invalidate();
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
尝试在OnResume()方法
中移动这段代码答案 1 :(得分:0)
请您告诉我,每次点击“地图”标签时,我是否可以刷新“MapsActicity”。
首先将注意力转移到新的城市
mapController.animateTo(new GeoPoint( (int) (lat * 1E6),
(int) (lng * 1E6))
或
mapView.invalidate()
/ mapView.postInvalidate()
来刷新您的地图。