Ivan
Ivan

Reputation: 1526

Bind location to MapView in MVVM

Just wonder how can I bind a latitude and longitude to com.google.android.gms.maps.MapView. Feels like it doesn't expose any layout attributes for that.

Upvotes: 4

Views: 2315

Answers (1)

Ivan
Ivan

Reputation: 1526

Found answer myself.

Just need to create static method annotated with @BindingAdapter. Something like:

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

And use attribute defined above in layout:

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

Upvotes: 3

Related Questions