iphone - 如何在 MKMapView 上绘制当前位置到所需位置之间的路线?

标签 iphone ios xcode dictionary mkmapview

我想显示我当前位置和所需位置之间的路线。我能够做到这一点,但是当源点和目的地点到目前为止时,它不会显示路线并给出内存警告。您能建议任何示例代码或任何方法来执行此操作吗?

最佳答案

以下代码用于绘制当前位置到所需位置之间的路径

- (void)viewDidLoad {
    [super viewDidLoad];

    mapView = [[[MapView alloc] initWithFrame:
                         CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];

    [self.view addSubview:mapView];
    [self performSelector:@selector(LoadMaps) withObject:self afterDelay:1.0];
}

-(void)LoadMaps{
    MapWithRoutesAppDelegate *app = (MapWithRoutesAppDelegate *)[[UIApplication sharedApplication]delegate];
    CLLocation *location = app.userlocation;
    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"dLatitude : %@", latitude);
    NSLog(@"dLongitude : %@",longitude);


    Place* home = [[[Place alloc] init] autorelease];
    home.name = @"Home";
    home.description = @"Sweet home";
    home.latitude = coordinate.latitude;
    home.longitude = coordinate.longitude;

    Place* office = [[[Place alloc] init] autorelease];
    office.name = @"Office";
    office.description = @"Bad office";
    office.latitude = 23.329404;
    office.longitude = 72.0039299;

    [mapView showRouteFrom:home to:office];
    NSLog(@"latitide:%.7f, longitude: %.7f ", coordinate.latitude, coordinate.longitude);
}

在Appdelegate方法中包含

#pragma mark- CLLocationManager delegate
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{
    MapWithRoutesAppDelegate *app = (MapWithRoutesAppDelegate*)[[UIApplication sharedApplication] delegate];
    app.userlocation = newLocation;
    NSLog(@"latitide:%.7f, longitude: %.7f ", app.userlocation.coordinate.latitude, app.userlocation.coordinate.longitude);
}

You can download the complete source code from here.

愿这有帮助。

关于iphone - 如何在 MKMapView 上绘制当前位置到所需位置之间的路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13489756/

相关文章:

XCode 不会从 App Store 更新

ios - 我无法在 xcode 9.1 中的 UIButton 上显示图像

ios - 用于提取负数作为方程一部分的 NSString 正则表达式

ios - 添加多个 ImageView 帧时对象的数量只有一个

ios - 将字符串传递给第二个 View Controller

ios - Xcode 10 : what is in 'Library/Developer/Xcode/UserData/IB Support/Simulator Devices' ?

iphone - 另一个 VC 上的“setInt”不起作用

html - Iphone响应式网站问题

iphone - GKSession sendDataToAllPeers 在客户端上?

iphone - iOS-OAuth 实现如何创建框架? (简单的)