我正在尝试在IOS谷歌地图实用程序库中找到PolyUtils.distanceToLine
等效功能。
函数distanceToLine
能够计算点与线段之间的距离,而且我无法在Google Maps IOS utils库中找到任何类似的东西。
Link to android utils library
Link to IOS utils library
最佳答案
好的,即使我也遇到了同样的问题,所以我将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;
}
距离以米为单位。我希望这是有帮助的。