Android GoogleMaps - 从基础片段活动抽象类

时间:2017-05-13 16:33:49

标签: android google-maps android-fragments

我正在开展一个项目,我正在两个地图活动之间切换。

我正在尝试的结构是这样的:

BaseMapsActivity extends FragmentActivity

MapsActivity1 extends BaseMapsActivity

MapsActivity2 extends BaseMapsActivity

为了防止代码重复。我想将我的Google API客户端代码放在基本Activity中,然后在mapReady()基础上添加一些地图样式代码,然后只需对每个子活动进行更改(添加标记等)。

我的问题是,如何在基类中使用样式和位置逻辑准备这个mapFragment,然后用子活动中的片段来扩充框架布局来修改它?这个过程的步骤如何发挥作用?这甚至可能吗?

public abstract class BaseMapsActivity extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
Location mLastLocation;
Marker mCurrLocationMarker;

public static final String TAG = "BaseMapsActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_base_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.base_map_activity);
    mapFragment.getMapAsync(this);


}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    try {
        // Customise the styling of the base map using a JSON object defined
        // in a raw resource file.
        boolean success = googleMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        this, R.raw.style_json));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Can't find style. Error: ", e);
    }

    mMap.moveCamera(CameraUpdateFactory.zoomTo(14));
    //Initialize Google Play Services
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }
    } else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }
}

和子活动1 ......

public class MapsActivity1 extends BaseMapsActivity implements LocationListener, View.OnClickListener {
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

private GoogleMap mMap;
private FirebaseAuth firebaseAuth;
private FirebaseUser user;
private DatabaseReference databaseRef;


private static final String TAG = "MapsActivity1";


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    super.onMapReady(mMap);

和第二项活动......

public class MapsActivity2 extends BaseMapsActivity implements OnMapReadyCallback,
    View.OnClickListener, OnProgressListener {


//    GoogleMap mMap;
private static GoogleMap mMap;




SeekBar colorSeek;


private double longitude;
private double latitude;
private Bundle mapSpots;
private Button backToMap;
private SeekBar.OnSeekBarChangeListener seekBarListener;

private LatLng user;


private static final String TAG = "MapsActivity2";
private static final int HUE_MAX = 360;


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    super.onMapReady(mMap);

谢谢T

1 个答案:

答案 0 :(得分:1)

需要调用super.onMapReady(Bundle savedInstanceState)而不是在子类的onMapReady覆盖中,所以代替:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    super.onMapReady(mMap);

需要这样:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    super.onMapReady(mMap);

@Override
protected void onMapReady(GoogleMap googleMap){
    mMap = googleMap;
    super.onMapReady(mMap);