IOS-IOS中的googlemaps polyutils等效类

IOS-IOS中的googlemaps polyutils等效类,ios,objective-c,google-maps,google-maps-sdk-ios,Ios,Objective C,Google Maps,Google Maps Sdk Ios,我试图在IOS google maps util库中找到PolyUtils.distanceToLine等效函数 函数distanceToLine能够计算点和线段之间的距离,我在Google maps IOS utils库中找不到任何类似的函数 好吧,即使我也面临同样的问题,所以我将PolyUtils Android库转换为Objective-C - (double) distanceOfPointToLine :(double)currentLocationX currentLocation

我试图在IOS google maps util库中找到
PolyUtils.distanceToLine
等效函数

函数
distanceToLine
能够计算点和线段之间的距离,我在Google maps IOS utils库中找不到任何类似的函数


好吧,即使我也面临同样的问题,所以我将PolyUtils Android库转换为Objective-C

- (double) distanceOfPointToLine :(double)currentLocationX currentLocationY:(double)currentLocationY x1:(double)x1 y1:(double)y1 x2:(double)x2 y2:(double)y2 {
    if (x1 == x2) {
        [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2];
    } else {
        double s0lat = [self degreeToRadians:currentLocationX];
        double s0lng = [self degreeToRadians:currentLocationY];
        double s1lat = [self degreeToRadians:x1];
        double s1lng = [self degreeToRadians:y1];
        double s2lat = [self degreeToRadians:x2];
        double s2lng = [self degreeToRadians:y2];
        double s2s1lat = s2lat - s1lat;
        double s2s1lng = s2lng - s1lng;
        double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
        if (u <= 0.0) {
             NSLog(@"%f",[self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x1 toLong:y1]);
             return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x1 toLong:y1];
        } else if (u >= 1.0) {
             NSLog(@"%f",[self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2]);
            return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2];
        } else {
            CLLocation* sa = [[CLLocation alloc]initWithLatitude:currentLocationX-x1 longitude:currentLocationY-y1];
            CLLocation* sb = [[CLLocation alloc]initWithLatitude:u*x2-x1 longitude:u*y2-y1];
            NSLog(@"%f",[self computeDistanceBetween:sa.coordinate.latitude fromLong:sa.coordinate.longitude toLat:sb.coordinate.latitude toLong:sb.coordinate.longitude]);
            return [self computeDistanceBetween:sa.coordinate.latitude fromLong:sa.coordinate.longitude toLat:sb.coordinate.latitude toLong:sb.coordinate.longitude];
        }
    }
    return 0;
}
- (double)computeDistanceBetween :(double)fromLat fromLong:(double)fromLong toLat:(double)toLat toLong:(double)toLong {
        return [self computeAngleBetween:fromLat lng1:fromLong lat2:toLat lng2:toLong] * 6371009.0;
}
- (double)computeAngleBetween :(double)lat1 lng1:(double)lng1 lat2:(double)lat2 lng2:(double)lng2 {
    return [self distanceRadians:[self degreeToRadians:lat1] lng1:[self degreeToRadians:lng1] lat2:[self degreeToRadians:lat2] lng2:[self degreeToRadians:lng2]];
}
- (double)degreeToRadians :(double)paramDegree {
    return paramDegree * M_PI/180;
}

- (double)distanceRadians :(double)lat1 lng1:(double)lng1 lat2:(double)lat2 lng2:(double)lng2 {
    return [self arcHav:[self havDistance:lat1 lat2:lat2 differenceBetweenLongitudes:lng1-lng2]];
}

- (double)arcHav :(double)x {
    return 2.0* asin(sqrt(x));
}
- (double)havDistance :(double)lat1 lat2:(double)lat2 differenceBetweenLongitudes:(double)differenceBetweenLongitudes {
    double sum = [self hav:lat1-lat2] + [self hav:differenceBetweenLongitudes] * cos(lat1) *cos(lat2);
    return sum;
}
- (double)hav :(double)x  {
    double sinHalf = sin(x*0.5);
    return sinHalf*sinHalf;
}
点线的(双)距离:(双)当前位置x当前位置y:(双)当前位置y x1:(双)x1 y1:(双)y1 x2:(双)x2 y2:(双)y2{ 如果(x1==x2){ [自计算距离介于:currentLocationX fromLong:currentLocationY到:x2到:y2]; }否则{ 双s0lat=[自度弧度:currentLocationX]; 双s0lng=[自度弧度:当前位置Y]; 双s1lat=[自度弧度:x1]; 双s1lng=[自度弧度:y1]; 双s2lat=[自度弧度:x2]; 双s2lng=[自度弧度:y2]; 双s2s1lat=s2lat-s1lat; 双s2s1lng=s2lng-s1lng; 双u=((s0lat-s1lat)*s2s1lat+(s0lng-s1lng)*s2s1lng)/(s2s1lat*s2s1lat+s2s1lng*s2s1lng); 如果(u=1.0){ NSLog(@“%f”,[自计算距离介于:currentLocationX fromLong:currentLocationY toLat:x2到long:y2]); 返回[自计算的距离介于:currentLocationX from long:currentLocationY toLat:x2 toLong:y2]; }否则{ CLLocation*sa=[[CLLocation alloc]initWithLatitude:currentLocationX-x1经度:currentLocationY-y1]; CLLocation*sb=[[CLLocation alloc]INITWITH纬度:u*x2-x1经度:u*y2-y1]; NSLog(@“%f”,[自计算距离介于:sa.coordinate.latitude from long:sa.coordinate.longitude toLat:sb.coordinate.latitude toLong:sb.coordinate.longitude]); 返回[自计算距离介于:sa.coordinate.latitude from long:sa.coordinate.longitude toLat:sb.coordinate.latitude toLong:sb.coordinate.longitude]; } } 返回0; } -(double)计算距离介于:(double)fromLat fromLong:(double)fromLong toLat:(double)toLat toLong:(double)toLong:(double)toLong{ return[self computeangelween:fromLat lng1:fromLong lat2:toLat lng2:toLong]*6371009.0; } -(double)computeAngleBeween:(double)lat1 lng1:(double)lng1 lat2:(double)lat2 lng2:(double)lng2:(double)lng2{ 返回[自距离弧度:[自下降弧度:lat1]lng1:[自下降弧度:lng1]lat2:[自下降弧度:lat2]lng2:[自下降弧度:lng2]]; } -(双)度弧度:(双)参数度{ 返回参数度*M_PI/180; } -(双)距离弧度:(双)lat1 lng1:(双)lng1 lat2:(双)lat2 lng2:(双)lng2:(双)lng2{ 返回[self-arcHav:[self-havDistance:lat1 lat2:lat2不同角度之间的差异:lng1-lng2]]; } -(双)阿尔乔夫:(双)x{ 返回2.0*asin(sqrt(x)); } -(双)距离(双)纬度(双)纬度(双)纬度(双)纬度(双)纬度差异(双)纬度差异{ 双和=[self-hav:lat1-lat2]+[self-hav:两个角度之间的差异]*cos(lat1)*cos(lat2); 回报金额; } -(双)hav:(双)x{ 双sinHalf=sin(x*0.5); 返回sinHalf*sinHalf; }
距离以米为单位。我希望这会有所帮助。

我在Swift中创建了一个CLLocation扩展,可以这样使用:

let distance = location.distanceToLine(from: start, to: end)
扩展位置{
///返回从当前对象位置到两个指定位置之间的假想线的最短距离(以米为单位)。
///
///-参数:
///-开始:构成假想线的第一个位置。
///-结束:构成假想线的第二个位置。
///-返回:当前对象位置与假想线之间的最短距离(以米为单位)。
func distanceToLine(从开始:CLLocation到结束:CLLocation)->CLLocationDistance{
设s0lat=度弧度(坐标纬度)
设s0lng=度弧度(坐标经度)
设s1lat=度弧度(起点坐标纬度)
设s1lng=度弧度(起点坐标经度)
设s2lat=度弧度(终点坐标纬度)
设s2lng=度弧度(终点坐标经度)
设s2s1lat=s2lat-s1lat
设s2s1lng=s2lng-s1lng
设u=((s0lat-s1lat)*s2s1lat+(s0lng-s1lng)*s2s1lng)/(s2s1lat*s2s1lat+s2s1lng*s2s1lng)
如果u=1.0{
返回距离(起点:终点)
}否则{
设sa=CLLocation(纬度:坐标.latitude-start.coordinate.latitude,
经度:坐标。经度-开始。坐标。经度)
设sb=CLLocation(纬度:u*(end.coordinate.latitude-start.coordinate.latitude),
经度:u*(end.coordinate.longitude-start.coordinate.longitude))
返回sa距离(从:sb)
}
}
私有函数度弧度(degrees:Double)->Double{return degrees*.pi/180.0}
}

上面的一个答案有一些错误-我在下面的代码中修复了它们

  - (double) distanceOfPointToLine :(double)currentLocationX currentLocationY:(double)currentLocationY x1:(double)x1 y1:(double)y1 x2:(double)x2 y2:(double)y2 {
    if (x1 == x2 && y1 == y2) {
      return  [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2];
    } else {
        double s0lat = [self degreeToRadians:currentLocationX];
        double s0lng = [self degreeToRadians:currentLocationY];
        double s1lat = [self degreeToRadians:x1];
        double s1lng = [self degreeToRadians:y1];
        double s2lat = [self degreeToRadians:x2];
        double s2lng = [self degreeToRadians:y2];
        double s2s1lat = s2lat - s1lat;
        double s2s1lng = s2lng - s1lng;
        double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
        if (u <= 0.0) {
             return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x1 toLong:y1];
        } else if (u >= 1.0) {
            return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2];
        } else {
            return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x1 + u * (x2-x1) toLong:y1 + u * (y2-y1)];
        }
    }
   
}
点线的(双)距离:(双)当前位置x当前位置y:(双)当前位置y x1:(双)x1 y1:(双)y1 x2:(双)x2 y2:(双)y2{ 如果(x1==x2&&y1==y2){ 返回[自计算的距离介于:currentLocationX from long:currentLocationY toLat:x2 toLong:y2]; }否则{ 双s0lat=[自度弧度:currentLocationX]; 双s0lng=[自度弧度:当前位置Y]; 双s1lat=[自度弧度:x1]; 双s1lng=[自度弧度:y1]; 双s2lat=[自度弧度:x2]; 双s2lng=[自度弧度:y2]; 双s2s1lat=s2lat-s1lat;
  - (double) distanceOfPointToLine :(double)currentLocationX currentLocationY:(double)currentLocationY x1:(double)x1 y1:(double)y1 x2:(double)x2 y2:(double)y2 {
    if (x1 == x2 && y1 == y2) {
      return  [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2];
    } else {
        double s0lat = [self degreeToRadians:currentLocationX];
        double s0lng = [self degreeToRadians:currentLocationY];
        double s1lat = [self degreeToRadians:x1];
        double s1lng = [self degreeToRadians:y1];
        double s2lat = [self degreeToRadians:x2];
        double s2lng = [self degreeToRadians:y2];
        double s2s1lat = s2lat - s1lat;
        double s2s1lng = s2lng - s1lng;
        double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
        if (u <= 0.0) {
             return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x1 toLong:y1];
        } else if (u >= 1.0) {
            return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x2 toLong:y2];
        } else {
            return [self computeDistanceBetween:currentLocationX fromLong:currentLocationY toLat:x1 + u * (x2-x1) toLong:y1 + u * (y2-y1)];
        }
    }
   
}