我正在尝试为CLLocation编写一个类别,以将轴承返回到另一个CLLocation.
我相信我对公式做错了(计算不是我的强项).返回的轴承始终关闭.
我一直在看这个问题并尝试应用被接受为正确答案的更改及其引用的网页:
计算两个CLLocationCoordinate2D之间的方位
http://www.movable-type.co.uk/scripts/latlong.html
谢谢你的任何指示.我已经尝试将其他问题的反馈结合起来,而我仍然没有得到任何东西.
谢谢
这是我的类别 -
----- CLLocation + Bearing.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface CLLocation (Bearing)
-(double) bearingToLocation:(CLLocation *) destinationLocation;
-(NSString *) compassOrdinalToLocation:(CLLocation *) nwEndPoint;
@end
Run Code Online (Sandbox Code Playgroud)
--------- CLLocation + Bearing.m
#import "CLLocation+Bearing.h"
double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};
@implementation CLLocation (Bearing)
-(double) bearingToLocation:(CLLocation *) destinationLocation {
double lat1 = DegreesToRadians(self.coordinate.latitude);
double lon1 = DegreesToRadians(self.coordinate.longitude);
double lat2 = DegreesToRadians(destinationLocation.coordinate.latitude);
double lon2 = DegreesToRadians(destinationLocation.coordinate.longitude);
double dLon = lon2 …Run Code Online (Sandbox Code Playgroud) 所以我正在处理iPhone GPS的一些准确性问题.我有一个使用位置的应用程序.
在委托方法中,locationManager: didUpdateToLocation: fromLocation:我正在返回一个位置.从一些研究来看,似乎GPS在返回的第一个结果上并不总是准确的,即使设置desiredAccuracy属性也是如此kCLLocationAccuracyBest.
为了解决这个问题,我不会stopUpdatingLocation在它返回newLocation:至少3次之前打电话(这很快).我还玩过两个关于是否stopUpdatingLocation返回的"要求" newLocation.我尝试的一种方法是检查lat和long newLocation并进行比较oldLocation,如果这些不相同,则保持位置更新运行.我也试图与检查之间的距离oldLocation和newLocation,如果它小于20米,它的罚款.这两个都是通过返回至少3次运行来测试的.后一种方式不那么"严格",因为如果用户在移动的车辆中,newLocation并且oldLocation很难与100%完全相同.
现在,我的问题是,即使在进行上述操作时(基本上不接受某个位置,直到发生一些更新并CLLocationManager检查CLLocations它们之间的距离(或者它们是否相同),我仍然看到有时奇怪的位置结果,在测试时.
如果我离开应用程序,进入Maps.app,使用GPS,然后打开多任务处理,强制退出我的应用程序,然后重新打开它以获得干净的启动,有时会修复.
人们习惯于解决同类问题的经验和可能的解决方案?评论和解决方案赞赏:)
这是我的代码......
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
location_updated = [locations lastObject];
NSLog(@"updated coordinate are %@",location_updated);
latitude1 = location_updated.coordinate.latitude;
longitude1 = location_updated.coordinate.longitude;
self.lblLat.text = [NSString stringWithFormat:@"%f",latitude1];
self.lblLon.text = [NSString stringWithFormat:@"%f",longitude1];
NSString *str = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude1,longitude1];
url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection)
{
webData1 = [[NSMutableData alloc]init];
}
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(latitude1,longitude1);
marker.title = formattedAddress;
marker.icon = [UIImage imageNamed:@"m2.png"];
marker.map = mapView_;
marker.draggable = YES;
}
Run Code Online (Sandbox Code Playgroud)
这个方法多次调用,我不想......
我正在尝试计算仅限swift的代码中两个CLLocation点之间的方位.我遇到了一些困难,并假设这是一个非常简单的功能.堆栈溢出似乎没有列出任何内容.
func d2r(degrees : Double) -> Double {
return degrees * M_PI / 180.0
}
func RadiansToDegrees(radians : Double) -> Double {
return radians * 180.0 / M_PI
}
func getBearing(fromLoc : CLLocation, toLoc : CLLocation) {
let fLat = d2r(fromLoc.coordinate.latitude)
let fLng = d2r(fromLoc.coordinate.longitude)
let tLat = d2r(toLoc.coordinate.latitude)
let tLng = d2r(toLoc.coordinate.longitude)
var a = CGFloat(sin(fLng-tLng)*cos(tLat));
var b = CGFloat(cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(fLng-tLng))
return atan2(a,b)
}
Run Code Online (Sandbox Code Playgroud)
我的atan2调用关于lvalue cgfloat或其他东西我收到错误...
我正在试图做的是传递CLLocation给函数getPlacemarkFromLocation,然后使用传递CLLocation通过reverseGeocodeLocation设置CLPlacemark?将返回.
我在创建completionHandler闭包时遇到问题reverseGeocodeLocation,它会抛出编译器错误/崩溃:
在斯威夫特,CLGeocodeCompletionHandler是CLGeocodeCompletionHandler = (AnyObject[]!, NSError!) -> Void根据文档AnyObject[]!应该包含CLPlacemark的对象,就像Objective-C的版本.
这是我目前的代码:
class func getPlacemarkFromLocation(location:CLLocation)->CLPlacemark?{
var g = CLGeocoder()
var p:CLPlacemark?
g.reverseGeocodeLocation(location, completionHandler: {
(placemarks, error) in
let pm = placemarks as? CLPlacemark[]
if (pm && pm?.count > 0){
p = placemarks[0] as? CLPlacemark
}
})
return p?
}
Run Code Online (Sandbox Code Playgroud)
编辑:似乎错误placemarks.count与placemarks没有像数组一样对待.它现在编译,但是当我试图进入p内部时,我只得到零completionHandler.我检查了CLLocation被传递的内容并且它们是有效的. …
我有两个代表纬度和经度的字符串,如:" - 56.6462520",我想分配给一个CLLocation对象来比较我当前的位置.我尝试了下面的代码,但我只得到错误:
CLLocation * LocationAtual = [[CLLocation alloc]init];
LocationAtual.coordinate.latitude = @"-56.6462520";
LocationAtual.coordinate.longitude = @"-36.6462520";
Run Code Online (Sandbox Code Playgroud)
然后将对象与我的实际位置纬度和经度进行比较.有什么建议?
在目标C(我没有经验)你可以用纬度和经度初始化一个CLLocation对象,如下所示:
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:your_latitiude_value longitude:your_longitude_value];
Run Code Online (Sandbox Code Playgroud)
但是我如何在Swift中做到这一点?
我试过这个,
let loc_coords = CLLocationCoordinate2D(latitude: your_latitiude_value, longitude: your_longitude_value)
let loc = CLLocation().coordinate(loc_coords)
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
无法使用类型'(CLLocationCoordiate2D)'的参数列表调用"coordinate"
我需要它作为CLLocation对象,因为我想在locationManager中使用distanceFromLocation方法.
希望你能帮忙,谢谢!
latitude-longitude cllocation cllocationdistance cllocationcoordinate2d swift
我需要检查用户位置是否属于MKCoordinateRegion.我很惊讶没有为此找到简单的功能,例如:CGRectContainsCGPoint(rect,point).
我找到了以下代码:
CLLocationCoordinate2D topLeftCoordinate =
CLLocationCoordinate2DMake(region.center.latitude
+ (region.span.latitudeDelta/2.0),
region.center.longitude
- (region.span.longitudeDelta/2.0));
CLLocationCoordinate2D bottomRightCoordinate =
CLLocationCoordinate2DMake(region.center.latitude
- (region.span.latitudeDelta/2.0),
region.center.longitude
+ (region.span.longitudeDelta/2.0));
if (location.latitude < topLeftCoordinate.latitude || location.latitude > bottomRightCoordinate.latitude || location.longitude < bottomRightCoordinate.longitude || location.longitude > bottomRightCoordinate.longitude) {
// Coordinate fits into the region
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定它是否准确,因为文档没有准确指定区域矩形的计算方式.
必须有更简单的方法来做到这一点.我是否忽略了MapKit框架文档中的某些功能?
我怎样才能得到两米之间的距离CLLocation?CLLocation它没有提供任何方法来实现它.
我有一个我想要调用的方法但是当我回到地图的中心时,它处于CLLocationCoordinate2D类型.
如何将CLLocationCoordinate2D的结果放入CLLocation?
cllocation ×10
ios ×6
iphone ×4
swift ×4
mapkit ×3
objective-c ×2
cocoa-touch ×1
haversine ×1
mkmapview ×1
string ×1