在另一个Activity中访问一个Activity的GoogleMap对象

时间:2017-06-26 19:56:31

标签: android google-maps android-fragments google-maps-android-api-2

我使用谷歌地图活动扩展我的应用程序。它创建了一个mapclass并扩展了FragmentActivity实现的OnMapReadyCallback。

onMapReadyCallBack返回GoogleMap对象,如下面的代码。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    public GoogleMap mMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

我想在我的MainActivity类上访问mMap来执行某些操作。我创建了一个具有onclick属性的按钮到gotolocation方法。但它返回错误,因为map对象为null;这是在我的活动类中访问GoogleMap对象的正确方法。

public void gotoLocation(View v){
    if (mMap != null){
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Kabul"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }else{
        Log.d("Map object", "It si null==============");
    }
}

2 个答案:

答案 0 :(得分:1)

请注意,一次只能向用户显示一个活动,因此无需从MainActivity访问mMap变量。

假设您想在单击gotoLocation()按钮时打开MapsActivity,您可以将与位置关联的数据从MainActivity传递到MapsActivity。

首先,在MainActivity中准备LatLng和描述,以便在单击按钮时将其发送到MapsActivity:

public void gotoLocation(View v){
  LatLng goToLocation = new LatLng(34.5392354, 69.1378334);

  Bundle args = new Bundle();
  args.putParcelable("latLon", goToLocation);
  args.put("desc", "Marker in Kabul");

  Intent i = new Intent(this, MapsActivity.class);
  i.putExtras(args);
  startActivity(i);
}

然后,当打开MapsActivity时,它将检索信息,并相应地修改标记:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    public GoogleMap mMap;
    LatLng mLatLng; 
    String mDescription;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);

        //Get the data sent from MainActivity
        Intent intent = getIntent();
        mLatLng = intent.getParcelableExtra("latLon");
        mDescription = intent.getStringExtra("desc");

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


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

        //Use the data sent from MainActivity:
        if (mLatLng != null) {
          // Add a marker for location/description sent from MainActivity
          mMap.addMarker(new MarkerOptions().position(mLatLng).title(mDescription));
          mMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
        }
    }
}

答案 1 :(得分:0)

除了第一个答案之外,mMap对象为空,因为它尚未实例化,并且尚未调用onMapReady回调。

查看OnMapReadyCallback界面https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback

的文档