GoogleMap设置(addMarker,setMapType& CameraUpdate)在SupportMapFragment中不起作用

时间:2014-01-16 21:18:31

标签: android google-maps android-fragments supportmapfragment

我的地图按照预期开始,但标记,mapType和缩放的设置从不适用于我的SupportMapFragment。当我在另一个项目中启动相同的代码时,在MainActivity中,一切正常。如何在SupportMapFragment中使其工作?

MainActivity(工作):

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
    private final LatLng BUTIKPLATS = new LatLng(57.873873, 11.974995);

    private GoogleMap karta;

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

        karta  = ((MapFragment) getFragmentManager().findFragmentById(R.id.karta)).getMap();

        karta.addMarker(new MarkerOptions().position(BUTIKPLATS).title("Vita Fläckens Blommor"));
        karta.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(BUTIKPLATS, 17);
        karta.animateCamera(update);
    }


}

SupportMapFragment(不工作):

import android.os.Bundle;
import android.view.View;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Karta extends SupportMapFragment{



    public static Karta newInstance() {
        Karta f = new Karta();
        return f;
    }


private GoogleMap karta;
private final LatLng BUTIKPLATS = new LatLng(57.873873, 11.974995);

public void onCreate(View v, Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
System.out.println("onViewCreated1");

        karta  = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.kontaktVisaFragment)).getMap();
System.out.println("Karta initierad (.getMap)");        
        if (karta !=null){
System.out.println("Karta != null");            
            karta.addMarker(new MarkerOptions().position(BUTIKPLATS).title("Vita Fläckens Blommor"));
            karta.setMapType(GoogleMap.MAP_TYPE_HYBRID);

            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(BUTIKPLATS, 17);
            karta.animateCamera(update);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

<{1}} onCreate()的{​​{1}}过早地试图操纵SupportMapFragment,因为它还不存在。当GoogleMap对象存在时,您需要等待稍后的时间点,或者将代码保留在活动中的位置。

来自your previous question

GoogleMap应该是比onViewCreated()更好的选择。但是,我的回答让事情变得太复杂了。替换:

onCreate()

使用:

karta  = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.kontaktVisaFragment)).getMap();