Tracking user path in Google maps iOS SDK

蹲街弑〆低调 提交于 2019-12-12 06:19:45

问题


I've been trying to plot the user path from one point to other after he starts his travel. Am using Google maps iOS SDK and GMSPolyLine to plot the path after the user starts his travel.

Am tracking the travel using locationManager:didUpdateLocation and plotting the line after the user updates his location. Am not able to get the paths plotted properly as it happens in google maps when we search for paths from one point to other. I've added screenshots for the difference happening.

My app: https://www.dropbox.com/s/h1wjedgcszc685g/IMG_6555.png?dl=0

The above is my app's screenshot and you can note that the turns are not plotted properly

The Output required: https://www.dropbox.com/s/poqaeadh1g93h6u/IMG_6648.png?dl=0

Can anyone point me towards the best practice to plot a neat path similar to google maps?


回答1:


This happen because your location is not update continuously and when it update polyline is draw straight between two point so you have to do one think when you get the next location you have called this api.

When you called this api you get the route array between two point you passed from that you get your best route(possible first route). From that route dictionary extract the overview_polyline object. overview_polyline object is decoded latitude and longitude array of the location point between your two point.

when you convert it via following methods then you have exact polyline which you want

There are two methods to decode polyline

First method

#pragma mark
#pragma mark - decode polyline
// these function is given by client
-(void) decodePoly:(NSString *)encoded Color:(UIColor *)color
{
    GMSMutablePath *path = [[GMSMutablePath alloc] init];
    //    NSString *encoded=@"g|vfEmo{gMz@h@PJNFRJNDXHJ@b@FZ@F?L?XAVCr@Kj@Ut@]zAi@HETE^IVELCPANAhCYHAHCHCHCHEh@i@";

    NSUInteger index = 0, len = encoded.length;
    int lat = 0, lng = 0;
    while (index < (len - 2)) {
        int b, shift = 0, result = 0;
        do {
            //            b = encoded.charAt(index++) - 63;
            b = [encoded 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 = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:((double) lat / 1E5) longitude:((double) lng / 1E5)];
        [path addCoordinate:loc.coordinate];
    }
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
    // Add the polyline to the map.
    polyline.strokeColor = color;
    polyline.strokeWidth = 5.0f;
    polyline.map = [self getGoogleMap];
}  

Second methods

GMSPolyline *polyline =[GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:"your encoded string"]];
        // Add the polyline to the map.
        polyline.strokeColor = color;
        polyline.strokeWidth = 5.0f;
        polyline.map = [self getGoogleMap];

May this thing help you.



来源:https://stackoverflow.com/questions/29826387/tracking-user-path-in-google-maps-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!