编程语言
首页 > 编程语言> > java-在片段中未调用onMapReady

java-在片段中未调用onMapReady

作者:互联网

我正在建立一个与谷歌地图有关的应用程序.在这种情况下,我试图在片段中实现地图.我在OnMapReady方法中操作地图.但是应用程序未调用OnMapReady方法.我在活动中实现了同一件事,并且完美地完成了工作.但是它不是零碎的.我不知道它是否存在片段生命周期的问题,并且我缺少某些东西.
这是代码.

Maps_Fragment.java

    package com.example.mudasir.login;


import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class Maps_Fragment extends Fragment implements OnMapReadyCallback {

    public GoogleMap mMap;
    String[] showroom_addresses = new String[5];
    List<Address> addressList = null;
    LatLng latlng;
    LatLngBounds.Builder builder  = new LatLngBounds.Builder();
    View view1;

    public Maps_Fragment() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_maps_, container, false);
    }
    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {

        showroom_addresses[0] = "Dubai";
        showroom_addresses[1] = "Fujairah";
        showroom_addresses[2] = "Al Ain";
        showroom_addresses[3] = "Sharjah";
        showroom_addresses[4] = "Ras Al Khaimah";
        Geocoder geocoder = new Geocoder(getActivity());
        for(int i =0; i<5; i++) {
            try {
                addressList = geocoder.getFromLocationName(showroom_addresses[i], 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            android.location.Address address = addressList.get(0);
            latlng = new LatLng(address.getLatitude(), address.getLongitude());
            Marker marker = googleMap.addMarker(new MarkerOptions().position(latlng).title(showroom_addresses[i]));
            builder.include(marker.getPosition());
        }

        LatLngBounds bounds = builder.build();
        int padding = 0;
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding);
        //CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new MarkerOptions().position(latlng).title(showroom_addresses[3]).getPosition(),7F);
        googleMap.animateCamera(cu);
    }

}

Maps_Fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mudasir.login.Maps_Fragment">

    <!-- TODO: Update blank fragment layout -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout">


        <fragment android:id="@+id/maps"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_alignParentTop="true">
            </fragment>

在活动的情况下,相同的方法和要求可以很好地工作,但在出现碎片的情况下,这是错误的.请帮忙.

解决方法:

您必须从

SupportMapFragment

代替

Fragment

[编辑]

我的建议是有两个片段:一个片段,当您没有任何结果时,另一个片段与SupportFragmentManager.

此外,在应该从SupportFragmentManager扩展的Maps_Fragment中,您必须删除onCreateView和onViewCreated的替代,否则您应该管理所有SupportMapFragment,但我认为您不希望这样做.

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_maps_, container, false);
    }
    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps);
        mapFragment.getMapAsync(this);
    }]

要实现OnMapReadyCallback,我将在onActivityCreated方法中实现:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getMapAsync(this);
}

标签:android-fragments,google-maps,fragment-lifecycle,java,android
来源: https://codeday.me/bug/20191026/1938386.html