【问题标题】:Markers are not showing on the map fragment标记未显示在地图片段上
【发布时间】:2021-05-31 12:52:42
【问题描述】:

这是我第一次使用 android studio,我创建了一个导航栏,我在其中使用了一个包含 3 个片段的主机片段,其中一个是地图片段,地图正在显示,但我无法获得标记,我不知道我是否弄乱了地图片段,但我将不胜感激任何帮助或提示。 谢谢。

MapFragment.java

package com.example.sanad;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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

public class MapsFragment extends Fragment {

    private OnMapReadyCallback callback = new OnMapReadyCallback() {

        /**
         * 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) {

            LatLng sydney = new LatLng(-50, 50);
            googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        }

    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                            @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_maps, container, false);





    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SupportMapFragment mapFragment =
                (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        if (mapFragment != null) {
            mapFragment.getMapAsync(callback);
        }
    }
}

fragment_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map_view"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsFragment"
    tools:ignore="DuplicateIds">
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    tools:context=".MapsActivity">


</fragment>
  
</RelativeLayout>

【问题讨论】:

  • 你确定 Latlang 是正确的吗?
  • 这不是悉尼的坐标,但我不知道那个位置,而且相机没有移动,所以 MapReady 上的洞没有被调用。
  • 尝试将 onViewCreated 内容移动到 onCreateView
  • 同样的问题 :(

标签: java android xml android-fragments


【解决方案1】:

代码看起来没问题,您可以尝试设置一些调试点来查看代码被调用的部分,尽管我确实注意到一件事,您可以将moveCamera 方法调用更新为animateCamera动画移动,你可能知道它是否工作的区别。我也更新了正确的坐标。

 LatLng sydney = new LatLng(-33.852, 151.211);
    googleMap.addMarker(new MarkerOptions()
        .position(sydney)
        .title("Marker in Sydney"));
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(sydney));

【讨论】:

  • 不幸的是,标记和动画相机无法正常工作:(。我得到了地图,但是这两个没有。无论如何,谢谢。
  • @SyyKee 你能调试到代码被调用的点吗?也许整个mapReady 回调不起作用?
猜你喜欢
  • 2018-04-28
  • 2017-01-09
  • 2015-11-27
  • 2018-03-30
  • 1970-01-01
  • 2021-07-07
  • 2015-01-25
  • 1970-01-01
相关资源
最近更新 更多