Bestimmung des Mittelpunkts zwischen 2 Cooridinaten
Ich versuche den Mittelpunkt zwischen zu Orten in einem zu bestimmenMKMapView
. Ich folge der beschriebenen MethodeHier (undHier) und schrieb es in Objective-C um, aber die Karte wird irgendwo nordöstlich von Baffin Island zentriert, was nicht weit von den beiden Punkten entfernt ist.
Meine Methode basiert auf der oben verlinkten Java-Methode:
<code>+(CLLocationCoordinate2D)findCenterPoint:(CLLocationCoordinate2D)_lo1 :(CLLocationCoordinate2D)_loc2 { CLLocationCoordinate2D center; double lon1 = _lo1.longitude * M_PI / 180; double lon2 = _loc2.longitude * M_PI / 100; double lat1 = _lo1.latitude * M_PI / 180; double lat2 = _loc2.latitude * M_PI / 100; double dLon = lon2 - lon1; double x = cos(lat2) * cos(dLon); double y = cos(lat2) * sin(dLon); double lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) ); double lon3 = lon1 + atan2(y, cos(lat1) + x); center.latitude = lat3 * 180 / M_PI; center.longitude = lon3 * 180 / M_PI; return center; } </code>
Die 2 Parameter haben folgende Daten:
<code>_loc1: latitude = 45.4959839 longitude = -73.67826455 _loc2: latitude = 45.482889 longitude = -73.57522299 </code>
Die oben genannten Punkte sind korrekt auf der Karte eingezeichnet (in und um Montreal). Ich versuche, die Karte in der Mitte zwischen 2 zu zentrieren, aber meine Methode gibt Folgendes zurück:
<code>latitude = 65.29055 longitude = -82.55425 </code>
was irgendwo in der Arktis, wenn es etwa 500 Meilen südlich sein sollte.