Android 如何使用带有单例方法的MapView使用GoogleMap?
我正在尝试使用MapView和singleton方法 在这里,我创建了一次MapView,并只执行了一次mapViewSingletonInstance.onCreate(savedInstance)。在此之后,我尝试使用它来执行anotherMapViewObject.addView(mapViewSingletonInstance) 但这只适用于一次。在使用anotherMapView添加到一个mapView后,它将不起作用Android 如何使用带有单例方法的MapView使用GoogleMap?,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我正在尝试使用MapView和singleton方法 在这里,我创建了一次MapView,并只执行了一次mapViewSingletonInstance.onCreate(savedInstance)。在此之后,我尝试使用它来执行anotherMapViewObject.addView(mapViewSingletonInstance) 但这只适用于一次。在使用anotherMapView添加到一个mapView后,它将不起作用 public class GoogleMapSingleton{
public class GoogleMapSingleton{
private static GoogleMapSingleton googleMapSingleton;
private static MapView singletonMapView;
private GoogleMapSingleton()
{
}
public static GoogleMapSingleton GoogleMapSingletonInstance(Bundle savedInstanceState, Context context)
{
if(googleMapSingleton == null){
singletonMapView = new MapView(context);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
singletonMapView.setLayoutParams(lp);
singletonMapView.onCreate(savedInstanceState);
singletonMapView.onResume();
singletonMapView.getMap();
googleMapSingleton = new GoogleMapSingleton();
}
return googleMapSingleton;
}
/**
* @return the singletonMapView
*/
public MapView getSingletonMapView() {
return singletonMapView;
}
}
也许您应该将null检查后的前6行移到构造函数中,只保留新的GoogleMapSingle()。这样你就有了一个更干净的getInstance方法。@Thupten我将这6行保留在空检查中,这样它只会在创建单例对象时发生一次,然后可以在任何地方使用(这样做的原因是每次调用mapView.onCreate(savedInstance)方法时google收费)。如果我把它放在外面,google会一次又一次地对mapView加载收费。也许你应该把空检查后的前6行移到构造函数中,只留下新的GoogleMapSingle()。这样你就有了一个更干净的getInstance方法。@Thupten我将这6行保留在空检查中,这样它只会在创建单例对象时发生一次,然后可以在任何地方使用(这样做的原因是每次调用mapView.onCreate(savedInstance)方法时google收费)。如果我把它放在外面,google会一次又一次地对mapView加载收费。也许你应该把空检查后的前6行移到构造函数中,只留下新的GoogleMapSingle()。这样你就有了一个更干净的getInstance方法。@Thupten我将这6行保留在空检查中,这样它只会在创建单例对象时发生一次,然后可以在任何地方使用(这样做的原因是每次调用mapView.onCreate(savedInstance)方法时google收费)。如果我把地图放在外面,谷歌会一次又一次地收取地图视图的费用。