'a yakınlaştığında rota çizgisini takip et. MapKit'te iki konum arasında rota çizen basit bir uygulama yazdım. Google Harita API'sı kullanıyorum. Online ve burada buldum kaynakları kullanılan Google'a istek yapmak kullanıyorum kodu: Bu kusursuz çalışıyorMapKit - Harita çizgisi
_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
[_httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude] forKey:@"origin"];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", endCoordinate.latitude, endCoordinate.longitude] forKey:@"destination"];
[parameters setObject:@"true" forKey:@"sensor"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200)
{
NSLog(@"Success: %@", operation.responseString);
}
else
{
NSLog(@"Status code = %d", statusCode);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", operation.responseString);
}
];
[_httpClient enqueueHTTPRequestOperation:operation];
.
ANCAK: Ben bu çalıştırın ve LA ve Şikago arasındaki rotayı göstermeye çalıştığınızda ne kadar benziyor, burada. Ben sokak seviyesine harita zumladığınızda, burada rota benziyor açıklanmıştır:
kimse haritası büyültüldüğünde ben çiziyorum o rota sokaklar izler elde edebilirsiniz biliyor mu? Sokaklarda tam yol göstermek için rotayı istiyorum. İsteğime Google’a ek parametrenin eklenmesi gerekip gerekmediğini bilmiyorum.
Herhangi bir yardım veya tavsiye harika olur. Şimdiden çok teşekkürler!
[değiştir # 1: Google'dan istek URL ve tepkisini ekleme]
yukarıdaki koddan operasyon nesnesi oluşturduktan sonra My istek URL'si şöyle görünür:
http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790
Sadece o yapıştırmak Tarayıcınızın URL'si ve Google'ın, koduma da girdiğim yanıt olarak gönderdiği JSON verilerini göreceksiniz.
: Ben MapKit tarafından kullanılan projeksiyon Google Maps tarafından kullanılan farklı olduğunu düşünüyoruz
- (void)parseResponse:(NSData *)response
{
NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSArray *routes = [dictResponse objectForKey:@"routes"];
NSDictionary *route = [routes lastObject];
if (route)
{
NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
_path = [self decodePolyLine:overviewPolyline];
}
}
- (NSMutableArray *)decodePolyLine:(NSString *)encodedStr
{
NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len)
{
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do
{
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(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);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array addObject:location];
}
return array;
}
Bu kütüphaneyi kullanıyorum. Bu benim için iyi çalışıyor. https://github.com/kadirpekel/MapWithRoutes –