OSMDroid get the radius of the map - kotlin

I'm using OSMDroid (Open Street Map for Android), and I'm trying to get the radius of the visible map, to send it to my API and get a result scaled correctly.
Here is what I'm doing on iOS using MapKit to achieve this:
extension MKMapView {
func topLeftCoordinate() -> CLLocationCoordinate2D {
return self.convert(CGPoint(x: 0, y: 0), toCoordinateFrom: self)
}
func currentRadius() -> Double {
let topLeftCoordinate = self.topLeftCoordinate()
return sqrt(pow(centerCoordinate.latitude - topLeftCoordinate.latitude, 2) + pow(centerCoordinate.longitude - topLeftCoordinate.longitude, 2))
}
}
OSMDroid is having a zoomLevel but I'm unable to find a way to convert it to what I need, keeping the correct scaling for API.
Thanks in advance for any help or lead.

I finally found a working solution. It's some kind of magic, but it seems to work correctly (constants were adapted a lot of times before getting the result)
fun MapView.currentRadius(): Double {
// This is magic
return 400 / 2.toDouble().pow(zoomLevelDouble)
}

Related

Adjusting screen contrast programmatically in Cocoa application

I'm trying to adjust the screen contrast in Objective C for a Cocoa application using kIODisplayContrastKey. I saw a post about adjusting screen brightness here:
Programmatically change Mac display brightness
- (void) setBrightnessTo: (float) level
{
io_iterator_t iterator;
kern_return_t result = IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceMatching("IODisplayConnect"),
&iterator);
// If we were successful
if (result == kIOReturnSuccess)
{
io_object_t service;
while ((service = IOIteratorNext(iterator)))
{
IODisplaySetFloatParameter(service, kNilOptions, CFSTR(kIODisplayBrightnessKey), level);
// Let the object go
IOObjectRelease(service);
return;
}
}
}
Code by Alex King from the link above.
And that code worked. So I tried to do the same for contrast by using a different key (kIODisplayContrastKey), but that doesn't seem to work. Does anybody have an idea if that's possible?
I'm using 10.9.3

Resizing JavaFX Windows while maintaining aspect ratio?

I have a window, which I have to keep square. My code is
primaryStage.minHeightProperty().bind(scene.widthProperty());
primaryStage.minWidthProperty().bind(scene.heightProperty());
It does resize the square when extending but has problems when I try to make it smaller?
Sometimes one of the sides gets a little shorter or longer than the other one. Is there a fix for this? Did I do something wrong in the code I currently use?
This doesn't answer your question directly, but I think is actually better UX since you don't restrict the user. Instead of fixing the aspect ratio of the stage, consider using insets to pad the content you want to remain square:
private val DEF_PAD = 6.0
chart.paddingProperty.bind(boundValue(stage.widthProperty, stage.heightProperty) {
// Maintain a square plot with padding
val w = stage.getWidth
val h = stage.getHeight
val extra = DEF_PAD + 0.5 * Math.abs(w - h)
if (w > h) {
new Insets(DEF_PAD, extra, DEF_PAD, extra)
} else if (h > w) {
new Insets(extra, DEF_PAD, extra, DEF_PAD)
} else {
new Insets(DEF_PAD)
}
})
(Written in Scala. boundValue is just a utility that creates a binding with a varargs dependency list)

Unity: camera falls through terrain

Problem is:
I have created terrain and I need to fly over terrain with Camera. I added to Camera "Mouse Look" script, RigidBody: usegravity - unchecked and I have added my code in Update method:
float vert = Input.GetAxis("Vertical");
float hor = Input.GetAxis("Horizontal");
if (vert != 0)
{
if (!Physics.Raycast(this.transform.position, this.transform.forward, 5))
{
transform.Translate(Vector3.forward * flySpeed * vert);
}
else
{
transform.Translate(Vector3.up * flySpeed * vert);
}
}
if (hor != 0)
{
if (!Physics.Raycast(this.transform.position, this.transform.forward, 5))
{
transform.Translate(Vector3.right * flySpeed * hor);
}
else
{
transform.Translate(Vector3.up * flySpeed* hor);
}
}
if (Input.GetKey(KeyCode.E))
{
transform.Translate(Vector3.up * flySpeed);
}
else if (Input.GetKey(KeyCode.Q))
{
Vector3 v = Vector3.down * flySpeed;
if (!Physics.Raycast(this.transform.position, this.transform.forward, 5))
{
transform.Translate(v);
}
}
But sometimes then i go down - Q - camera goes through terrain. Why?
Also looks ugly if you are moving with camera forward as low as possible over terrain and camera does not fall through it - it starts to jump. Also why?
Make sure you have a Terrain Collider on your terrain.
In addition to S.Richmonds answer, you can add a character controller or other similar collider-component object to your camera.
See this answer in the unity questions network:
http://answers.unity3d.com/questions/45763/getting-camera-to-not-see-under-the-ground.html
The Update() method in a monobehavior gets called once each fram. Because the rate which update is called is dependent on frame rate, moving an object by a constant value in Update() can result in inconsistant motion. This can be corrected by multiplying a constant speed by Time.deltaTime, which is the time in seconds since the last frame was rendered. This will fix the fallthrough unless flySpeed is set too high (where the change in position each frame is greater than the collider's size). Additionally as suggested above, using a CharacterController without a rigidbody would be better suited to this situation. Rigidbodies are for objects primarily controlled by physics, while the CharacterController is for objects controlled by scripts.

How to set up Camera Unity

Someone can help me to create a camera like this: http://www.youtube.com/watch?v=8fIoxtJ_FK4&feature=youtu.be&t=1m24s
I know that, they used gyroscope to do this. However, I don't know How did they do with the camera
Thanks a lot!
You can either use the rotation rate or the attitude of the gyroscope. Just update the rotation of your camera based on the gyro. Here is an example code how it could be done.
void Start()
{
Input.gyro.enabled = true;
}
void Update()
{
rotationRate = Input.gyro.rotationRateUnbiased;
angle.x += -rotationRate.x * ROTATION_SPEED;
angle.y += -rotationRate.y * ROTATION_SPEED;
Camera.main.transform.localEulerAngles = angle;
}
The documentation can be found here: http://docs.unity3d.com/Documentation/ScriptReference/Gyroscope.html

Computing the union 2 MKPolygons

I am working on map applications with polygon MKOverlays. I have a requirement to merge (union) overlapping polygons.
Is there a well known algorithm to do this? Are there any free existing libraries/implementations that help with such geometry operations?
I have found the GEOS library, but apparently its licensing terms disallow use without distributing your source code. Is anyone else using this library. If yes, where can I find the way to include this in my Xcode project.
The only free libraries I'm aware of are -
Clipper:
http://angusj.com/delphi/clipper.php
Boost Polygon:
http://www.boost.org/doc/libs/1_47_0/libs/polygon/doc/index.htm
Boost Geometry:
http://trac.osgeo.org/ggl/
Try gpc. They have several licences. Also there are similar libraries listed on their page.
There is a great library RSClipperWrapper which is basically a wrapper for Clipper. There is even a great library comparison inside their website:
TL;DR, free library, error free and fast.
A few notes:
RSClipperWrapper takes CGPoint but fear not, you can pass lat/long into it and it will get the job done (tested and verified).
For convinice I've written an extension so we can just pass a custom Polygon array and get the merged polygons - if you're using MKPolygon or other type then don't forget adjust your type:
extension Clipper {
static func union(polygons: [Polygon]) -> [Polygon] {
let pointsPolygons = convert(polygons: polygons)
let unionfied = Clipper.unionPolygons(pointsPolygons, withPolygons: pointsPolygons)
return convert(pointsPolygons: unionfied)
}
static func convert(polygons: [Polygon]) -> [[CGPoint]] {
var polygonsPoints: [[CGPoint]] = []
for polygon in polygons {
var points: [CGPoint] = []
for location in polygon.locations {
points.append(CGPoint(x: location.coordinate.latitude, y: location.coordinate.longitude))
}
polygonsPoints.append(points)
}
return polygonsPoints
}
static func convert(pointsPolygons: [[CGPoint]]) -> [Polygon] {
var polygons: [Polygon] = []
for pointsPolygon in pointsPolygons {
var locations: [CLLocation] = []
for point in pointsPolygon {
locations.append(CLLocation(latitude: CLLocationDegrees(point.x), longitude: CLLocationDegrees(point.y)))
}
let polygon = Polygon(locations: locations)
polygons.append(polygon)
}
return polygons
}
}