Search code examples
iosobjective-cgoogle-mapsgoogle-maps-sdk-ios

IOS - googlemaps polyutils equivalent class in ios


I am trying to find the PolyUtils.distanceToLine equivalent function in IOS google maps util Library.

The function distanceToLine has the ability to compute the distance between a point and a line segment, And I am not able to find anything similar in Google maps IOS utils library.

Link to android utils library

Link to IOS utils library


Solution

  • Ok even I faced the same issue so I converted PolyUtils Android Library to 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;
    }
    

    The distance is in meters. I hope this is helpful.