【问题标题】:Find the distance between my location and a point in ios查找我的位置与ios中某个点之间的距离
【发布时间】:2015-09-14 16:03:09
【问题描述】:

我正在使用一种方法来获取我的位置:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {


CLLocation *currentLocation = newLocation;

NSLog(@"%@", currentLocation);}


//new method to find the distance
        -(void) zoomToNearestSpot: (NSArray *) spot location: (CLLocation *)myLocation
    {
        myLocation = currentLocation;

        spot = chSpot;

        for (int i=0; i< spot.count; i++)
        {
        ChargingSpots *mySpot = [spot objectAtIndex:i];

        CLLocation *thePoint = [[CLLocation alloc]initWithLatitude:[mySpot.LocationLat doubleValue] longitude:[mySpot.LocationLong doubleValue]];

        CLLocationDistance dist = [myLocation distanceFromLocation:thePoint];

            NSLog(@"%f", dist);


       you get here **distance** in meter so in assumption  **20 miles = 32187 meter** 

设置条件,例如

if(distance <= 32187)
{
  // under 20 miles   you can add the zoom level
}
else
{
  // out of 20 miles
}



        }
    }

首先,我需要根据它们的数量缩小地图上的所有地点,然后找到最近的地点并将相机放大。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    你有更多的两个坐标

    1) 当前(用户位置)
    2) 另一个坐标..

    这里distanceFromLocation:方法用于获取两个位置之间的距离。

    CLLocationDistance distance = [currentLOcation distanceFromLocation:anotherLocation];
    

    例如,您需要添加以下行

    -(void)locationManager:(CLLocationManager *)manager
       didUpdateToLocation:(CLLocation *)newLocation
                  fromLocation:(CLLocation *)oldLocation
     {
    
      CLLocation *currentLocation = newLocation;
    
       for (int i=0; i<chSpot.count; i++)
     {
    
         ChargingSpots *spot = [chSpot objectAtIndex:i];
         NSString *locLat = spot.LocationLat;
         NSString *locLong = spot.LocationLong;
    
         CLLocation *LocationAtual = [[CLLocation alloc] initWithLatitude: locLat longitude: locLong];
    
         CLLocationDistance distanceBetween = [currentLocation
            distanceFromLocation: LocationAtual];
    
         NSString *tripString = [[NSString alloc] 
           initWithFormat:@"%f", 
           distanceBetween];
    
         NSLog(@"DIST: %f", tripString); 
       }
    
     }
    

    【讨论】:

    • 如何从我的方法中获取“currentLocation”值??我需要在“ViewdidLoad”中添加这段代码,而不是在方法中
    • 在全局范围内声明 CLLocation *currentLocation
    • 好的,这很好,但我的位置是在 ViewDidLoad 中声明的,它的所有属性都在那里使用,所以如何从中获取纬度和经度??
    • 对不起,最后一件事,这段代码也依赖于 ViewDidLoad 中的一些前几行,我已经更新了我的代码,所以你可以看到它
    • @AbdelrahmanManna -- 如您所愿,您可以从视图 didload 中删除代码并添加委托方法,否则部分获取位置并创建自定义方法,否则 chSpot --> 为此创建内存数组并为此添加值,您可以通过多种方式实现此概念
    【解决方案2】:

    你可以使用这个代码

    1.) CLLocation *loc=[[CLLocation alloc]initWithLatitude:spot.LocationLat longitude:spot.LocationLong];

    2.) CLLocationDistance distanceInMeters=[currentLocation distanceFromLocation:loc];

    这会有所帮助。谢谢你

    【讨论】:

    • 如何从我的方法中获取“currentLocation”值??我需要在“ViewdidLoad”中添加这段代码,而不是在方法中
    • 让它成为类数据成员,当前位置在这个函数之外 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {}。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 2012-06-20
    相关资源
    最近更新 更多