I want to calculate user location based on iBeacon data and Accuracy. At this moment i have 2 iBeacon sensor(but in the future i will use 3 iBeacons for calculation user location), each iBeacon has own location and based on location and signal(Accuracy) i want to calculate user location.
For test, i create method
func determineLocation() -> CLLocationCoordinate2D? {
let beacon1 = CLLocation(latitude: 51.78889022813119, longitude: 19.440722653352616)
let beacon2 = CLLocation(latitude: 51.788790844937374, longitude: 19.440694676783878)
let beacon1Accuracy = 2.0
let beacon2Accuracy = 3.0
let x1 = beacon1.coordinate.latitude
let y1 = beacon1.coordinate.longitude
let r1 = beacon1Accuracy
let x2 = beacon2.coordinate.latitude
let y2 = beacon2.coordinate.longitude
let r2 = beacon2Accuracy
// Calculate the user's location using trilateration
let A = 2 * x2 - 2 * x1
let B = 2 * y2 - 2 * y1
let C = pow(r1, 2) - pow(r2, 2) - pow(x1, 2) + pow(x2, 2) - pow(y1, 2) + pow(y2, 2)
let x = C * B / (pow(B, 2) + pow(A, 2))
let y = C * A / (pow(B, 2) + pow(A, 2))
// Create a CLLocationCoordinate2D object with the user's location
let location = CLLocationCoordinate2D(latitude: x, longitude: y)
return location
}
but this method return incorrect location, it looks like this:
latitude: 25939.440985715428, longitude: 84509.21179857382
Can you please help me find what the problem, and maybe if someone know libraries which can help it will be nice(iOS, Android also will be great)