【问题标题】:Bind location to MapView in MVVM在 MVVM 中将位置绑定到 MapView
【发布时间】:2016-04-27 15:37:04
【问题描述】:

只是想知道如何将纬度和经度绑定到com.google.android.gms.maps.MapView。感觉它没有为此公开任何布局属性。

【问题讨论】:

    标签: android mvvm


    【解决方案1】:

    自己找到答案了。

    只需要创建带有@BindingAdapter 注释的静态方法。 比如:

    @BindingAdapter("app:latLong")
    public static void bindLocationToMap(MapView mapView, LatLng latLong) {
       CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLong, 10);
       mapView.animateCamera(cameraUpdate);
    }
    

    并在布局中使用上面定义的属性:

    ...
    <com.google.android.gms.maps.MapView android:id="@+id/mapview"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            app:location="@{latLong} />
    ...
    

    【讨论】:

    • 但是com.google.android.gms.maps.MapView 是使用地图的正确方式吗?我的意思是,每个文档都告诉您使用片段,我想知道这是否是一种已弃用的方式。
    • 我也很好奇 jturolla 的问题。但是我知道您可以以与新方式相同的方式使用@BindingAdapter?我刚刚开始使用 mvvm,所以我有点困惑谁应该处理地图更新。
    • 据我所知,使用 MapView 本身并没有被禁止(MapFragment 自己做的),但您只需要注意将生命周期事件传递给 MapView
    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2013-03-24
    • 2011-02-20
    • 2014-05-04
    • 2016-07-06
    • 2018-02-07
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多