首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在DialogFragment中显示MapView或GoogleMap?

如何在DialogFragment中显示MapView或GoogleMap?
EN

Stack Overflow用户
提问于 2017-03-23 07:45:17
回答 3查看 4.4K关注 0票数 1

我有一个创建为dialogFragment的对话框,它有自己的XML布局。是否可以在DialogFragment中嵌入一个带有引用的地图,这样我就可以更新它的位置?

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.andrew.neighborlabour.CreateJobDialog">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvTitle"
            android:text="Create Job"
            android:textAppearance="@color/colorPrimaryDark"
            android:textSize="24dp"
            android:padding="20dp"
            android:lines="2"
            android:layout_gravity="left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>

    </LinearLayout>

    <ScrollView android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="400dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Address"
                android:id="@+id/etAddress"/>

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

        </LinearLayout>

    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Create Job"
        android:id="@+id/btCreate"
        android:onClick="Create"/>

</LinearLayout>

在XML中,我可以看到映射,但是我不确定如何从我的java代码中与之交互。

dialogFragment:

代码语言:javascript
复制
public class CreateJobDialog extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_create_job, container, false);
        return view;
    }
}

创建对话框的步骤:

代码语言:javascript
复制
CreateJobDialog createJobDialog = new CreateJobDialog();
                createJobDialog.show(this.getFragmentManager(), "NoticeDialogFragment");
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-23 08:30:11

这是非常标准的,只需使用getMapAsync()方法来获取对Google Map的引用。

唯一令人惊讶的是,我不得不在DialogFragment代码中使用getFragmentManager()而不是getChildFragmentManager()。通常,当在片段中嵌套SupportMapFragment时,需要使用子FragmentManager,但显然对于DialogFragment是不同的。

下面是DialogFragment代码:

代码语言:javascript
复制
import android.support.v4.app.DialogFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapDialogFragment extends DialogFragment
        implements OnMapReadyCallback{

    GoogleMap mMap;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_map_dialog, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return rootView;
    }

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

        LatLng latLng = new LatLng(37.7688472,-122.4130859);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mMap.addMarker(markerOptions);
    }
}

启动DialogFragment:

代码语言:javascript
复制
new MapDialogFragment().show(getSupportFragmentManager(), null);

结果:

票数 7
EN

Stack Overflow用户

发布于 2018-05-29 07:04:04

我将在@Daniel Nugent的回答中添加onDestroyView()方法中以下代码的实现。

代码语言:javascript
复制
 @Override
    public void onDestroyView() {
        super.onDestroyView();
        assert getFragmentManager() != null;
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
        FragmentTransaction ft = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

我遵循了他回答的步骤,但标出了一个“致命的例外”。当opening> close>返回打开‘对话框片段’。

代码语言:javascript
复制
 Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
    Caused by: java.lang.IllegalArgumentException: Binary XML file line #44: Duplicate id 0x7f0900c3, tag null, or parent id 0x7f090067 with another fragment for com.google.android.gms.maps.SupportMapFragment.

注意:对于和我有相同问题的其他人来说,遵循这个答案可能会很有用。我不评论我的观察,因为我不遵守50个名誉点。

票数 6
EN

Stack Overflow用户

发布于 2019-11-11 07:45:53

好吧,我有几个问题是由于使用google api的multimap。因此,我认为完整的可重用对话框使用的完整答案应该是这样的(基于之前的答案)。

代码语言:javascript
复制
public class MapDialogFragment extends DialogFragment
        implements OnMapReadyCallback{

    GoogleMap mMap;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_map_edit_location, container, false);


        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentByTag("mapFragment");
        if (mapFragment == null) {
            mapFragment = new SupportMapFragment();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.mapFragmentContainer, mapFragment, "mapFragment");
            ft.commit();
            fm.executePendingTransactions();
        }
        mapFragment.getMapAsync(this);
    }

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

        LatLng latLng = new LatLng(37.7688472,-122.4130859);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mMap.addMarker(markerOptions);
    }


}

然后是带有google地图的xml文件(请注意,我们只使用父布局,以避免在另一个片段中膨胀)。

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapFragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

来自我的片段的调用。

代码语言:javascript
复制
new MapDialogFragment().show(getActivity().getSupportFragmentManager(), null);

希望它能帮助一些人搜索对话框,即使在multimap上下文中也是如此。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42964706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档