[英]Tracking user path in Google maps iOS SDK
在他開始旅行之后,我一直在嘗試繪制從一個點到另一個點的用戶路徑。 在用戶開始旅行后,我正在使用Google Maps 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地圖的整潔路徑?
發生這種情況是因為您的位置不是連續更新的,並且當它更新多段線時會在兩點之間筆直畫出,因此當您獲得下一個位置時,您必須三思而后行,將其稱為此 api。
當您調用此api時,您將獲得傳遞的兩點之間的路線數組,從而獲得最佳路線(可能是第一條路線)。 從該路線字典中提取overview_polyline對象。 overview_polyline對象是您兩個點之間的位置點的經度和緯度數組。
當您通過以下方法將其轉換時,您將具有所需的精確折線
有兩種解碼折線的方法
第一種方法
#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];
}
第二種方法
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];
希望這件事對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.