我正在使用Google Maps Android API v2。
起初,只是为了测试Google示例中的这里,我设法使典型的普通地图显示出来。
public class POnlineMapView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.online_map_activity);
}
}
上面的代码运行完美,这表明一切都已经正确设置。
我现在正在尝试使用MapView类来操纵显示设置,例如中心点,但每次我尝试获取GoogleMap对象时,似乎都会得到一个空对象。为什么会这样?
public class POnlineMapView extends Activity {
private MapView myMapView;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myMapView = new MapView(getApplicationContext());
Bundle b = getIntent().getExtras();
double longitude = b.getDouble("longitude");
double latitude = b.getDouble("latitude");
setContentView(R.layout.online_map_activity);
map = myMapView.getMap();
CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(17);
map.moveCamera(center); //this gives a NullPointerException, probably due to the myMapView.getMap() method?
map.animateCamera(zoom);
}
}