【问题标题】:How to draw routes on maps in ios 6?如何在 iOS 6 中的地图上绘制路线?
【发布时间】:2012-10-26 03:36:28
【问题描述】:

我想在地图上显示地图并绘制路线。我的应用程序支持 ios 4 plus。那么我应该如何使用地图在 ios 6 上和以前一样工作。我也想知道我应该在我的应用程序中使用自定义地图视图来显示地图和路线,还是应该使用

[[UIApplication sharedApplication] openURL:]

我从来没有用户MapKits。所以请提供任何教程。如果有任何第三方库可以使用,也请告诉我。

【问题讨论】:

    标签: iphone ios mapkit ios6-maps maproute


    【解决方案1】:

    如果您不想要应用内地图。使用以下内容:

    NSString *destinationAddress = @"Amsterdam";
    
    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
    
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {
            if([placemarks count] > 0) {
    
                MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];
    
                MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];
    
                MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];
    
    
                NSArray *mapItems = @[mapItem, mapItem2];
    
                NSDictionary *options = @{
            MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
            MKLaunchOptionsMapTypeKey:
                [NSNumber numberWithInteger:MKMapTypeStandard],
            MKLaunchOptionsShowsTrafficKey:@YES
                };
    
                [MKMapItem openMapsWithItems:mapItems launchOptions:options];
    
            } else {
                //error nothing found
            }
        }];
        return;
    } else {
    
        NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];
    
        NSString *urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
                     [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                     [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
    }
    

    这将打开地图应用程序并检查它是 ios5 还是 ios6。

    对于 ios5,我使用这篇文章中的 LocalizedCurrentLocation http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

    对于 ios6,我使用 CLGeocoder 获取地标,然后用它和当前位置打开地图。

    记得加CoreLocation.framework和MapKit.framework

    【讨论】:

    • 你好。你能给我一个想法,我怎样才能得到一个描绘导航路线和行车路线的地图应用程序......请帮忙。因为我使用了你上面的代码,但它把我带出了我的原生应用程序,然后在检查路线或地图后我无法回到我的应用程序.. 谢谢!!
    • 我帖子的第一行:“如果您不想要应用内地图。”做一个应用内导航地图并不容易。检查这篇文章以获取一些信息stackoverflow.com/questions/2834523/…
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多