简体   繁体   English

使用wifi和3G分配两点之间的不同距离

[英]Giving the different distance between two points using wifi and 3G

I am calculating the distance between two locations like below 我正在计算两个位置之间的距离,如下所示

             CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

            CLLocationCoordinate2D coordinate = [self getLocation];

            CLLocation *locB = [[CLLocation alloc] initWithLatitude:coordinate.latitude   longitude:coordinate.longitude];

            CLLocationDistance distance = [locA distanceFromLocation:locB];


            CLLocationDistance kilometers = distance / 1000.0;

But it giving the different result when i connect to wifi and 3G network.how to get the same result for both. 但是当我连接到wifi和3G网络时,它给出了不同的结果。如何为两者获得相同的结果。

You invariably will not get the same location for both. 两者的位置永远不会相同。 In fact, if you come back to the exact same location tomorrow and turn on location services, you might not get the same latitude and longitude even if you stayed with the same technology. 实际上,如果明天返回精确的位置并启用位置服务,即使您使用相同的技术,也可能不会获得相同的纬度和经度。

The latitude and longitude you receive from a CLLocationManager is not an exact science. 您从CLLocationManager接收到的纬度和经度并不是一门精确的科学。 In fact, if you look at the original CLLocation objects you received, you'll notice that there is a horizontalAccuracy property that reflects how accurate/inaccurate the coordinate could be. 实际上,如果您查看收到的原始CLLocation对象,则会注意到有一个horizontalAccuracy属性,该属性反映了坐标的精确度/不准确度。 When you first start location services, you'll even see multiple locations come in, generally with increasing accuracy. 首次启动位置服务时,您甚至会看到多个位置,通常以提高的准确性为准。

But it will never be perfect and you shouldn't expect it to be. 但是它永远不会是完美的,您不应该期望它是完美的。 This is one of the reasons why it's important to test location-aware code on a real device, because the simulator will lull one into a false sense of absolute accuracy which doesn't exist in the real world. 这是在真实设备上测试位置感知代码很重要的原因之一,因为模拟器会使人误以为是真实世界中不存在的绝对准确性。 You should write code that anticipates this. 您应该编写可以预见到这一点的代码。 And, if nothing else, you should start considering horizontalAccuracy of the objects you receive from the location manager. 并且,如果没有其他问题,则应该开始考虑从位置管理器收到的对象的horizontalAccuracy

there's nothing wrong in the code lines you posted, the only line that could give you differences in the 2 cases (wifi and 3g) is your method [self getLocation] 您发布的代码行没有错,在两种情况下(wifi和3g)唯一可以使您区别的行是您的方法[self getLocation]

where i guess you get the device location. 我猜你在哪里得到设备的位置。

In this case your device may use both gps and wifi access to calculate its location, so little differences may occurs (you may have noticed that some map applications advice you to turn your wifi on to get more accuracy) 在这种情况下,您的设备可能同时使用gps和wifi访问来计算其位置,因此差异可能很小(您可能已经注意到,某些地图应用建议您打开wifi以提高准确性)

Anyway, in your getLocation method try to add a more precise accuracy when you use CLLocationManager 无论如何,当您使用CLLocationManager时,请在您的getLocation方法中尝试添加更精确的精度

i tried this: 我尝试了这个:

- (CLLocationCoordinate2D )getLocation {
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];
    CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);

    return startCoord;
}

and the accuracy is enough to get no differences between 3G/wifi but you could set it to 而且准确度足以使3G / wifi之间没有区别,但您可以将其设置为

kCLLocationAccuracyBestForNavigation;
kCLLocationAccuracyBest;
kCLLocationAccuracyNearestTenMeters;

ps i hope you are using a device with GPS, of course... ps,我希望您使用的是带GPS的设备,当然...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM