【问题标题】:How to inflate android mapbox?如何给android mapbox充气?
【发布时间】:2020-01-10 13:20:46
【问题描述】:

我无法为 mapbox 充气。我尝试将 getInstance 放在 MainActivity 上,片段在最左下角加载了 mapbox 徽标,但地图为空。所以我尝试将 getInstance 放在片段上,然后我得到了错误。

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        // Mapbox access token is configured here. This needs to be called either in your application
        // object or in the same activity which contains the mapview.
        Mapbox.getInstance(this, getString(R.string.access_token)); // Error in context:this
        mapView = view.findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);
        return view;
    }
   @Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
    this.mapboxMap = mapboxMap;
    mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {

        }
    });
}
}

两个底部导航项都在工作

【问题讨论】:

    标签: android android-layout android-fragments mapbox mapbox-android


    【解决方案1】:

    您忘记提及错误,但我认为您的代码可以正常工作。

    您尚未定义地图应居中的位置,因此它可能会回落到 0/0 的纬度/经度。这是正确的in the middle of the atlantic ocean。所以你在地图上看到的蓝色只是水:)

    尝试移动地图并缩小,您很快就会看到非洲!

    编辑

    我又读了一遍。你在说这个吗?

    Mapbox.getInstance(this, getString(R.string.access_token)); // Error in context:this
    

    好吧,如果这段代码在片段中执行,您可能需要getActivity() 而不仅仅是this

    【讨论】:

    • 是的,我忘记在片段 xml 和缩放级别中设置默认纬度、经度。所以基本上它适用于 MainActivity 上的访问令牌。但我更喜欢你的第二个答案,所以我可以把 mapbox 的东西放在一起。非常感谢人
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2021-06-16
    相关资源
    最近更新 更多