Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handled methods camera#move and camera#animate #30

Merged
merged 9 commits into from
Mar 27, 2019
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ After you get the key, place it in project's Android directory:
- Add your access token to `$project_dir/example/android/app/src/values/developer-config.xml`

#### iOS
Add this lines to your Info.plist
Add these lines to your Info.plist
```plist
<key>io.flutter.embedded_views_preview</key>
<true/>
Expand Down
84 changes: 84 additions & 0 deletions ios/Classes/Convert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,88 @@ class Convert {
delegate.setMyLocationEnabled(myLocationEnabled: myLocationEnabled)
}
}

class func parseCameraUpdate(cameraUpdate: [Any], mapView: MGLMapView) -> MGLMapCamera? {
guard let type = cameraUpdate[0] as? String else { return nil }
switch (type) {
case "newCameraPosition":
guard let cameraPosition = cameraUpdate[1] as? [String: Any] else { return nil }
return MGLMapCamera.fromDict(cameraPosition, mapView: mapView)
case "newLatLng":
guard let coordinate = cameraUpdate[1] as? [Double] else { return nil }
let camera = mapView.camera
camera.centerCoordinate = CLLocationCoordinate2D.fromArray(coordinate)
return camera
case "newLatLngBounds":
guard let bounds = cameraUpdate[1] as? [[Double]] else { return nil }
guard let padding = cameraUpdate[2] as? CGFloat else { return nil }
return mapView.cameraThatFitsCoordinateBounds(MGLCoordinateBounds.fromArray(bounds), edgePadding: UIEdgeInsets.init(top: padding, left: padding, bottom: padding, right: padding))
case "newLatLngZoom":
guard let coordinate = cameraUpdate[1] as? [Double] else { return nil }
guard let zoom = cameraUpdate[2] as? Double else { return nil }
let camera = mapView.camera
camera.centerCoordinate = CLLocationCoordinate2D.fromArray(coordinate)
let altitude = getAltitude(zoom: zoom, mapView: mapView)
return MGLMapCamera(lookingAtCenter: camera.centerCoordinate, altitude: altitude, pitch: camera.pitch, heading: camera.heading)
case "scrollBy":
guard let x = cameraUpdate[1] as? CGFloat else { return nil }
guard let y = cameraUpdate[1] as? CGFloat else { return nil }
let camera = mapView.camera
let mapPoint = mapView.convert(camera.centerCoordinate, toPointTo: mapView)
let movedPoint = CGPoint(x: mapPoint.x + x, y: mapPoint.y + y)
camera.centerCoordinate = mapView.convert(movedPoint, toCoordinateFrom: mapView)
return camera
case "zoomBy":
guard let zoomBy = cameraUpdate[1] as? Double else { return nil }
let camera = mapView.camera
let zoom = getZoom(mapView: mapView)
let altitude = getAltitude(zoom: zoom+zoomBy, mapView: mapView)
camera.altitude = altitude
if (cameraUpdate.count == 2) {
return camera
} else {
guard let point = cameraUpdate[2] as? [CGFloat] else { return nil }
//TODO
}
case "zoomIn":
let camera = mapView.camera
let zoom = getZoom(mapView: mapView)
let altitude = getAltitude(zoom: zoom + 1, mapView: mapView)
camera.altitude = altitude
return camera
case "zoomOut":
let camera = mapView.camera
let zoom = getZoom(mapView: mapView)
let altitude = getAltitude(zoom: zoom - 1, mapView: mapView)
camera.altitude = altitude
return camera
case "zoomTo":
guard let zoom = cameraUpdate[1] as? Double else { return nil }
let camera = mapView.camera
let altitude = getAltitude(zoom: zoom, mapView: mapView)
camera.altitude = altitude
return camera
case "bearingTo":
guard let bearing = cameraUpdate[1] as? Double else { return nil }
let camera = mapView.camera
camera.heading = bearing
return camera
case "tiltTo":
guard let tilt = cameraUpdate[1] as? CGFloat else { return nil }
let camera = mapView.camera
camera.pitch = tilt
return camera
default:
print("\(type) not implemented!")
}
return nil
}

class func getZoom(mapView: MGLMapView) -> Double {
return MGLZoomLevelForAltitude(mapView.camera.altitude, mapView.camera.pitch, mapView.camera.centerCoordinate.latitude, mapView.frame.size)
}

class func getAltitude(zoom: Double, mapView: MGLMapView) -> Double {
return MGLAltitudeForZoomLevel(zoom, mapView.camera.pitch, mapView.camera.centerCoordinate.latitude, mapView.frame.size)
}
}
12 changes: 12 additions & 0 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
} else {
result(nil)
}
case "camera#move":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let cameraUpdate = arguments["cameraUpdate"] as? [Any] else { return }
if let camera = Convert.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView) {
mapView.setCamera(camera, animated: false)
}
case "camera#animate":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let cameraUpdate = arguments["cameraUpdate"] as? [Any] else { return }
if let camera = Convert.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView) {
mapView.setCamera(camera, animated: true)
}
default:
result(FlutterMethodNotImplemented)
}
Expand Down