簡體   English   中英

如何在 MKMapView 上繪制我當前位置到所需位置之間的路線?

[英]How to draw a route between my current location to desired location on 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);
}

您可以從這里下載完整的源代碼。

可能這有很大幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM