在 SupportFragment 中向 Google 地图添加标记

Add Marker to Google Map in SupportFragment

我为 tabs 做了一个 tabbed-activity。在第四个 tab 中,我做了一个 map,我添加了一个 marker,但是 marker 不可见,我不明白为什么。我为我的问题尝试了很多解决方案,但现在我遇到了这种情况。

这是我的 Java 代码:

public class tab_mappa extends SupportMapFragment implements OnMapReadyCallback {

    private GoogleMap googleMap;
    SupportMapFragment mSupportMapFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);
        View root = inflater.inflate(R.layout.tabmappa, null, false);
        mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        if (mSupportMapFragment == null) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mSupportMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.map, mSupportMapFragment).commit();
        }

        return root;
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        this.googleMap = googleMap;
        this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        this.googleMap.getUiSettings().setZoomControlsEnabled(true);
        this.googleMap.getUiSettings().setCompassEnabled(true);
        this.googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        this.googleMap.getUiSettings().setZoomGesturesEnabled(true);
        this.googleMap.getUiSettings().setRotateGesturesEnabled(true);

        LatLng pos1 = new LatLng(44.783878,10.879663);
        final LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(pos1);

        this.googleMap.addMarker(new MarkerOptions().position(pos1));
        this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
            }
        });

    }

    @Override
    public void onPause() {
        super.onPause();
    }

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

    private void setUpMapIfNeeded() {
        if (googleMap == null) {
            getMapAsync(this);
        }
    }
    }

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
tools:context="esame.progetto.xhondar.github.com.info.tab_mappa">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.03"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map" />

</LinearLayout>

这是我的最终结果:

你有什么想法吗?

在没有生成器的情况下使用标记怎么样?您在 onMapReady()

中编写了代码
LatLng pos1 = new LatLng(44.783878,10.879663);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pos1);

this.googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
     @Override
     public void onMapLoaded() {
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
     }
});

变为

LatLng pos1 = new LatLng(44.783878,10.879663);

googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
     @Override
     public void onMapLoaded() {
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
     }
});

这个。我在 googleMap 之前删除了构建器和 this

我解决了问题,这是给需要的人的解决方案:

Java代码:

import android.support.v4.app.Fragment;
public class tab_mappa extends Fragment implements OnMapReadyCallback {
        GoogleMap map;
        SupportMapFragment mapFragment;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.tabmappa, container, false);
            SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

            return v;
        }

        @Override
        public void onMapReady(GoogleMap googleMap){
            map = googleMap;
            this.map = googleMap;
            this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            this.map.getUiSettings().setZoomControlsEnabled(true);
            this.map.getUiSettings().setCompassEnabled(true);
            this.map.getUiSettings().setMyLocationButtonEnabled(true);
            this.map.getUiSettings().setZoomGesturesEnabled(true);
            this.map.getUiSettings().setRotateGesturesEnabled(true);

            LatLng pp = new LatLng(44.783878,10.879663);
            map.addMarker(new MarkerOptions().position(pp).title("Carpi"));
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(pp, 8));
        }
    }

Xml代码:

<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="esame.progetto.xhondar.github.com.info.tab"/>

错误位于 xml 代码中:是 Fragment 而不是 FrameLayout,这导致了代码内的冲突。