Skip to content

Commit

Permalink
add getCamera()
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Sep 14, 2024
1 parent ad9099d commit 9136f39
Show file tree
Hide file tree
Showing 17 changed files with 865 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.josxha.maplibre

import LngLat
import MapCamera
import MapLibreFlutterApi
import MapLibreHostApi
import MapOptions
Expand Down Expand Up @@ -146,6 +147,14 @@ class MapLibreMapController(
callback(Result.success(LngLat(latLng.longitude, latLng.latitude)))
}

override fun getCamera(callback: (Result<MapCamera>) -> Unit) {
val position = mapLibreMap.cameraPosition
val target = mapLibreMap.cameraPosition.target!!
val center = LngLat(target.longitude, target.latitude)
val camera = MapCamera(center, position.zoom, position.tilt, position.bearing)
callback(Result.success(camera))
}

override fun addFillLayer(id: String, sourceId: String, callback: (Result<Unit>) -> Unit) {
mapLibreMap.style?.addLayer(FillLayer(id, sourceId))
callback(Result.success(Unit))
Expand Down
67 changes: 65 additions & 2 deletions android/src/main/kotlin/com/github/josxha/maplibre/Pigeon.g.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ data class MapOptions (
}

/**
* A longitude/latitude coordinate object
* A longitude/latitude coordinate object.
*
* Generated class from Pigeon that represents data sent in messages.
*/
Expand All @@ -121,7 +121,7 @@ data class LngLat (
}

/**
* A pixel location / location on the device screen
* A pixel location / location on the device screen.
*
* Generated class from Pigeon that represents data sent in messages.
*/
Expand All @@ -146,6 +146,37 @@ data class ScreenLocation (
)
}
}

/**
* The current position of the map camera.
*
* Generated class from Pigeon that represents data sent in messages.
*/
data class MapCamera (
val center: LngLat,
val zoom: Double,
val tilt: Double,
val bearing: Double
)
{
companion object {
fun fromList(pigeonVar_list: List<Any?>): MapCamera {
val center = pigeonVar_list[0] as LngLat
val zoom = pigeonVar_list[1] as Double
val tilt = pigeonVar_list[2] as Double
val bearing = pigeonVar_list[3] as Double
return MapCamera(center, zoom, tilt, bearing)
}
}
fun toList(): List<Any?> {
return listOf(
center,
zoom,
tilt,
bearing,
)
}
}
private open class PigeonPigeonCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
Expand All @@ -164,6 +195,11 @@ private open class PigeonPigeonCodec : StandardMessageCodec() {
ScreenLocation.fromList(it)
}
}
132.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
MapCamera.fromList(it)
}
}
else -> super.readValueOfType(type, buffer)
}
}
Expand All @@ -181,6 +217,10 @@ private open class PigeonPigeonCodec : StandardMessageCodec() {
stream.write(131)
writeValue(stream, value.toList())
}
is MapCamera -> {
stream.write(132)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
Expand All @@ -193,6 +233,11 @@ interface MapLibreHostApi {
fun jumpTo(center: LngLat?, zoom: Double?, bearing: Double?, pitch: Double?, callback: (Result<Unit>) -> Unit)
/** Animate the viewport of the map to a new location. */
fun flyTo(center: LngLat?, zoom: Double?, bearing: Double?, pitch: Double?, durationMs: Long, callback: (Result<Unit>) -> Unit)
/**
* Get the current camera position with the map center, zoom level, camera
* tilt and map rotation.
*/
fun getCamera(callback: (Result<MapCamera>) -> Unit)
/** Convert a coordinate to a location on the screen. */
fun toScreenLocation(lng: Double, lat: Double, callback: (Result<ScreenLocation>) -> Unit)
/** Convert a screen location to a coordinate. */
Expand Down Expand Up @@ -258,6 +303,24 @@ interface MapLibreHostApi {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.maplibre.MapLibreHostApi.getCamera$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
api.getCamera{ result: Result<MapCamera> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(wrapError(error))
} else {
val data = result.getOrNull()
reply.reply(wrapResult(data))
}
}
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.maplibre.MapLibreHostApi.toScreenLocation$separatedMessageChannelSuffix", codec)
if (api != null) {
Expand Down
26 changes: 26 additions & 0 deletions example/lib/controller_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ class _ControllerPageState extends State<ControllerPage> {
},
child: const Text('Fly to Iceland'),
),
OutlinedButton(
onPressed: () async {
final camera = await _controller.getCamera();
debugPrint(camera.toString());
if (context.mounted) {
await showDialog<void>(
context: context,
builder: (context) => AlertDialog(
title: const Text('MapCenter'),
content: Text('''
center: Position(lng: ${camera.center.lng}, lat: ${camera.center.lat})
zoom: ${camera.zoom}
bearing: ${camera.bearing}
tilt: ${camera.tilt}'''),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('OK'),
),
],
),
);
}
},
child: const Text('Get MapCamera'),
),
],
),
),
Expand Down
64 changes: 62 additions & 2 deletions ios/Classes/Pigeon.g.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ struct MapOptions {
}
}

/// A longitude/latitude coordinate object
/// A longitude/latitude coordinate object.
///
/// Generated class from Pigeon that represents data sent in messages.
struct LngLat {
Expand Down Expand Up @@ -151,7 +151,7 @@ struct LngLat {
}
}

/// A pixel location / location on the device screen
/// A pixel location / location on the device screen.
///
/// Generated class from Pigeon that represents data sent in messages.
struct ScreenLocation {
Expand Down Expand Up @@ -180,6 +180,41 @@ struct ScreenLocation {
}
}

/// The current position of the map camera.
///
/// Generated class from Pigeon that represents data sent in messages.
struct MapCamera {
var center: LngLat
var zoom: Double
var tilt: Double
var bearing: Double



// swift-format-ignore: AlwaysUseLowerCamelCase
static func fromList(_ pigeonVar_list: [Any?]) -> MapCamera? {
let center = pigeonVar_list[0] as! LngLat
let zoom = pigeonVar_list[1] as! Double
let tilt = pigeonVar_list[2] as! Double
let bearing = pigeonVar_list[3] as! Double

return MapCamera(
center: center,
zoom: zoom,
tilt: tilt,
bearing: bearing
)
}
func toList() -> [Any?] {
return [
center,
zoom,
tilt,
bearing,
]
}
}

private class PigeonPigeonCodecReader: FlutterStandardReader {
override func readValue(ofType type: UInt8) -> Any? {
switch type {
Expand All @@ -189,6 +224,8 @@ private class PigeonPigeonCodecReader: FlutterStandardReader {
return LngLat.fromList(self.readValue() as! [Any?])
case 131:
return ScreenLocation.fromList(self.readValue() as! [Any?])
case 132:
return MapCamera.fromList(self.readValue() as! [Any?])
default:
return super.readValue(ofType: type)
}
Expand All @@ -206,6 +243,9 @@ private class PigeonPigeonCodecWriter: FlutterStandardWriter {
} else if let value = value as? ScreenLocation {
super.writeByte(131)
super.writeValue(value.toList())
} else if let value = value as? MapCamera {
super.writeByte(132)
super.writeValue(value.toList())
} else {
super.writeValue(value)
}
Expand Down Expand Up @@ -233,6 +273,9 @@ protocol MapLibreHostApi {
func jumpTo(center: LngLat?, zoom: Double?, bearing: Double?, pitch: Double?, completion: @escaping (Result<Void, Error>) -> Void)
/// Animate the viewport of the map to a new location.
func flyTo(center: LngLat?, zoom: Double?, bearing: Double?, pitch: Double?, durationMs: Int64, completion: @escaping (Result<Void, Error>) -> Void)
/// Get the current camera position with the map center, zoom level, camera
/// tilt and map rotation.
func getCamera(completion: @escaping (Result<MapCamera, Error>) -> Void)
/// Convert a coordinate to a location on the screen.
func toScreenLocation(lng: Double, lat: Double, completion: @escaping (Result<ScreenLocation, Error>) -> Void)
/// Convert a screen location to a coordinate.
Expand Down Expand Up @@ -294,6 +337,23 @@ class MapLibreHostApiSetup {
} else {
flyToChannel.setMessageHandler(nil)
}
/// Get the current camera position with the map center, zoom level, camera
/// tilt and map rotation.
let getCameraChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.maplibre.MapLibreHostApi.getCamera\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
getCameraChannel.setMessageHandler { _, reply in
api.getCamera { result in
switch result {
case .success(let res):
reply(wrapResult(res))
case .failure(let error):
reply(wrapError(error))
}
}
}
} else {
getCameraChannel.setMessageHandler(nil)
}
/// Convert a coordinate to a location on the screen.
let toScreenLocationChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.maplibre.MapLibreHostApi.toScreenLocation\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
Expand Down
1 change: 1 addition & 0 deletions lib/maplibre.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export 'package:geotypes/geotypes.dart';
export 'src/annotations.dart';
export 'src/layers.dart';
export 'src/map.dart';
export 'src/map_camera.dart';
export 'src/map_controller.dart';
export 'src/map_options.dart';
export 'src/sources.dart';
29 changes: 29 additions & 0 deletions lib/src/map_camera.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:geotypes/geotypes.dart';

/// The current camera position on the map.
class MapCamera {
/// Default constructor for a [MapCamera].
const MapCamera({
required this.center,
required this.zoom,
required this.tilt,
required this.bearing,
});

/// The position of the map center.
final Position center;

/// The zoom level of the map.
final double zoom;

/// The bearing of the map.
final double bearing;

/// The camera tilt of the map.
final double tilt;

@override
String toString() => 'MapCamera('
'center: Position(lng: ${center.lng}, lat: ${center.lat}), '
'zoom: $zoom, bearing: $bearing, tilt: $tilt)';
}
3 changes: 3 additions & 0 deletions lib/src/map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ abstract interface class MapController {
/// Add a new layer to the map. The source must be added before adding it to
/// the map.
Future<void> addLayer(Layer layer);

/// Get the current camera position on the map.
Future<MapCamera> getCamera();
}
Loading

0 comments on commit 9136f39

Please sign in to comment.