簡體   English   中英

android studio onMapReady沒有被調用

[英]android studio onMapReady not called

我想在我的一個視圖中集成地圖視圖。

我已經生成了一個新的地圖片段。 它以不同的視角出現,就像一個魅力。

然后我嘗試將代碼集成到正常活動中(一個帶有操作欄等等)。 它有點工作 - 在屏幕上出現很好,但onMapReady永遠不會在那個環境中被調用(換句話說,我會留在非洲,而不是去悉尼)。

已經好幾個小時......

這是xml代碼:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jp.artfoundui.MapsActivity" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/monument_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_menu_camera" />

這是java代碼:

public class MonumentActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_monument);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

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

    }
        /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @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));
    }
}
}

有什么建議 ?

我得到了答案 - 有點兒。

關於片段活動的整個代碼永遠不會被調用。 我嘗試了幾種解決方案來激活這個片段,但都陷入了死胡同......

另一種解決方案是繼續使用主要活動(AppCompatActivity的子類)。 事實證明,AppCompatActivity可以直接實現OnMapReadyCallback。

所以我添加了“實現OnMapReadyCallback”,然后直接添加了“onMapReady”回調。 代碼如下所示:

public class MonumentActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_monument);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.monument_map);
    mapFragment.getMapAsync(this);
[...]
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng louvres = new LatLng(48.860294, 2.338629);
    mMap.addMarker(new MarkerOptions().position(louvres).title("Marker sur le Louvres"));
    // mMap.moveCamera(CameraUpdateFactory.newLatLng(louvres));
    // mMap.moveCamera(CameraUpdateFactory.zoomTo(18));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(louvres, 15));
}

工作良好。 我得到一個完整的活動(工具欄,菜單處於活動狀態,還有一個額外的浮動按鈕),底層地圖工作得很好......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM