如何在两个位置之间绘制路线并使用MapKit绘制主要点? [英] How to draw route between two locations and plot main points also using MapKit?
问题描述
我正在使用MapKit api来获取地图上的当前位置,并在两个位置之间绘制路线。我还希望获得其路线之间的所有主要站点。
我使用以下函数获取两个位置之间的路径
I am using MapKit api to get current location on map and drawing route between two location pointed by drop pins.I also want to get all the main stands between its route. I m using below function to get route between two location
- (NSArray*)getRoutePointFrom:(MyLocation*)origin to:(MyLocation*)destination
{
NSString* saddr = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false&avoid=highways&mode=driving",saddr,daddr]];
NSError *error=nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: &error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
SBJsonParser *json=[[SBJsonParser alloc] init];
NSDictionary *dic=[json objectWithString:responseString error:nil];
NSDictionary *nextdic=[dic valueForKey:@"routes"];
NSDictionary *legdic=[nextdic valueForKey:@"legs"];
NSDictionary *stepdic=[legdic valueForKey:@"steps"];
NSArray *array=[[NSArray alloc] initWithArray:[[stepdic valueForKey:@"polyline"] valueForKey:@"points"]];
NSString *string=[NSString stringWithFormat:@"%@",[[array objectAtIndex:0] objectAtIndex:0]];
return [self decodePolyLine:[string mutableCopy]];
}
-(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];
// printf("[%f,", [latitude doubleValue]);
// printf("%f]", [longitude doubleValue]);
CLLocation *loc =[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
[array addObject:loc];
}
NSLog(@"array in decode polygon is %@",array);
return array;
}
但它不起作用。 ..
but it is not working . ..
关于这个
的帮助谢谢!...
help regarding this thank you!...
推荐答案
这个问题已被问过好几次了。我猜您正在从 http://iosguy.com/获取代码2012/05/22 / tracing-routes-with-mapkit /
你也可以看看那个问题:在iOS中绘制多点积分路线
The question has been asked several times. I guess you are taking code from http://iosguy.com/2012/05/22/tracing-routes-with-mapkit/ You can also look at that SO question: Plotting Route with Multiple Points in iOS
您可以从 http://iosboilerplate.com 也是有贡献的。
You can get the code from http://iosboilerplate.com too and contribute to it.
最后但是不仅仅是,有一个框架可以帮助你以一小笔钱来做这件事(但没有什么比你需要做的那样):
http://www.cocoacontrols.com/controls/mtdirectionskit
And last but not least, there's a framework out there that can help you do it for a small sum of money (but nothing compared to what it would take you to do same): http://www.cocoacontrols.com/controls/mtdirectionskit
这篇关于如何在两个位置之间绘制路线并使用MapKit绘制主要点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!