在Google地图iOS SDK中跟踪用户路径 [英] Tracking user path in Google maps iOS SDK

查看:117
本文介绍了在Google地图iOS SDK中跟踪用户路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在他开始旅行之后,我一直试图在一个地方绘制用户路径。我使用Google地图iOS SDK和GMSPolyLine来绘制用户开始旅行后的路径。



使用 locationManager:didUpdateLocation 并在用户更新其位置后绘制线条。当我们搜索从一个点到另一个点的路径时,无法正确绘制路径,因为它发生在Google地图中。我添加了屏幕截图,以了解所发生的差异。



我的应用:
https://www.dropbox.com/s/h1wjedgcszc685g/IMG_6555.png?dl=0



以上是我应用程序的截图,你可以注意到这些转折点没有被正确地绘制出来

所需输出:
https://www.dropbox.com/s/poqaeadh1g93h6u/IMG_6648.png?dl=0

任何人都可以指向我的最佳做法,绘制类似于Google地图的整洁路径吗?

发生这种情况是因为你的位置不是连续更新的,当它更新多段线时会在两点之间画直线,所以当你得到下一个你称为 this api。当你调用这个API时你得到了你从最好的路线(可能的第一条路线)通过的两点之间的路线数组。从该路由字典中提取 overview_polyline 对象。 overview_polyline 对象是您的两点之间的位置点的纬度和经度数组。



当您通过以下方法转换它时,你需要的折线

有两种解码折线的方法


第一种方法



  #pragma mark 
#pragma mark - decode polyline
/ /这些函数由客户端提供
- (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 = [编码字符索引:索引++] - 63;
result | =(b& 0x1f)<<转移;
shift + = 5;
} while(b> = 0x20);
int dlat =((result& 1)!= 0?〜(result>> 1):( result>> 1));
lat + = dlat;
shift = 0;
结果= 0;
do {
b = [encoded characterAtIndex:index ++] - 63;
result | =(b& 0x1f)<<转移;
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];
//将折线添加到地图。
polyline.strokeColor = color;
polyline.strokeWidth = 5.0f;
polyline.map = [self getGoogleMap];
}




第二种方法



  GMSPolyline * polyline = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:your encoded string]]; 
//将折线添加到地图。
polyline.strokeColor = color;
polyline.strokeWidth = 5.0f;
polyline.map = [self getGoogleMap];

这件事可以帮助您。


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?

解决方案

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.

这篇关于在Google地图iOS SDK中跟踪用户路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆