没有布局Android的Google Map片段的基本方法Java [英] Base methods Java for a Google Map fragment without a layout Android

查看:68
本文介绍了没有布局Android的Google Map片段的基本方法Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,我很头疼.首先,我为我的App创建了所有布局,然后开始制作方法.

I'm new at Android and i'm having quite a headache. First i created all the layouts for my App and now i started to make the methods.

遵循Google指南 Hello Map ,问题在于这种方法是一种具有您自己的布局的活动,但是我正在使用的片段保存了地图,不是.

Following the Google Guideline Hello Map, the problem is that this method is an activity with your own layout, however the fragment that i'm using that holds the map, it's not.

当我不对XML片段使用类"时,它将显示地图.但是当我尝试并尝试使用您好地图时,它不会什么都不显示.

When i don't use a "class" to my fragment in XML, it shows the map. But when i do and try to use this method Hello Map, it doesn't show anything.

这是包含片段的xml文件

Here's the xml file containing the fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_menu"
tools:context=".MenuActivity">


<AutoCompleteTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/autoCompleteTextView_map"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Digite o Local"
    />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_below="@+id/autoCompleteTextView_map"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/autoCompleteTextView_map"
    android:layout_alignEnd="@+id/autoCompleteTextView_map"
    android:layout_alignParentBottom="true"
    class="com.example.RF.MyApp.MapFragment" />
 </RelativeLayout>

这是我试图使用的Java代码,这很不好哈哈

And here's the Java code that i was trying to use, it's very bad haha

public class MapFragment extends Fragment {

 View myView;
public MapFragment(){

    // Required empty public constructor
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    RelativeLayout relativeLayout = new RelativeLayout(getActivity());
    myView=relativeLayout;


    return myView;
}


public void onMapReady(GoogleMap map) {
    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

    map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));
}
}

如果有人有解决方案,请帮助我.谢谢!;)

If somebody has a solution, please help me. Thanks! ;)

推荐答案

我将使用FrameLayout并在Activity代码中动态添加Fragment,而不是使用静态Fragment.

Instead of using a static Fragment, I would use a FrameLayout and dynamically add the Fragment in the Activity code.

对于创建不需要布局xml的Map Fragment类,只需将Fragment扩展为SupportMapFragment.

As for creating a Map Fragment class that doesn't need a layout xml, just have the Fragment extend SupportMapFragment.

这是一个简单的例子:

public class MyMapFragment extends SupportMapFragment 
                       implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Marker marker;


    public MyMapFragment () {
    }

    @Override
    public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {

        if (mMap == null) {
            getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;
        setUpMap();
    }

    private void setUpMap() {

        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setMapToolbarEnabled(false);

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {

                //remove previously placed Marker
                if (marker != null) {
                    marker.remove();
                }

                //place marker where user just clicked
                marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

            }
        });

    }

}

这篇关于没有布局Android的Google Map片段的基本方法Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆