Decoding polyline with new Google Maps API

后端 未结 6 1691
时光取名叫无心
时光取名叫无心 2020-12-07 21:12

I am drawing a route between two points in a map. I receive the points this way:

StringBuilder urlString = new StringBuilder();
    urlString.append("htt         


        
6条回答
  •  广开言路
    2020-12-07 21:52

    Here is the implementation in iOS, for anybody who is curious, which is entirely based upon @Fustigador's answer, but converted to iOS. Note that I have not formatted the code accordingly. I only added some variables for the sake of returning the promised object.

    - (GMSPath *)decodedPolylinePathFromEncodedPolylineString:(NSString *)encodedPolylineString {
    
    
        NSString *decodedPolylineString = @"";
        GMSMutablePath *decodedPolylinePath = [GMSMutablePath new];
        CLLocationCoordinate2D decodedCoordinate;
        CLLocationDegrees latitude, longitude;
    
        int index = 0;
        NSUInteger len = encodedPolylineString.length;
    
        int lat = 0, lng = 0;
    
        while (index < len) {
            int b, shift = 0, result = 0;
            do {
    
                b = [encodedPolylineString characterAtIndex:index++] - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;
    
            shift = 0;
            result = 0;
            do {
                b = [encodedPolylineString characterAtIndex:index++] - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;
    
            latitude =  (((double) lat / 1E5));
            longitude = (((double) lng / 1E5));
    
            decodedCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
    
            decodedPolylineString = [NSString stringWithFormat:@"%f%f", latitude, longitude];
            ;
            NSLog(@"%@",decodedPolylineString);
    
            [decodedPolylinePath addCoordinate:decodedCoordinate];
        }
    
    
        return decodedPolylinePath;
    }
    

提交回复
热议问题