获取iPhone中源和目标坐标之间的所有纬度和经度

时间:2014-06-23 05:00:10

标签: ios objective-c ios7

我想检索源坐标和目标坐标之间的所有纬度和经度。我知道我们可以轻松地在源坐标和目标坐标之间创建一条路径,但我想要它们之间的所有纬度和经度,以便用户移动。我是使用MkMapView显示坐标和CLLocationManager来检索当前位置。我是iPhone的新手。我搜索了这个问题,但我没有得到准确的结果。请帮助我。

2 个答案:

答案 0 :(得分:1)

这是一个高中代数问题。如果您有点A和点B(在视图坐标系中),那么您可以找到描述两者之间的线的等式。你很可能想要使用点斜率公式。一旦你有一个描述该线的方程,那么你可以找到它们之间的所有点,但是线上有无数个点。您必须决定所需的准确度。无论如何,从A点的X到B的X的简单循环,在每次迭代中评估方程并求解Y应该会产生一组点。然后,您可以使用MKMapView中的convertPoint方法获取lat和long。

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

编辑:使用具体示例进行更新

这假设你有一个mkMapview实例。这可能应该放在你的mapview中。如果您需要更多解释,可以查看mathisfun.com http://www.mathsisfun.com/algebra/line-equation-2points.html

MKMapView mapView.... //this is your job to intialize, this code probably belongs inside
//your mapview so should probably be assigned to self. 

//Point slope formula: y - y1 = m*(x - x1)
//m = x-x1 / y - y1

//two points from a line that starts at 0,0 and goes diagonlly up to the right at a 45 degree angle
CGPoint pointA = CGPointMake(0, 0);
CGPoint pointB = CGPointMake(10, 10);

//find the slope
CGFloat m = (pointB.y - pointA.y) / (pointB.x - pointA.x);

NSMutableArray *points = [[NSMutableArray alloc] init];
for (int i = pointA.x; i <= pointB.x; i++)
{
    //for each x value between A and B use the point slope formula to find the y value
    //since were starting at pointA (0, 0) our equation becomes
    //y - 0 = m * (x - 0), simplifying and solving for y we get
    //y = m * (x - 0) -> y = m*x, and since we are increment x each time our equatio we evaluate is
    // y = m*i
    CGFloat y = m*i;

    CGPoint c = CGPointMake(i, y);

    CLLocationCoordinate2D coordinate = [mapView convertPoint:c toCoordinateFromView:self.view];

    //for simplicity in adding to the array I convert to a CLLocation object since an NSArray can only hold objects
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    [points addObject:location];
}

答案 1 :(得分:0)

您可以使用MKDirections API获取路由信息,但不会返回路径中步骤的各个纬度和经度。它将返回一系列步骤,这些步骤是导航说明,MKPolyLines可以覆盖MKMapView