且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-30 11:03:10

以下代码为当前位置到欲望之间的绘制路径位置

   - (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;
//配置来自位置的信息的新事件
CLLocationCoordinate2D coordinates = [位置坐标];

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

NSLog(@dLatitude:%@,纬度);
NSLog(@dLongitude:%@,longitude);


Place * home = [[[Place alloc] init] autorelease];
home.name = @Home;
home.description = @甜蜜的家;
home.latitude = coordinate.latitude;
home.longitude = coordinate.longitude;

Place * office = [[[Place alloc] init] autorelease];
office.name = @Office;
office.description = @坏办公室;
office.latitude = 23.329404;
office.longitude = 72.0039299;

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

App Applegate方法包含



(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:$ {code} (CLLocation *)oldLocation {
MapWithRoutesAppDelegate * app =(MapWithRoutesAppDelegate *)[[UIApplication sharedApplication] delegate];
app.userlocation = newLocation;
NSLog(@latitide:%。7f,longitude:%.7f,app.userlocation.coordinate.latitude,app.userlocation.coordinate.longitude);
}

您可以从这里下载完整的源代码。



可以帮助很多。


I want to show a route between my current location and desired location. I am able to do this but when the source point and destination point so far then it will not show a route and gave the memory warning. Can you suggest any sample code or any way to do this?

Following Code for the draw path between the current location to desire location

- (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);
}

In Appdelegate Method contain

#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.

May this help lot.