में ज़ूम किए जाने पर रूट लाइन सड़कों का पालन करें, मैंने सरल एप्लिकेशन लिखा जो मैपकिट पर दो स्थानों के बीच मार्ग खींचता है। मैं Google मानचित्र एपीआई का उपयोग कर रहा हूं। मैंने ऑनलाइन संसाधनों का उपयोग किया था और यहां Google का अनुरोध करने के लिए मैं जिस कोड का उपयोग कर रहा हूं:मैपकिट -
_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];
यह बेकार ढंग से काम करता है।
लेकिन: जब मैं इस चलाने के लिए और ला और शिकागो के बीच मार्ग दिखाने के लिए प्रयास करते हैं, यह इस प्रकार की तरह लग रहा है। जब मैं सड़क के स्तर के लिए नक्शे ज़ूम है, तो यहां मार्ग की तरह लग रहा है:
क्या किसी को पता है कि मैं कैसे प्राप्त कर सकते हैं कि मार्ग मैं ड्राइंग हूँ सड़कों इस प्रकार जब नक्शा ज़ूम इन करता है? मैं सड़कों के माध्यम से सटीक पथ दिखाने के लिए मार्ग चाहता हूं। मुझे नहीं पता कि Google को मेरे अनुरोध में कुछ अतिरिक्त पैरामीटर जोड़ने की आवश्यकता है या नहीं।
कोई भी मदद या सलाह बहुत अच्छी होगी। अग्रिम में बहुत धन्यवाद!
[संपादित करें # 1: जोड़ना अनुरोध URL और गूगल से प्रतिक्रिया]
ऊपर कोड से आपरेशन वस्तु बनाने के बाद मेरा अनुरोध URL ऐसा दिखाई देगा:
http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790
बस पेस्ट कि अपने ब्राउज़र पर यूआरएल और आप जेएसओएन डेटा देखेंगे जो Google मेरे कोड में प्रतिक्रिया के रूप में भेजता है।
[# 2 संपादित करें: गूगल से जवाब पार्स और पथ निर्माण]
- (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;
}
मैं इस पुस्तकालय का उपयोग कर रहा हूं। यह मेरे लिए अच्छा काम कर रहा है। https://github.com/kadirpekel/MapWithRoutes –