gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:53 25 4
gpt4 key购买 nike

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

最佳答案

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

- (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com